Problem generating a web / radar chart

Topics: Developer Forum
Dec 6, 2012 at 2:31 AM

Hello everybody,

Directly to the issue: I'm not able to create a web chart (radar chart) with PHPExcel.


I paste my code below. I'm able to generate a 4 bars chart without problems but the same code doesn't work when I try to do the same with the web / radar chart.


According the the API of the library, I pass the constant PHPExcel_Chart_DataSeries::TYPE_RADARCHART to the object PHPExcel_Chart_DataSeries.


After creating the Excel file and trying to open it, Excel 2010 crashes with a reference to   /xl/drawings/drawing1.xml and I never see any chart. 

Let me know where's the problem if you see it.


Working with PHP 5 and the latest version of PHPExcel, 1.7.8, testing on Apache Server 2.2 in Unix. 


Thanks a lot.

CODE:

 

$total_datos=mysql_num_rows($result_gene);
$datos_generales=array();
array_push($datos_generales, array('', 'Sup.', 'Sub.', 'Pares', 'Tú'));
while($datos_gene=mysql_fetch_array($result_gene)) {
array_push($datos_generales, array($datos_gene['nombre'], $datos_gene['media_sup'], $datos_gene['media_sub'], $datos_gene['media_par'], $datos_gene['sol']));
$fila_actual++;
}
$sheet = $workbook->getActiveSheet();
$sheet->fromArray($datos_generales, null, 'B18');
$labels=array();
$categories=array(new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$19:$B$22', null, 4));
$values=array();
for($fila=19; $fila<19+$total_datos; $fila++) {
array_push($values, new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$'.$fila.':$F$'.$fila, null, 4));
}
array_push($labels, new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$18', null, 4));
array_push($labels, new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$18', null, 4));
array_push($labels, new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$18', null, 4));
array_push($labels, new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$F$18', null, 4));
$series = new PHPExcel_Chart_DataSeries(
  PHPExcel_Chart_DataSeries::TYPE_BARCHART,       // plotType
  PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,  // plotGrouping
  array(0, 1, 2, 3),                                       // plotOrder
  $labels,                                        // plotLabel
  $categories,                                    // plotCategory
  $values                                         // plotValues
);
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
$chart = new PHPExcel_Chart(
  'chart1',                                       // name
  null,                                           // title
  $legend,                                        // legend
  $plotarea,                                      // plotArea
  true,                                           // plotVisibleOnly
  0,                                              // displayBlanksAs
  null,                                           // xAxisLabel
  null                                            // yAxisLabel
);
$fila_actual+=2;
$chart->setTopLeftPosition('H'.($fila_actual-13));
$chart->setBottomRightPosition('O'.($fila_actual-1));
$sheet->addChart($chart);

Coordinator
Dec 6, 2012 at 7:31 AM

Taken directly from the example 33chartcreate-radar.php from the current github develop branch:

$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
 array(
  array('', 2010, 2011, 2012),
  array('Jan',   47,   45,  71),
  array('Feb',   56,   73,  86),
  array('Mar',   52,   61,  69),
  array('Apr',   40,   52,  60),
  array('May',   42,   55,  71),
  array('Jun',   58,   63,  76),
  array('Jul',   53,   61,  89),
  array('Aug',   46,   69,  85),
  array('Sep',   62,   75,  81),
  array('Oct',   51,   70,  96),
  array('Nov',   55,   66,  89),
  array('Dec',   68,   62,  0),
 )
);

// Set the Labels for each data series we want to plot
//  Datatype
//  Cell reference for data
//  Format Code
//  Number of datapoints in series
//  Data values
//  Data Marker
$dataseriesLabels = array(
 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011
 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // 2012
);
// Set the X-Axis Labels
//  Datatype
//  Cell reference for data
//  Format Code
//  Number of datapoints in series
//  Data values
//  Data Marker
$xAxisTickValues = array(
 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$13', NULL, 12), // Jan to Dec
 new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$13', NULL, 12), // Jan to Dec
);
// Set the Data values for each data series we want to plot
//  Datatype
//  Cell reference for data
//  Format Code
//  Number of datapoints in series
//  Data values
//  Data Marker
$dataSeriesValues = array(
 new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$13', NULL, 12),
 new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$13', NULL, 12),
);
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
 PHPExcel_Chart_DataSeries::TYPE_RADARCHART,    // plotType
 NULL,             // plotGrouping
 range(0, count($dataSeriesValues)-1),     // plotOrder
 $dataseriesLabels,          // plotLabel
 $xAxisTickValues,          // plotCategory
 $dataSeriesValues,          // plotValues
 NULL,             // smooth line
 PHPExcel_Chart_DataSeries::STYLE_MARKER     // plotStyle
);
// Set up a layout object for the Pie chart
$layout = new PHPExcel_Chart_Layout();
// Set the series in the plot area
$plotarea = new PHPExcel_Chart_PlotArea($layout, array($series));
// Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new PHPExcel_Chart_Title('Test Radar Chart');

// Create the chart
$chart = new PHPExcel_Chart(
 'chart1',  // name
 $title,   // title
 $legend,  // legend
 $plotarea,  // plotArea
 true,   // plotVisibleOnly
 0,    // displayBlanksAs
 NULL,   // xAxisLabel
 NULL   // yAxisLabel  - Radar charts don't have a Y-Axis
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('F2');
$chart->setBottomRightPosition('M15');
// Add the chart to the worksheet
$objWorksheet->addChart($chart);

// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
Dec 7, 2012 at 10:39 AM

Thank you!! That example helped a lot.