Stickers & Legends PHPExcel Piechart

I have a problem with the PHPExcel library (1.7.7): When I want to create a piechart, labels and legends are not displayed. However, with different graphics, I do not have this problem. Do you have any solution?

Thank.

Here is the code:

$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
    );
+5
source share
3 answers

We must include data labels by declaring setShowVal (TRUE).

Find the following code that 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 declared area of ​​the site must 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
);
+5
source

Don't you have a problem because ur forgot to install xAxisLabel and yAxisLabel?

Try creating an array that receives the labels you want to set, and then load it into that array, as you did, but setting plotLabel. for example: for tags:

$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
);

- :

$xAxisLabel = new PHPExcel_Chart_Title('xAxisLabel');
$yAxisLabel = new PHPExcel_Chart_Title('yAxisLabel');

:

$chart = new PHPExcel_Chart(
'chart2',                                       // name
$title,                                         // title
$legend,                                        // legend
$plotarea,                                      // plotArea
true,                                           // plotVisibleOnly
0,                                              // displayBlanksAs
xAxisLabel,                                     // xAxisLabel
yAxisLabel                                      // yAxisLabel
);
+2

I know that it is really late, but I think the answer should be given here. I am using PHPExcel version 1.8.0. I have the same problem and I found a solution.

To show legends you need to set the option plotGroupingtonull

$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 to display PieChart shortcuts, you need to enable data labels using the layout.

$layout = new PHPExcel_Chart_Layout();
$layout->setShowVal(TRUE);      
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
+2
source

All Articles