From 5a86c55ed4a86125d88c2a384f5c5c0344755268 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 18 Jun 2012 21:47:17 +0100 Subject: [PATCH] Example for basic chart creation --- Tests/33chartcreate.php | 118 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Tests/33chartcreate.php diff --git a/Tests/33chartcreate.php b/Tests/33chartcreate.php new file mode 100644 index 00000000..15710d5e --- /dev/null +++ b/Tests/33chartcreate.php @@ -0,0 +1,118 @@ +getActiveSheet(); +$objWorksheet->fromArray( + array( + array('', 2010, 2011, 2012), + array('Q1', 12, 15, 21), + array('Q2', 56, 73, 86), + array('Q3', 52, 61, 69), + array('Q4', 30, 32, 0), + ) +); + +// Set the Labels for each dataset we want to plot +$labels = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', null, 1), // 2010 + 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 +$categories = array( + new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4 +); +// Set the Data values for each dataset we want to plot +$values = array( + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4), + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4), + new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', null, 4), +); + +// Build the dataseries +$series = new PHPExcel_Chart_DataSeries( + PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType + PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping + array(0, 1, 2), // plotOrder + $labels, // plotLabel + $categories, // plotCategory + $values // plotValues +); +// Set additional dataseries parameters +$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL); + +// Set the series in the plot area +$plotarea = new PHPExcel_Chart_PlotArea(null, array($series)); +// Set the chart legend +$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false); + +// Create the chart +$chart = new PHPExcel_Chart( + 'chart1', // name + null, // title + $legend, // legend + $plotarea, // plotArea + true, // plotVisibleOnly + 0, // displayBlanksAs + null, // xAxisLabel + null // yAxisLabel +); + +// Set the position where the chart should appear in the worksheet +$chart->setTopLeftPosition('A7'); +$chart->setBottomRightPosition('H20'); + +// Add the chart to the worksheet +$objWorksheet->addChart($chart); + + +// Save Excel 2007 file +echo date('H:i:s') , " Write to Excel2007 format" , PHP_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', __FILE__) , PHP_EOL; + + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , PHP_EOL; + +// Echo done +echo date('H:i:s') , " Done writing file" , PHP_EOL;