I have a problem with the library PHPExcel (1.7.7): When I want to create a piechart, labels and legends are not displayed. However, with other graphics, I do not have that problem. Do you have any a solution?
Thanks.
Here is the code used:
$categories = array(
new PHPExcel_Chart_DataSeriesValues('String', 'RECAPITULATIF!$B$6:$B$8', null, 3),
);
$values = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'RECAPITULATIF!$F$6:$F$8', null, 3),
);
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
array(0), // plotOrder
null, // plotLabel
$categories, // plotCategory
$values // plotValues
);
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
$title = new PHPExcel_Chart_Title('Pie chart');
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, null, false);
$chart = new PHPExcel_Chart(
'chart2', // name
$title, // title
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel
);
We have to enable data labels by declaring the setShowVal(TRUE).
Find the following code I applied
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
array(0, 1), // plotOrder
$labels, // plotLabel
$categories, // plotCategory
$values // plotValues
);
$layout1 = new PHPExcel_Chart_Layout();
$layout1->setShowVal(TRUE); // Initializing the data labels with Values
$layout1->setShowPercent(TRUE); // Initializing the data labels with Percentages
The applied plot area needs to be declared
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea($layout1, 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
);
Aren't u getting this problem because ur forgetting to set a xAxisLabel and yAxisLabel?
Try creating an array getting the labels u want to set and then load it in this array like u were doing, but setting the plotLabel. for example: for the labels:
$labels = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', null, 1),
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', null, 1),
);
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
array(0), // plotOrder
$labels, // plotLabel <----- u were setting null
$categories, // plotCategory
$values // plotValues
);
Then you could do something like this:
$xAxisLabel = new PHPExcel_Chart_Title('xAxisLabel');
$yAxisLabel = new PHPExcel_Chart_Title('yAxisLabel');
and then:
$chart = new PHPExcel_Chart(
'chart2', // name
$title, // title
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
xAxisLabel, // xAxisLabel
yAxisLabel // yAxisLabel
);
I know it's really late, But i think answer should be given here. I am using PHPExcel version 1.8.0. I have the same problem and i have found a solution.
For showing the Legends you have to set the plotGrouping
option to null
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
null, // plotGrouping
range(0, count($values)-1), // plotOrder
$labels, // plotLabel
$categories, // plotCategory
$values // plotValues
);
And for showing the PieChart Labels you have to enable data labels by using layout.
$layout = new PHPExcel_Chart_Layout();
$layout->setShowVal(TRUE);
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With