1

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)?

Vlado
  • 3,517
  • 2
  • 26
  • 24
  • 1
    A table cell is basically a shape. Shapes can contain text but not collections of other shapes (like your chart). You *can* set the fill of a table cell to a picture, though. You might be able to do what you want by exporting the chart as an image then setting that as the cell's fill. You'd have to be careful to make the chart (and hence its exported image) the same proportions as the cell you're filling; otherwise it will get distorted. – Steve Rindsberg Aug 01 '22 at 14:26
  • The problem is I need to embed native editable vector chart in each cell. Maybe I could size each cell with the size of the chart and place each chart with correct coordinates over each cell in a way that chart covers up the cell? Do you believe it's doable? – Vlado Aug 01 '22 at 15:55
  • 1
    Overlaying each cell with a chart is one way, though I suspect it'd be rather fragile. I suspect you might be better off building your own "fake" chart with rectangles and lines, then sizing the chart to the rectangles. – Steve Rindsberg Aug 02 '22 at 16:07
  • Thank you, I managed to produce a matrix with native charts overlaying each cell and it works good. – Vlado Aug 03 '22 at 10:03

0 Answers0