Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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