PHPOffice/PHPPresentation (PowerPoint) how to put a chart (createChartShape) inside a table cell?

363 views Asked by At

By following the docs I found how to draw a chart directly inside the PowerPoint slide like this:

$seriesData = array(
  'Monday' => 12,
  'Tuesday' => 15,
  'Wednesday' => 13,
);
$series = new \PhpOffice\PhpPresentation\Shape\Chart\Series('Downloads', $seriesData);
$lineChart = new \PhpOffice\PhpPresentation\Shape\Chart\Type\Line();
$lineChart->addSeries($series);
$shape = $currentSlide->createChartShape(); // etc.

And this works correctly.

But I have an issue with encapsulating this in a PHP method, and looping it, and inserting the result chart inside a table cell:

$shape = $currentSlide->createTableShape();
$shape->setHeight(800);
// ..etc.
foreach ($datapoints as $datapoint) {
    $row = $shape->createRow();
    $row->setHeight(100);
    $oCell = $row->nextCell();
    $oCell->setWidth(240);
    // This adds plain text in cell, it works:
    $datapointValue = "Some text";
    $oCell->createTextRun($datapointValue);
    ///////
    // Instead the plain text I need to somehow add a chart here, with a loop:
    // $datapointChart = $this->getMyChart($datapoint); // Chart should be encapsulated in a method like 'getMyChart'
    // $oCell->createTextRun($datapointChart);
    // .. but I have no idea what should 'getMyChart' return, and which method should I use instead the 'createTextRun' for the cell
    ///////
}

Basically I have no idea how to use this for a table cell (not directly for the slide):

$shape = $currentSlide->createChartShape();

And if I encapsulate the chart in a PHP method: what should this method return (that should be passed to the cell)?

0

There are 0 answers