Completed
Push — master ( bc0154...042bac )
by Mark
32s queued 28s
created

Chart::getPlotArea()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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