PHPWord/samples/Sample_41_TemplateSetChart.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2021-02-06 10:40:52 +01:00
<?php
2022-09-16 11:45:45 +02:00
2021-02-06 10:40:52 +01:00
use PhpOffice\PhpWord\Element\Chart;
use PhpOffice\PhpWord\Shared\Converter;
include_once 'Sample_Header.php';
// Template processor instance creation
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_41_TemplateSetChart.docx');
2022-09-16 11:45:45 +02:00
$chartTypes = ['pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column'];
$twoSeries = ['bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column'];
$threeSeries = ['bar', 'line'];
2021-02-06 10:40:52 +01:00
2022-09-16 11:45:45 +02:00
$categories = ['A', 'B', 'C', 'D', 'E'];
$series1 = [1, 3, 2, 5, 4];
$series2 = [3, 1, 7, 2, 6];
$series3 = [8, 3, 2, 5, 4];
2021-02-06 10:40:52 +01:00
2021-02-06 21:32:30 +01:00
$i = 0;
2021-02-06 10:40:52 +01:00
foreach ($chartTypes as $chartType) {
2021-02-06 21:32:30 +01:00
$chart = new Chart($chartType, $categories, $series1);
2021-02-06 10:40:52 +01:00
2021-02-06 21:32:30 +01:00
if (in_array($chartType, $twoSeries)) {
$chart->addSeries($categories, $series2);
}
if (in_array($chartType, $threeSeries)) {
$chart->addSeries($categories, $series3);
}
2021-02-06 10:40:52 +01:00
2021-02-06 21:32:30 +01:00
$chart->getStyle()
->setWidth(Converter::inchToEmu(3))
->setHeight(Converter::inchToEmu(3));
2021-02-06 10:40:52 +01:00
2021-02-06 21:32:30 +01:00
$templateProcessor->setChart("chart{$i}", $chart);
2022-09-16 11:45:45 +02:00
++$i;
2021-02-06 10:40:52 +01:00
}
echo date('H:i:s'), ' Saving the result document...', EOL;
$templateProcessor->saveAs('results/Sample_41_TemplateSetChart.docx');
2022-09-16 11:45:45 +02:00
echo getEndingNotes(['Word2007' => 'docx'], 'results/Sample_41_TemplateSetChart.docx');
2021-02-06 10:40:52 +01:00
if (!CLI) {
include_once 'Sample_Footer.php';
}