|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 4 | + * word processing documents. |
| 5 | + * |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the LICENSE |
| 10 | + * file that was distributed with this source code. For the full list of |
| 11 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 12 | + * |
| 13 | + * @see https://github.com/PHPOffice/PHPWord |
| 14 | + * @copyright 2010-2018 PHPWord contributors |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace PhpOffice\PhpWord\Writer\Word2007; |
| 19 | + |
| 20 | +use PhpOffice\PhpWord\PhpWord; |
| 21 | +use PhpOffice\PhpWord\Settings; |
| 22 | +use PhpOffice\PhpWord\TestHelperDOCX; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace |
| 26 | + */ |
| 27 | +class ChartTest extends \PHPUnit\Framework\TestCase |
| 28 | +{ |
| 29 | + private $outputEscapingEnabled; |
| 30 | + |
| 31 | + /** |
| 32 | + * Executed before each method of the class |
| 33 | + */ |
| 34 | + public function setUp() |
| 35 | + { |
| 36 | + $this->outputEscapingEnabled = Settings::isOutputEscapingEnabled(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Executed after each method of the class |
| 41 | + */ |
| 42 | + public function tearDown() |
| 43 | + { |
| 44 | + Settings::setOutputEscapingEnabled($this->outputEscapingEnabled); |
| 45 | + TestHelperDOCX::clear(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test chart elements |
| 50 | + */ |
| 51 | + public function testChartElements() |
| 52 | + { |
| 53 | + $phpWord = new PhpWord(); |
| 54 | + $section = $phpWord->addSection(); |
| 55 | + $style = array( |
| 56 | + 'width' => 5000000, |
| 57 | + 'height' => 5000000, |
| 58 | + 'showAxisLabels' => true, |
| 59 | + 'showGridX' => true, |
| 60 | + 'showGridY' => true, |
| 61 | + 'showLegend' => false, |
| 62 | + ); |
| 63 | + |
| 64 | + $chartTypes = array('pie', 'doughnut', 'bar', 'line', 'area', 'scatter', 'radar'); |
| 65 | + $categories = array('A', 'B', 'C', 'D', 'E'); |
| 66 | + $series1 = array(1, 3, 2, 5, 4); |
| 67 | + foreach ($chartTypes as $chartType) { |
| 68 | + $section->addChart($chartType, $categories, $series1, $style); |
| 69 | + } |
| 70 | + $colorArray = array('FFFFFF', '000000', 'FF0000', '00FF00', '0000FF'); |
| 71 | + $numColor = count($colorArray); |
| 72 | + $chart = $section->addChart('pie', $categories, $series1, $style); |
| 73 | + $chart->getStyle()->setColors($colorArray)->setTitle('3d chart')->set3d(true); |
| 74 | + $chart = $section->addChart('stacked_bar', $categories, $series1, $style); |
| 75 | + $chart->getStyle()->setColors($colorArray)->setShowLegend(true); |
| 76 | + $chart = $section->addChart('scatter', $categories, $series1, $style); |
| 77 | + $chart->getStyle()->setMajorTickPosition('cross'); |
| 78 | + $section->addChart('scatter', $categories, $series1, $style, 'seriesname'); |
| 79 | + |
| 80 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 81 | + |
| 82 | + $index = 0; |
| 83 | + foreach ($chartTypes as $chartType) { |
| 84 | + ++$index; |
| 85 | + $file = "word/charts/chart{$index}.xml"; |
| 86 | + $path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart"; |
| 87 | + self::assertTrue($doc->elementExists($path, $file), "chart type $chartType"); |
| 88 | + } |
| 89 | + |
| 90 | + $index = 11; |
| 91 | + $file = "word/charts/chart{$index}.xml"; |
| 92 | + $doc->setDefaultFile($file); |
| 93 | + $chartType = 'scatter'; |
| 94 | + $path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart"; |
| 95 | + self::assertEquals('seriesname', $doc->getElement($path . '/c:ser/c:tx/c:strRef/c:strCache/c:pt/c:v')->nodeValue); |
| 96 | + |
| 97 | + $index = 8; |
| 98 | + $file = "word/charts/chart{$index}.xml"; |
| 99 | + $doc->setDefaultFile($file); |
| 100 | + $chartType = 'pie3D'; |
| 101 | + $path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart"; |
| 102 | + for ($idx = 0; $idx < $numColor; ++$idx) { |
| 103 | + $idxp1 = $idx + 1; |
| 104 | + $element = $path . "/c:ser/c:dPt[$idxp1]/c:spPr/a:solidFill/a:srgbClr"; |
| 105 | + self::assertEquals($colorArray[$idx], $doc->getElementAttribute($element, 'val'), "pie3d chart idx=$idx"); |
| 106 | + } |
| 107 | + |
| 108 | + $index = 9; |
| 109 | + $file = "word/charts/chart{$index}.xml"; |
| 110 | + $doc->setDefaultFile($file); |
| 111 | + $chartType = 'bar'; |
| 112 | + $path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart"; |
| 113 | + for ($idxp1 = 1; $idxp1 < $numColor; ++$idxp1) { |
| 114 | + $idx = $idxp1; // stacked bar chart is shifted |
| 115 | + $element = $path . "/c:ser/c:dPt[$idxp1]/c:spPr/a:solidFill/a:srgbClr"; |
| 116 | + self::assertEquals($colorArray[$idx], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public function testChartEscapingEnabled() |
| 121 | + { |
| 122 | + Settings::setOutputEscapingEnabled(true); |
| 123 | + $phpWord = new PhpWord(); |
| 124 | + $section = $phpWord->addSection(); |
| 125 | + $style = array( |
| 126 | + 'width' => 5000000, |
| 127 | + 'height' => 5000000, |
| 128 | + 'showAxisLabels' => true, |
| 129 | + 'showGridX' => true, |
| 130 | + 'showGridY' => true, |
| 131 | + 'showLegend' => false, |
| 132 | + 'valueAxisTitle' => 'Values', |
| 133 | + ); |
| 134 | + $categories = array('A&B', 'C<D>', 'E', 'F', 'G'); |
| 135 | + $series1 = array(1, 3, 2, 5, 4); |
| 136 | + $section->addChart('bar', $categories, $series1, $style); |
| 137 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 138 | + |
| 139 | + $index = 1; |
| 140 | + $file = "word/charts/chart{$index}.xml"; |
| 141 | + $doc->setDefaultFile($file); |
| 142 | + $chartType = 'bar'; |
| 143 | + $path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart/c:ser/c:cat/c:strLit"; |
| 144 | + $element = "$path/c:pt[1]/c:v"; |
| 145 | + self::assertEquals('A&B', $doc->getElement($element)->nodeValue); |
| 146 | + $element = "$path/c:pt[2]/c:v"; |
| 147 | + self::assertEquals('C<D>', $doc->getElement($element)->nodeValue); |
| 148 | + } |
| 149 | + |
| 150 | + public function testChartEscapingDisabled() |
| 151 | + { |
| 152 | + Settings::setOutputEscapingEnabled(false); |
| 153 | + $phpWord = new PhpWord(); |
| 154 | + $section = $phpWord->addSection(); |
| 155 | + $style = array( |
| 156 | + 'width' => 5000000, |
| 157 | + 'height' => 5000000, |
| 158 | + 'showAxisLabels' => true, |
| 159 | + 'showGridX' => true, |
| 160 | + 'showGridY' => true, |
| 161 | + 'showLegend' => false, |
| 162 | + 'valueAxisTitle' => 'Values', |
| 163 | + ); |
| 164 | + $categories = array('A&B', 'C<D>', 'E', 'F', 'G'); |
| 165 | + $series1 = array(1, 3, 2, 5, 4); |
| 166 | + $section->addChart('bar', $categories, $series1, $style); |
| 167 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 168 | + |
| 169 | + $index = 1; |
| 170 | + $file = "word/charts/chart{$index}.xml"; |
| 171 | + $doc->setDefaultFile($file); |
| 172 | + $chartType = 'bar'; |
| 173 | + $path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart/c:ser/c:cat/c:strLit"; |
| 174 | + $element = "$path/c:pt[1]/c:v"; |
| 175 | + self::assertEquals('A&B', $doc->getElement($element)->nodeValue); |
| 176 | + $element = "$path/c:pt[2]/c:v"; |
| 177 | + self::assertEquals('C<D>', $doc->getElement($element)->nodeValue); |
| 178 | + } |
| 179 | + |
| 180 | + public function testValueAxisTitle() |
| 181 | + { |
| 182 | + $phpWord = new PhpWord(); |
| 183 | + $section = $phpWord->addSection(); |
| 184 | + $style = array( |
| 185 | + 'width' => 5000000, |
| 186 | + 'height' => 5000000, |
| 187 | + 'showAxisLabels' => true, |
| 188 | + 'showGridX' => true, |
| 189 | + 'showGridY' => true, |
| 190 | + 'showLegend' => false, |
| 191 | + 'valueAxisTitle' => 'Values', |
| 192 | + ); |
| 193 | + $chartType = 'line'; |
| 194 | + $categories = array('A', 'B', 'C', 'D', 'E'); |
| 195 | + $series1 = array(1, 3, 2, 5, 4); |
| 196 | + $section->addChart($chartType, $categories, $series1, $style); |
| 197 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 198 | + |
| 199 | + $index = 1; |
| 200 | + $file = "word/charts/chart{$index}.xml"; |
| 201 | + $doc->setDefaultFile($file); |
| 202 | + $chartType = 'line'; |
| 203 | + $path = '/c:chartSpace/c:chart/c:plotArea'; |
| 204 | + $element = "$path/c:{$chartType}Chart"; |
| 205 | + self::assertTrue($doc->elementExists($path)); |
| 206 | + $element = "$path/c:valAx"; |
| 207 | + self::assertTrue($doc->elementExists($element)); |
| 208 | + $element .= '/c:title/c:tx/c:rich/a:p/a:r/a:t'; |
| 209 | + self::assertEquals('Values', $doc->getElement($element)->nodeValue); |
| 210 | + } |
| 211 | + |
| 212 | + public function testNoAxisLabels() |
| 213 | + { |
| 214 | + $phpWord = new PhpWord(); |
| 215 | + $section = $phpWord->addSection(); |
| 216 | + $style = array( |
| 217 | + 'width' => 5000000, |
| 218 | + 'height' => 5000000, |
| 219 | + 'showAxisLabels' => false, |
| 220 | + 'showGridX' => true, |
| 221 | + 'showGridY' => true, |
| 222 | + 'showLegend' => false, |
| 223 | + 'valueAxisTitle' => 'Values', |
| 224 | + ); |
| 225 | + $chartType = 'line'; |
| 226 | + $categories = array('A', 'B', 'C', 'D', 'E'); |
| 227 | + $series1 = array(1, 3, 2, 5, 4); |
| 228 | + $section->addChart($chartType, $categories, $series1, $style); |
| 229 | + $doc = TestHelperDOCX::getDocument($phpWord); |
| 230 | + |
| 231 | + $index = 1; |
| 232 | + $file = "word/charts/chart{$index}.xml"; |
| 233 | + $doc->setDefaultFile($file); |
| 234 | + $chartType = 'line'; |
| 235 | + $path = '/c:chartSpace/c:chart/c:plotArea'; |
| 236 | + $element = "$path/c:{$chartType}Chart"; |
| 237 | + $element = "$path/c:valAx"; |
| 238 | + $element .= '/c:tickLblPos'; |
| 239 | + self::assertEquals('none', $doc->getElementAttribute($element, 'val')); |
| 240 | + } |
| 241 | +} |
0 commit comments