简体   繁体   中英

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. I already can add Legend, but it seems that Data Table can be added too.

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\Writer\Excel2007\Chart.php

I have tried and it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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