PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Chart / Chart.php

Chart.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Chart/Chart.php

901 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Chart;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Settings;
6 use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
7
8 class Chart
9 {
10 /**
11 * Chart Name.
12 */
13 private string $name;
14
15 /**
16 * Worksheet.
17 */
18 private ?Worksheet $worksheet = null;
19
20 /**
21 * Chart Title.
22 */
23 private ?Title $title;
24
25 /**
26 * Chart Legend.
27 */
28 private ?Legend $legend;
29
30 /**
31 * X-Axis Label.
32 */
33 private ?Title $xAxisLabel;
34
35 /**
36 * Y-Axis Label.
37 */
38 private ?Title $yAxisLabel;
39
40 /**
41 * Chart Plot Area.
42 */
43 private ?PlotArea $plotArea;
44
45 /**
46 * Plot Visible Only.
47 */
48 private bool $plotVisibleOnly;
49
50 /**
51 * Display Blanks as.
52 */
53 private string $displayBlanksAs;
54
55 /**
56 * Chart Asix Y as.
57 */
58 private Axis $yAxis;
59
60 /**
61 * Chart Asix X as.
62 */
63 private Axis $xAxis;
64
65 /**
66 * Top-Left Cell Position.
67 */
68 private string $topLeftCellRef = 'A1';
69
70 /**
71 * Top-Left X-Offset.
72 */
73 private int $topLeftXOffset = 0;
74
75 /**
76 * Top-Left Y-Offset.
77 */
78 private int $topLeftYOffset = 0;
79
80 /**
81 * Bottom-Right Cell Position.
82 */
83 private string $bottomRightCellRef = '';
84
85 /**
86 * Bottom-Right X-Offset.
87 */
88 private int $bottomRightXOffset = 10;
89
90 /**
91 * Bottom-Right Y-Offset.
92 */
93 private int $bottomRightYOffset = 10;
94
95 private ?int $rotX = null;
96
97 private ?int $rotY = null;
98
99 private ?int $rAngAx = null;
100
101 private ?int $perspective = null;
102
103 private bool $oneCellAnchor = false;
104
105 private bool $autoTitleDeleted = false;
106
107 private bool $noFill = false;
108
109 private bool $noBorder = false;
110
111 private bool $roundedCorners = false;
112
113 private bool $date1904 = false;
114
115 private string $lang = 'en-GB';
116
117 /** @var array{
118 * b?: numeric-string,
119 * l?: numeric-string,
120 * r?: numeric-string,
121 * t?: numeric-string,
122 * header?: numeric-string,
123 * footer?: numeric-string,
124 * }
125 */
126 private array $pageMargins = [
127 'b' => '0.75',
128 'l' => '0.7',
129 'r' => '0.7',
130 't' => '0.75',
131 'header' => '0.3',
132 'footer' => '0.3',
133 ];
134
135 /** @var array{
136 * paperSize?: string,
137 * orientation?: string,
138 * }
139 */
140 private array $pageSetup = [
141 'orientation' => 'portrait',
142 ];
143
144 private GridLines $borderLines;
145
146 private ChartColor $fillColor;
147
148 /**
149 * Rendered width in pixels.
150 */
151 private ?float $renderedWidth = null;
152
153 /**
154 * Rendered height in pixels.
155 */
156 private ?float $renderedHeight = null;
157
158 /**
159 * Create a new Chart.
160 * majorGridlines and minorGridlines are deprecated, moved to Axis.
161 */
162 public function __construct(string $name, ?Title $title = null, ?Legend $legend = null, ?PlotArea $plotArea = null, bool $plotVisibleOnly = true, string $displayBlanksAs = DataSeries::DEFAULT_EMPTY_AS, ?Title $xAxisLabel = null, ?Title $yAxisLabel = null, ?Axis $xAxis = null, ?Axis $yAxis = null, ?GridLines $majorGridlines = null, ?GridLines $minorGridlines = null)
163 {
164 $this->name = $name;
165 $this->title = $title;
166 $this->legend = $legend;
167 $this->xAxisLabel = $xAxisLabel;
168 $this->yAxisLabel = $yAxisLabel;
169 $this->plotArea = $plotArea;
170 $this->plotVisibleOnly = $plotVisibleOnly;
171 $this->setDisplayBlanksAs($displayBlanksAs);
172 $this->xAxis = $xAxis ?? new Axis();
173 $this->yAxis = $yAxis ?? new Axis();
174 if ($majorGridlines !== null) {
175 $this->yAxis->setMajorGridlines($majorGridlines);
176 }
177 if ($minorGridlines !== null) {
178 $this->yAxis->setMinorGridlines($minorGridlines);
179 }
180 $this->fillColor = new ChartColor();
181 $this->borderLines = new GridLines();
182 }
183
184 public function __destruct()
185 {
186 $this->worksheet = null;
187 }
188
189 /**
190 * Get Name.
191 */
192 public function getName(): string
193 {
194 return $this->name;
195 }
196
197 public function setName(string $name): self
198 {
199 $this->name = $name;
200
201 return $this;
202 }
203
204 /**
205 * Get Worksheet.
206 */
207 public function getWorksheet(): ?Worksheet
208 {
209 return $this->worksheet;
210 }
211
212 /**
213 * Set Worksheet.
214 *
215 * @return $this
216 */
217 public function setWorksheet(?Worksheet $worksheet = null)
218 {
219 $this->worksheet = $worksheet;
220
221 return $this;
222 }
223
224 public function getTitle(): ?Title
225 {
226 return $this->title;
227 }
228
229 /**
230 * Set Title.
231 *
232 * @return $this
233 */
234 public function setTitle(Title $title)
235 {
236 $this->title = $title;
237
238 return $this;
239 }
240
241 public function getLegend(): ?Legend
242 {
243 return $this->legend;
244 }
245
246 /**
247 * Set Legend.
248 *
249 * @return $this
250 */
251 public function setLegend(Legend $legend)
252 {
253 $this->legend = $legend;
254
255 return $this;
256 }
257
258 public function getXAxisLabel(): ?Title
259 {
260 return $this->xAxisLabel;
261 }
262
263 /**
264 * Set X-Axis Label.
265 *
266 * @return $this
267 */
268 public function setXAxisLabel(Title $label)
269 {
270 $this->xAxisLabel = $label;
271
272 return $this;
273 }
274
275 public function getYAxisLabel(): ?Title
276 {
277 return $this->yAxisLabel;
278 }
279
280 /**
281 * Set Y-Axis Label.
282 *
283 * @return $this
284 */
285 public function setYAxisLabel(Title $label)
286 {
287 $this->yAxisLabel = $label;
288
289 return $this;
290 }
291
292 public function getPlotArea(): ?PlotArea
293 {
294 return $this->plotArea;
295 }
296
297 public function getPlotAreaOrThrow(): PlotArea
298 {
299 $plotArea = $this->getPlotArea();
300 if ($plotArea !== null) {
301 return $plotArea;
302 }
303
304 throw new Exception('Chart has no PlotArea');
305 }
306
307 /**
308 * Set Plot Area.
309 */
310 public function setPlotArea(PlotArea $plotArea): self
311 {
312 $this->plotArea = $plotArea;
313
314 return $this;
315 }
316
317 /**
318 * Get Plot Visible Only.
319 */
320 public function getPlotVisibleOnly(): bool
321 {
322 return $this->plotVisibleOnly;
323 }
324
325 /**
326 * Set Plot Visible Only.
327 *
328 * @return $this
329 */
330 public function setPlotVisibleOnly(bool $plotVisibleOnly)
331 {
332 $this->plotVisibleOnly = $plotVisibleOnly;
333
334 return $this;
335 }
336
337 /**
338 * Get Display Blanks as.
339 */
340 public function getDisplayBlanksAs(): string
341 {
342 return $this->displayBlanksAs;
343 }
344
345 /**
346 * Set Display Blanks as.
347 *
348 * @return $this
349 */
350 public function setDisplayBlanksAs(string $displayBlanksAs)
351 {
352 $displayBlanksAs = strtolower($displayBlanksAs);
353 $this->displayBlanksAs = in_array($displayBlanksAs, DataSeries::VALID_EMPTY_AS, true) ? $displayBlanksAs : DataSeries::DEFAULT_EMPTY_AS;
354
355 return $this;
356 }
357
358 public function getChartAxisY(): Axis
359 {
360 return $this->yAxis;
361 }
362
363 /**
364 * Set yAxis.
365 */
366 public function setChartAxisY(?Axis $axis): self
367 {
368 $this->yAxis = $axis ?? new Axis();
369
370 return $this;
371 }
372
373 public function getChartAxisX(): Axis
374 {
375 return $this->xAxis;
376 }
377
378 /**
379 * Set xAxis.
380 */
381 public function setChartAxisX(?Axis $axis): self
382 {
383 $this->xAxis = $axis ?? new Axis();
384
385 return $this;
386 }
387
388 /**
389 * Set the Top Left position for the chart.
390 *
391 * @return $this
392 */
393 public function setTopLeftPosition(string $cellAddress, ?int $xOffset = null, ?int $yOffset = null)
394 {
395 $this->topLeftCellRef = $cellAddress;
396 if ($xOffset !== null) {
397 $this->setTopLeftXOffset($xOffset);
398 }
399 if ($yOffset !== null) {
400 $this->setTopLeftYOffset($yOffset);
401 }
402
403 return $this;
404 }
405
406 /**
407 * Get the top left position of the chart.
408 *
409 * Returns ['cell' => string cell address, 'xOffset' => int, 'yOffset' => int].
410 *
411 * @return array{cell: string, xOffset: int, yOffset: int} an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
412 */
413 public function getTopLeftPosition(): array
414 {
415 return [
416 'cell' => $this->topLeftCellRef,
417 'xOffset' => $this->topLeftXOffset,
418 'yOffset' => $this->topLeftYOffset,
419 ];
420 }
421
422 /**
423 * Get the cell address where the top left of the chart is fixed.
424 */
425 public function getTopLeftCell(): string
426 {
427 return $this->topLeftCellRef;
428 }
429
430 /**
431 * Set the Top Left cell position for the chart.
432 *
433 * @return $this
434 */
435 public function setTopLeftCell(string $cellAddress)
436 {
437 $this->topLeftCellRef = $cellAddress;
438
439 return $this;
440 }
441
442 /**
443 * Set the offset position within the Top Left cell for the chart.
444 *
445 * @return $this
446 */
447 public function setTopLeftOffset(?int $xOffset, ?int $yOffset)
448 {
449 if ($xOffset !== null) {
450 $this->setTopLeftXOffset($xOffset);
451 }
452
453 if ($yOffset !== null) {
454 $this->setTopLeftYOffset($yOffset);
455 }
456
457 return $this;
458 }
459
460 /**
461 * Get the offset position within the Top Left cell for the chart.
462 *
463 * @return int[]
464 */
465 public function getTopLeftOffset(): array
466 {
467 return [
468 'X' => $this->topLeftXOffset,
469 'Y' => $this->topLeftYOffset,
470 ];
471 }
472
473 /**
474 * @return $this
475 */
476 public function setTopLeftXOffset(int $xOffset)
477 {
478 $this->topLeftXOffset = $xOffset;
479
480 return $this;
481 }
482
483 public function getTopLeftXOffset(): int
484 {
485 return $this->topLeftXOffset;
486 }
487
488 /**
489 * @return $this
490 */
491 public function setTopLeftYOffset(int $yOffset)
492 {
493 $this->topLeftYOffset = $yOffset;
494
495 return $this;
496 }
497
498 public function getTopLeftYOffset(): int
499 {
500 return $this->topLeftYOffset;
501 }
502
503 /**
504 * Set the Bottom Right position of the chart.
505 *
506 * @return $this
507 */
508 public function setBottomRightPosition(string $cellAddress = '', ?int $xOffset = null, ?int $yOffset = null)
509 {
510 $this->bottomRightCellRef = $cellAddress;
511 if ($xOffset !== null) {
512 $this->setBottomRightXOffset($xOffset);
513 }
514 if ($yOffset !== null) {
515 $this->setBottomRightYOffset($yOffset);
516 }
517
518 return $this;
519 }
520
521 /**
522 * Get the bottom right position of the chart.
523 *
524 * @return array{cell: string, xOffset: int, yOffset:int} an associative array containing the cell address, X-Offset and Y-Offset from the top left of that cell
525 */
526 public function getBottomRightPosition(): array
527 {
528 return [
529 'cell' => $this->bottomRightCellRef,
530 'xOffset' => $this->bottomRightXOffset,
531 'yOffset' => $this->bottomRightYOffset,
532 ];
533 }
534
535 /**
536 * Set the Bottom Right cell for the chart.
537 *
538 * @return $this
539 */
540 public function setBottomRightCell(string $cellAddress = '')
541 {
542 $this->bottomRightCellRef = $cellAddress;
543
544 return $this;
545 }
546
547 /**
548 * Get the cell address where the bottom right of the chart is fixed.
549 */
550 public function getBottomRightCell(): string
551 {
552 return $this->bottomRightCellRef;
553 }
554
555 /**
556 * Set the offset position within the Bottom Right cell for the chart.
557 *
558 * @return $this
559 */
560 public function setBottomRightOffset(?int $xOffset, ?int $yOffset)
561 {
562 if ($xOffset !== null) {
563 $this->setBottomRightXOffset($xOffset);
564 }
565
566 if ($yOffset !== null) {
567 $this->setBottomRightYOffset($yOffset);
568 }
569
570 return $this;
571 }
572
573 /**
574 * Get the offset position within the Bottom Right cell for the chart.
575 *
576 * @return int[]
577 */
578 public function getBottomRightOffset(): array
579 {
580 return [
581 'X' => $this->bottomRightXOffset,
582 'Y' => $this->bottomRightYOffset,
583 ];
584 }
585
586 /**
587 * @return $this
588 */
589 public function setBottomRightXOffset(int $xOffset)
590 {
591 $this->bottomRightXOffset = $xOffset;
592
593 return $this;
594 }
595
596 public function getBottomRightXOffset(): int
597 {
598 return $this->bottomRightXOffset;
599 }
600
601 /**
602 * @return $this
603 */
604 public function setBottomRightYOffset(int $yOffset)
605 {
606 $this->bottomRightYOffset = $yOffset;
607
608 return $this;
609 }
610
611 public function getBottomRightYOffset(): int
612 {
613 return $this->bottomRightYOffset;
614 }
615
616 public function refresh(): void
617 {
618 if ($this->worksheet !== null && $this->plotArea !== null) {
619 $this->plotArea->refresh($this->worksheet);
620 }
621 }
622
623 /**
624 * Render the chart to given file (or stream).
625 *
626 * @param ?string $outputDestination Name of the file render to
627 *
628 * @return bool true on success
629 */
630 public function render(?string $outputDestination = null): bool
631 {
632 if ($outputDestination == 'php://output') {
633 $outputDestination = null;
634 }
635
636 $libraryName = Settings::getChartRenderer();
637 if ($libraryName === null) {
638 return false;
639 }
640
641 // Ensure that data series values are up-to-date before we render
642 $this->refresh();
643
644 $renderer = new $libraryName($this);
645
646 return $renderer->render($outputDestination);
647 }
648
649 public function getRotX(): ?int
650 {
651 return $this->rotX;
652 }
653
654 public function setRotX(?int $rotX): self
655 {
656 $this->rotX = $rotX;
657
658 return $this;
659 }
660
661 public function getRotY(): ?int
662 {
663 return $this->rotY;
664 }
665
666 public function setRotY(?int $rotY): self
667 {
668 $this->rotY = $rotY;
669
670 return $this;
671 }
672
673 public function getRAngAx(): ?int
674 {
675 return $this->rAngAx;
676 }
677
678 public function setRAngAx(?int $rAngAx): self
679 {
680 $this->rAngAx = $rAngAx;
681
682 return $this;
683 }
684
685 public function getPerspective(): ?int
686 {
687 return $this->perspective;
688 }
689
690 public function setPerspective(?int $perspective): self
691 {
692 $this->perspective = $perspective;
693
694 return $this;
695 }
696
697 public function getOneCellAnchor(): bool
698 {
699 return $this->oneCellAnchor;
700 }
701
702 public function setOneCellAnchor(bool $oneCellAnchor): self
703 {
704 $this->oneCellAnchor = $oneCellAnchor;
705
706 return $this;
707 }
708
709 public function getAutoTitleDeleted(): bool
710 {
711 return $this->autoTitleDeleted;
712 }
713
714 public function setAutoTitleDeleted(?bool $autoTitleDeleted): self
715 {
716 if (is_bool($autoTitleDeleted)) {
717 $this->autoTitleDeleted = $autoTitleDeleted;
718 }
719
720 return $this;
721 }
722
723 public function getNoFill(): bool
724 {
725 return $this->noFill;
726 }
727
728 public function setNoFill(bool $noFill): self
729 {
730 $this->noFill = $noFill;
731
732 return $this;
733 }
734
735 public function getNoBorder(): bool
736 {
737 return $this->noBorder;
738 }
739
740 public function setNoBorder(bool $noBorder): self
741 {
742 $this->noBorder = $noBorder;
743
744 return $this;
745 }
746
747 public function getRoundedCorners(): bool
748 {
749 return $this->roundedCorners;
750 }
751
752 public function setRoundedCorners(?bool $roundedCorners): self
753 {
754 if ($roundedCorners !== null) {
755 $this->roundedCorners = $roundedCorners;
756 }
757
758 return $this;
759 }
760
761 public function getDate1904(): bool
762 {
763 return $this->date1904;
764 }
765
766 public function setDate1904(?bool $date1904): self
767 {
768 if ($date1904 !== null) {
769 $this->date1904 = $date1904;
770 }
771
772 return $this;
773 }
774
775 public function getLang(): string
776 {
777 return $this->lang;
778 }
779
780 public function setLang(?string $lang): self
781 {
782 if ($lang !== null && $lang !== '') {
783 $this->lang = $lang;
784 }
785
786 return $this;
787 }
788
789 public function getBorderLines(): GridLines
790 {
791 return $this->borderLines;
792 }
793
794 public function setBorderLines(GridLines $borderLines): self
795 {
796 $this->borderLines = $borderLines;
797
798 return $this;
799 }
800
801 public function getFillColor(): ChartColor
802 {
803 return $this->fillColor;
804 }
805
806 public function setRenderedWidth(?float $width): self
807 {
808 $this->renderedWidth = $width;
809
810 return $this;
811 }
812
813 public function getRenderedWidth(): ?float
814 {
815 return $this->renderedWidth;
816 }
817
818 public function setRenderedHeight(?float $height): self
819 {
820 $this->renderedHeight = $height;
821
822 return $this;
823 }
824
825 public function getRenderedHeight(): ?float
826 {
827 return $this->renderedHeight;
828 }
829
830 /**
831 * Implement PHP __clone to create a deep clone, not just a shallow copy.
832 */
833 public function __clone()
834 {
835 $this->worksheet = null;
836 $this->title = ($this->title === null) ? null : clone $this->title;
837 $this->legend = ($this->legend === null) ? null : clone $this->legend;
838 $this->xAxisLabel = ($this->xAxisLabel === null) ? null : clone $this->xAxisLabel;
839 $this->yAxisLabel = ($this->yAxisLabel === null) ? null : clone $this->yAxisLabel;
840 $this->plotArea = ($this->plotArea === null) ? null : clone $this->plotArea;
841 $this->xAxis = clone $this->xAxis;
842 $this->yAxis = clone $this->yAxis;
843 $this->borderLines = clone $this->borderLines;
844 $this->fillColor = clone $this->fillColor;
845 }
846
847 /** @return array{
848 * b?: numeric-string,
849 * l?: numeric-string,
850 * r?: numeric-string,
851 * t?: numeric-string,
852 * header?: numeric-string,
853 * footer?: numeric-string,
854 * }
855 */
856 public function getPageMargins(): array
857 {
858 return $this->pageMargins;
859 }
860
861 /** @param mixed $pageMargins expecting array matching $this->pageMargins */
862 public function setPageMargins($pageMargins): self
863 {
864 if (is_array($pageMargins)) {
865 foreach (['b', 'l', 'r', 't', 'header', 'footer'] as $key) {
866 $value = $pageMargins[$key] ?? null;
867 if (is_string($value) && is_numeric($value)) {
868 $this->pageMargins[$key] = "$value";
869 }
870 }
871 }
872
873 return $this;
874 }
875
876 /** @return array{
877 * paperSize?: string,
878 * orientation?: string,
879 * }
880 */
881 public function getPageSetup(): array
882 {
883 return $this->pageSetup;
884 }
885
886 /** @param mixed $pageSetup expecting array matching $this->pageSetup */
887 public function setPageSetup($pageSetup): self
888 {
889 if (is_array($pageSetup)) {
890 foreach (['paperSize', 'orientation'] as $key) {
891 $value = $pageSetup[$key] ?? null;
892 if (is_string($value)) {
893 $this->pageSetup[$key] = "$value";
894 }
895 }
896 }
897
898 return $this;
899 }
900 }
901