PHPExcel饼图标签和图例

use*_*114 5 charts phpexcel pie-chart

我有一个库PHPExcel(1.7.7)的问题:当我想创建一个饼图时,不显示标签和图例.但是,对于其他图形,我没有那个问题.你有什么解决方案吗?

谢谢.

这是使用的代码:

$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
    );
Run Code Online (Sandbox Code Playgroud)

小智 5

我们必须通过声明setShowVal(TRUE)来启用数据标签.

找到我应用的以下代码

$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
Run Code Online (Sandbox Code Playgroud)

需要声明应用的绘图区域

$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
);
Run Code Online (Sandbox Code Playgroud)


Per*_*ack 3

您是否因为忘记设置 xAxisLabel 和 yAxisLabel 而遇到此问题?

尝试创建一个数组,获取您想要设置的标签,然后像您所做的那样将其加载到该数组中,但设置plotLabel。例如:对于标签:

$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
);
Run Code Online (Sandbox Code Playgroud)

然后你可以做这样的事情:

$xAxisLabel = new PHPExcel_Chart_Title('xAxisLabel');
$yAxisLabel = new PHPExcel_Chart_Title('yAxisLabel');
Run Code Online (Sandbox Code Playgroud)

进而:

$chart = new PHPExcel_Chart(
'chart2',                                       // name
$title,                                         // title
$legend,                                        // legend
$plotarea,                                      // plotArea
true,                                           // plotVisibleOnly
0,                                              // displayBlanksAs
xAxisLabel,                                     // xAxisLabel
yAxisLabel                                      // yAxisLabel
);
Run Code Online (Sandbox Code Playgroud)