How to send worksheet data to a PHPExcel chart

I took an example from 33chartcreate-pie.php from PHPExcel-1.8 and changed according to my need. now the example has an x ​​axis value similar.

$xAxisTickValues1 = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Q1 to Q4 ); 

Now, I want to add it as static as PASS and FAIL, therefore, I try it as

 $xAxisTickValues1 = array( new PHPExcel_Chart_DataSeriesValues('String', 'PASS:FAIL', NULL, 2), // Q1 to Q4 ); 

but it does not work. How to put a static value in this array?

+6
source share
3 answers

An example that you follow:

 $xAxisTickValues1 = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Q1 to Q4 ); 

Here the worksheet is the name of the worksheet. Thus, when plotting, it will read the value from the specified header, followed by columns from A2 to A5.

If you just want to add a static value. Create a cell with static content in it and pass the cell index to the data series values.

+2
source

We need to show the static labels in the cells to show them on the graph or PIE chart. When plotting, we can show and hide labels or values ​​using functions such as setShowVal, setShowCatName.

  $objPHPExcel = new PHPExcel(); $objWorksheet = $objPHPExcel->getActiveSheet(); $objWorksheet->fromArray( array( array('', 2010, 2011, 2012), array('PASS', 12, 15, 21), array('FAIL', 56, 73, 86) ) ); $dataSeriesLabels1 = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011 ); $xAxisTickValues1 = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$3', NULL, 2), // Q1 to Q4 ); $dataSeriesValues1 = array( new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$3', NULL, 2), ); // Build the dataseries $series1 = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType NULL, // plotGrouping (Pie charts don't have any grouping) range(0, count($dataSeriesValues1)-1), // plotOrder $dataSeriesLabels1, // plotLabel $xAxisTickValues1, // plotCategory $dataSeriesValues1 // plotValues ); // Set up a layout object for the Pie chart $layout1 = new PHPExcel_Chart_Layout(); /*$layout1->setShowVal(TRUE);*/ $layout1->setShowCatName(TRUE); // Set the series in the plot area $plotArea1 = new PHPExcel_Chart_PlotArea($layout1, array($series1)); // Set the chart legend $legend1 = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false); $title1 = new PHPExcel_Chart_Title('Test Pie Chart'); // Create the chart $chart1 = new PHPExcel_Chart( 'chart1', // name $title1, // title $legend1, // legend $plotArea1, // plotArea true, // plotVisibleOnly 0, // displayBlanksAs NULL, // xAxisLabel NULL // yAxisLabel - Pie charts don't have a Y-Axis ); 
+1
source

Change your ad array, e.g.

 array( array('', 2010, 2011, 2012), array('Q1', 12, 15, 21), array('Pass or fail', 56, 73, 86) ) 

. So you can set a static value. Now, to display it, you can add a property to your layout to show your category name for the chart with ->setShowCatName(TRUE);

+1
source

Source: https://habr.com/ru/post/1015414/


All Articles