1
votes

I want to use phpexcel to draw a radar chart, but when I open the generated file with Microsoft Excel 2010, the chart's main axis is missing(like below image, the first is wrong, the second is correct).

The wrong chart draw by phpexcel

The correct chart draw by Excel 2010

Then I read phpexcel's examples '33chartcreate-radar.php', but it has the same problem.

How do I make the main axis show?

$excel = new PHPExcel();
$sheet = $excel->getActiveSheet();
$sheet->setTitle('sheet');

$data = [
    ['Direction', 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'],
    ['Frequency', 10, 20, 5, 10, 15, 30, 5, 5]
];

$sheet->fromArray($data, null, 'A1');

$dataseriesLabels = array(new PHPExcel_Chart_DataSeriesValues('String',
    'sheet!$A$2', NULL, 1));
$xAxisTickValues = array(new PHPExcel_Chart_DataSeriesValues('String',
    'sheet!$B$1:$I$1', NULL, 8));
$dataSeriesValues = array(new PHPExcel_Chart_DataSeriesValues('String',
    'sheet!$B$2:$I$2', NULL, 8));

$series = new PHPExcel_Chart_DataSeries(
    PHPExcel_Chart_DataSeries::TYPE_RADARCHART,
    null,
    range(0, count($dataSeriesValues)-1),
    $dataseriesLabels,
    $xAxisTickValues,
    $dataSeriesValues,
    null,
    null,
    PHPExcel_Chart_DataSeries::STYLE_MARKER
);
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT,
    NULL, false);
$title = new PHPExcel_Chart_Title('Wind Rose Diagram');
$chart = new PHPExcel_Chart(
    'chart1',
    $title,
    $legend,
    $plotarea,
    true,
    0,
    null,
    null
);
$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('I32');
$sheet->addChart($chart);

$ExcelWrite = PHPExcel_IOFactory::createWriter($excel, "Excel2007");
$ExcelWrite->setIncludeCharts(true);
$ExcelWrite->save('D:\TestRadarChart.xlsx');
2

2 Answers

0
votes

If Excel vba is OK, I could display it.

ActiveChart.Axes(xlCategory).hasMajorGridlines=true

  1. Select the target chart on excel.

  2. Press Alt+F11 on excel, then VBE opens.

  3. Press Ctrl+G on VBE, then immediate window opens.

  4. Paste the above code.

  5. Place the cursor on the line of the code and press the enter key.

I made the file with PHPExcel and edit it with excel 2010.

0
votes

I do not know about side effect, but it may be possible to fix it.

I confirmed with PHPExcel 1.8.0.

source:

Lib/PHPExcel/Classes/PHPExcel/Writer/Excel2007/Chart.php(450)

before:

$objWriter->startElement('c:axPos');
    $objWriter->writeAttribute('val', "b");
$objWriter->endElement();

after:

$objWriter->startElement('c:axPos');
    $objWriter->writeAttribute('val', "b");
$objWriter->endElement();

$objWriter->startElement('c:majorGridlines');   //added
$objWriter->endElement();                       //added