1

有问题的库是PHPExcel 1.7.7

我使用了在codeplex的一个线程中找到的示例代码来使用 PHPExcel 创建图表。但是,论坛中提供的示例代码只处理两列数据,我希望扩大图表中显示的列数。无论我尝试如何处理第三组数据(D 列及以后)都不会显示。

标签/类别字符串没有问题 - 因此与任何保留名称都没有冲突(即使它们是字符串),我返回 plotValues 以查看是否丢失了任何内容,我仍然看到定义的列显示在那里,但无法弄清楚为什么图表中没有显示第三列。

$excel->getActiveSheet()->fromArray(array(
    $header ,
    array('Central',   12,   15 , 12 ),
    array('Northeast',   56,   73 , 10 ),
    array('Southeast',   52,   61 , 33 ),
    array('Western',   30,   32 , 55 ),
));


$labels = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$B$1', null, 1),
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$C$1', null, 1),
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$D$1', null, 1),
);

$categories = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Summary!$A$2:$A$5', null, 4),
);

// this is where the problem arises - the third DataSeriesValues is not being displayed
$values = array(
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$B$2:$B$5', null, 4),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$C$2:$C$5', null, 4),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$D$2:$D$5', null, 4),
/*
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$C$2:$C$5', null, 5),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$D$2:$D$5', null, 5)
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$E$2:$E$5', null, 5),
    new PHPExcel_Chart_DataSeriesValues('Number', 'Summary!$F$2:$F$5', null, 5),
*/
);

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

$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea(null, 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
);

$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');
$excel->getActiveSheet()->addChart($chart);
$excel->getActiveSheet()->setTitle("Summary");
4

1 回答 1

2

正如 Robbie 指出的那样:当您在创建系列时设置 plotOrder 值时,您只是在告诉图表使用图 0 和 1

array(0, 1),

不绘制 0、1 和 2

array(0, 1, 2),

我倾向于使用

range(0, count($values)-1),

确保包含所有数据系列(无论有多少)

于 2012-10-11T06:28:37.613 回答