PHPWord/samples/Sample_32_Chart.php

81 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2014-06-10 07:11:32 +07:00
<?php
2022-09-16 11:45:45 +02:00
2014-06-10 07:11:32 +07:00
include_once 'Sample_Header.php';
use PhpOffice\PhpWord\Shared\Converter;
2014-06-10 07:11:32 +07:00
// New Word document
2015-01-01 20:41:42 +04:00
echo date('H:i:s'), ' Create new PhpWord object', EOL;
2014-06-10 07:11:32 +07:00
$phpWord = new \PhpOffice\PhpWord\PhpWord();
2016-06-04 20:06:37 +04:00
// Define styles
2022-09-16 11:45:45 +02:00
$phpWord->addTitleStyle(1, ['size' => 14, 'bold' => true], ['keepNext' => true, 'spaceBefore' => 240]);
$phpWord->addTitleStyle(2, ['size' => 14, 'bold' => true], ['keepNext' => true, 'spaceBefore' => 240]);
// 2D charts
$section = $phpWord->addSection();
2016-06-04 20:06:37 +04:00
$section->addTitle('2D charts', 1);
2022-09-16 11:45:45 +02:00
$section = $phpWord->addSection(['colsNum' => 2, 'breakType' => 'continuous']);
2014-06-10 07:11:32 +07:00
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'];
$categories = ['A', 'B', 'C', 'D', 'E'];
$series1 = [1, 3, 2, 5, 4];
$series2 = [3, 1, 7, 2, 6];
$series3 = [8, 3, 2, 5, 4];
$showGridLines = false;
$showAxisLabels = false;
2020-08-29 20:41:25 +08:00
$showLegend = true;
$legendPosition = 't';
// r = right, l = left, t = top, b = bottom, tr = top right
2014-06-10 07:11:32 +07:00
2014-06-12 18:15:00 +07:00
foreach ($chartTypes as $chartType) {
$section->addTitle(ucfirst($chartType), 2);
2014-06-12 18:15:00 +07:00
$chart = $section->addChart($chartType, $categories, $series1);
2015-01-01 20:41:42 +04:00
$chart->getStyle()->setWidth(Converter::inchToEmu(2.5))->setHeight(Converter::inchToEmu(2));
$chart->getStyle()->setShowGridX($showGridLines);
$chart->getStyle()->setShowGridY($showGridLines);
$chart->getStyle()->setShowAxisLabels($showAxisLabels);
2020-08-29 20:41:25 +08:00
$chart->getStyle()->setShowLegend($showLegend);
$chart->getStyle()->setLegendPosition($legendPosition);
2014-06-12 18:15:00 +07:00
if (in_array($chartType, $twoSeries)) {
$chart->addSeries($categories, $series2);
}
if (in_array($chartType, $threeSeries)) {
$chart->addSeries($categories, $series3);
}
2014-06-10 07:11:32 +07:00
$section->addTextBreak();
}
// 3D charts
2022-09-16 11:45:45 +02:00
$section = $phpWord->addSection(['breakType' => 'continuous']);
2016-06-04 20:06:37 +04:00
$section->addTitle('3D charts', 1);
2022-09-16 11:45:45 +02:00
$section = $phpWord->addSection(['colsNum' => 2, 'breakType' => 'continuous']);
2022-09-16 11:45:45 +02:00
$chartTypes = ['pie', 'bar', 'column', 'line', 'area'];
$multiSeries = ['bar', 'column', 'line', 'area'];
$style = [
'width' => Converter::cmToEmu(5),
'height' => Converter::cmToEmu(4),
'3d' => true,
2018-03-21 21:58:41 +01:00
'showAxisLabels' => $showAxisLabels,
2022-09-16 11:45:45 +02:00
'showGridX' => $showGridLines,
'showGridY' => $showGridLines,
];
foreach ($chartTypes as $chartType) {
$section->addTitle(ucfirst($chartType), 2);
$chart = $section->addChart($chartType, $categories, $series1, $style);
if (in_array($chartType, $multiSeries)) {
$chart->addSeries($categories, $series2);
$chart->addSeries($categories, $series3);
}
$section->addTextBreak();
}
2014-06-10 07:11:32 +07:00
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}