简体   繁体   English

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

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

I'm trying to make a chart through PhpOffice and got some difficulties with adding the Data.table element above the chart.我试图通过 PhpOffice 制作图表,但在图表上方添加 Data.table 元素时遇到了一些困难。 I already can add Legend, but it seems that Data Table can be added too.我已经可以添加 Legend,但似乎也可以添加 Data Table。

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

Already read this thing but can't find the right way to make this.已经读过这个东西,但找不到正确的方法来做这个。 I'm using standard chart building example but edited it for my purposes我正在使用标准图表构建示例,但出于我的目的对其进行了编辑

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

Now I got chart like this:现在我得到这样的图表:

在此处输入图像描述

But I need to get like this:但我需要这样:

在此处输入图像描述

You can try the patch mentioned here .你可以试试这里提到的补丁。

You need to add patch in these two files and try doing what mentioned in the above link.您需要在这两个文件中添加补丁并尝试执行上述链接中提到的操作。

PHPExcel\Chart\PlotArea.php PHPExcel\Chart\PlotArea.php

PHPExcel\Writer\Excel2007\Chart.php PHPExcel\Writer\Excel2007\Chart.php

I have tried and it works for me.我试过了,它对我有用。

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

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