繁体   English   中英

使用 PhpOffice 将数据表元素添加到图表

[英]Adding Data Table element to the chart using PhpOffice

我试图通过 PhpOffice 制作图表,但在图表上方添加 Data.table 元素时遇到了一些困难。 我已经可以添加 Legend,但似乎也可以添加 Data Table。

https://phpoffice.github.io/PhpSpreadsheet/1.2.0/PhpOffice/PhpSpreadsheet/Chart/DataSeries.html

已经读过这个东西,但找不到正确的方法来做这个。 我正在使用标准图表构建示例,但出于我的目的对其进行了编辑

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->setTitle('Test Data');
$worksheet->fromArray(
    [
        ['2016', 'Value1', 400, 'Value2', 522],
        ['2017', '', 600, '', 253],
        ['2018', '', 800, '', 140],
        ['2019', '', 900, '', 335],
        ['2020', '', 500, '', 200],
    ]
);
$dataSeriesLabels = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, "'Test Data'!B1", null, 1),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, "'Test Data'!D1", null, 1),
];
$xAxisTickValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,  "'Test Data'!A1:A5", null, 4),

];
$dataSeriesValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, "'Test Data'!C1:C5", null, 4),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, "'Test Data'!E1:E5", null, 4),
];

$series = new DataSeries(
    DataSeries::TYPE_BARCHART, // plotType
    DataSeries::GROUPING_STACKED, // plotGrouping
    range(0, count($dataSeriesValues) - 1), // plotOrder
    $dataSeriesLabels, // plotLabel
    $xAxisTickValues, // plotCategory
    $dataSeriesValues,        // plotValues
    DataSeries::DIRECTION_COL
);

$plotArea = new PlotArea(null, [$series]);
// Set the chart legend
$legend = new Legend(Legend::POSITION_RIGHT, null, false);

$title = new Title('Test Chart');
$yAxisLabel = new Title('Value ($k1)');

// Create the chart
$chart = new Chart(
    'chart1', // name
    $title, // title
    $legend, // legend
    $plotArea, // plotArea
    true, // plotVisibleOnly
    0, // displayBlanksAs
    null, // xAxisLabel
    $yAxisLabel,  // yAxisLabel
    null,
    null
);

$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');

// Add the chart to the worksheet
$sheet1->addChart($chart);

$helper = new Sample();
$filename = $helper->getFilename(__FILE__);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$callStartTime = microtime(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);

现在我得到这样的图表:

在此处输入图像描述

但我需要这样:

在此处输入图像描述

你可以试试这里提到的补丁。

您需要在这两个文件中添加补丁并尝试执行上述链接中提到的操作。

PHPExcel\Chart\PlotArea.php

PHPExcel\Writer\Excel2007\Chart.php

我试过了,它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM