-
Notifications
You must be signed in to change notification settings - Fork 58
/
Graph.php
2926 lines (2591 loc) · 108 KB
/
Graph.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* JPGraph v3.6.15
*/
namespace Amenadiel\JpGraph\Graph;
require_once __DIR__ . '/../config.inc.php';
use Amenadiel\JpGraph\Image;
use Amenadiel\JpGraph\Plot;
use Amenadiel\JpGraph\Text;
use Amenadiel\JpGraph\Util;
/**
* @class Graph
* // Description: Main class to handle graphs
*/
class Graph
{
public $gDateLocale;
public $gJpgDateLocale;
public $cache; // Cache object (singleton)
public $img; // Img object (singleton)
public $plots = []; // Array of all plot object in the graph (for Y 1 axis)
public $y2plots = []; // Array of all plot object in the graph (for Y 2 axis)
public $ynplots = [];
public $xscale; // X Scale object (could be instance of LinearScale or LogScale
public $yscale;
public $y2scale;
public $ynscale = [];
public $iIcons = []; // Array of Icons to add to
public $cache_name; // File name to be used for the current graph in the cache directory
public $xgrid; // X Grid object (linear or logarithmic)
public $ygrid;
public $y2grid; //dito for Y
public $doframe;
public $frame_color;
public $frame_weight; // Frame around graph
public $boxed = false;
public $box_color = 'black';
public $box_weight = 1; // Box around plot area
public $doshadow = false;
public $shadow_width = 4;
public $shadow_color = 'gray@0.5'; // Shadow for graph
public $xaxis; // X-axis (instane of Axis class)
public $yaxis;
public $y2axis;
public $ynaxis = []; // Y axis (instance of Axis class)
public $margin_color; // Margin color of graph
public $plotarea_color = [255, 255, 255]; // Plot area color
public $title;
public $subtitle;
public $subsubtitle; // Title and subtitle(s) text object
public $axtype = 'linlin'; // Type of axis
public $xtick_factor;
public $ytick_factor; // Factor to determine the maximum number of ticks depending on the plot width
public $texts;
public $y2texts; // Text object to ge shown in the graph
public $lines;
public $y2lines;
public $bands;
public $y2bands;
public $text_scale_off = 0;
public $text_scale_abscenteroff = -1; // Text scale in fractions and for centering bars
public $background_image = '';
public $background_image_type = -1;
public $background_image_format = 'png';
public $background_image_bright = 0;
public $background_image_contr = 0;
public $background_image_sat = 0;
public $background_image_xpos = 0;
public $background_image_ypos = 0;
public $image_bright = 0;
public $image_contr = 0;
public $image_sat = 0;
public $inline;
public $showcsim = 0;
public $csimcolor = 'red'; //debug stuff, draw the csim boundaris on the image if <>0
public $grid_depth = DEPTH_BACK; // Draw grid under all plots as default
public $iAxisStyle = AXSTYLE_SIMPLE;
public $iCSIMdisplay = false;
public $iHasStroked = false;
public $footer;
public $csimcachename = '';
public $csimcachetimeout = 0;
public $iCSIMImgAlt = '';
public $iDoClipping = false;
public $y2orderback = true;
public $tabtitle;
public $bkg_gradtype = -1;
public $bkg_gradstyle = BGRAD_MARGIN;
public $bkg_gradfrom = 'navy';
public $bkg_gradto = 'silver';
public $plot_gradtype = -1;
public $plot_gradstyle = BGRAD_MARGIN;
public $plot_gradfrom = 'silver';
public $plot_gradto = 'navy';
public $titlebackground = false;
public $titlebackground_color = 'lightblue';
public $titlebackground_style = 1;
public $titlebackground_framecolor;
public $titlebackground_framestyle;
public $titlebackground_frameweight;
public $titlebackground_bevelheight;
public $titlebkg_fillstyle = TITLEBKG_FILLSTYLE_SOLID;
public $titlebkg_scolor1 = 'black';
public $titlebkg_scolor2 = 'white';
public $framebevel;
public $framebeveldepth;
public $framebevelborder;
public $framebevelbordercolor;
public $framebevelcolor1;
public $framebevelcolor2;
public $background_image_mix = 100;
public $background_cflag = '';
public $background_cflag_type = BGIMG_FILLPLOT;
public $background_cflag_mix = 100;
public $iImgTrans = false;
public $iImgTransHorizon = 100;
public $iImgTransSkewDist = 150;
public $iImgTransDirection = 1;
public $iImgTransMinSize = true;
public $iImgTransFillColor = 'white';
public $iImgTransHighQ = false;
public $iImgTransBorder = false;
public $iImgTransHorizonPos = 0.5;
public $legend;
public $graph_theme;
protected $iYAxisDeltaPos = 50;
protected $iIconDepth = DEPTH_BACK;
protected $iAxisLblBgType = 0;
protected $iXAxisLblBgFillColor = 'lightgray';
protected $iXAxisLblBgColor = 'black';
protected $iYAxisLblBgFillColor = 'lightgray';
protected $iYAxisLblBgColor = 'black';
protected $iTables;
protected $isRunningClear = false;
protected $inputValues;
protected $isAfterSetScale = false;
// aWIdth Width in pixels of image
// aHeight Height in pixels of image
// aCachedName Name for image file in cache directory
// aTimeOut Timeout in minutes for image in cache
// aInline If true the image is streamed back in the call to Stroke()
// If false the image is just created in the cache
public function __construct($aWidth = 300, $aHeight = 200, $aCachedName = '', $aTimeout = 0, $aInline = true)
{
$this->gDateLocale = new Util\DateLocale();
$this->gJpgDateLocale = new Util\DateLocale();
if (!is_numeric($aWidth) || !is_numeric($aHeight)) {
Util\JpGraphError::RaiseL(25008); //('Image width/height argument in Graph::Graph() must be numeric');
}
// Initialize frame and margin
$this->InitializeFrameAndMargin();
// Automatically generate the image file name based on the name of the script that
// generates the graph
if ($aCachedName == 'auto') {
$aCachedName = Util\Helper::GenImgName();
}
// Should the image be streamed back to the browser or only to the cache?
$this->inline = $aInline;
$this->img = new Image\RotImage($aWidth, $aHeight);
$this->cache = new Image\ImgStreamCache();
// Window doesn't like '?' in the file name so replace it with an '_'
$aCachedName = str_replace('?', '_', $aCachedName);
$this->SetupCache($aCachedName, $aTimeout);
$this->title = new Text\Text();
$this->title->ParagraphAlign('center');
$this->title->SetFont(FF_DEFAULT, FS_NORMAL); //FF_FONT2, FS_BOLD
$this->title->SetMargin(5);
$this->title->SetAlign('center');
$this->subtitle = new Text\Text();
$this->subtitle->ParagraphAlign('center');
$this->subtitle->SetMargin(3);
$this->subtitle->SetAlign('center');
$this->subsubtitle = new Text\Text();
$this->subsubtitle->ParagraphAlign('center');
$this->subsubtitle->SetMargin(3);
$this->subsubtitle->SetAlign('center');
$this->legend = new Legend();
$this->footer = new Image\Footer();
// If the cached version exist just read it directly from the
// cache, stream it back to browser and exit
if ($aCachedName != '' && READ_CACHE && $aInline) {
if ($this->cache->GetAndStream($this->img, $aCachedName)) {
exit();
}
}
$this->SetTickDensity(); // Normal density
$this->tabtitle = new Text\GraphTabTitle();
if (!$this->isRunningClear) {
$this->inputValues = [];
$this->inputValues['aWidth'] = $aWidth;
$this->inputValues['aHeight'] = $aHeight;
$this->inputValues['aCachedName'] = $aCachedName;
$this->inputValues['aTimeout'] = $aTimeout;
$this->inputValues['aInline'] = $aInline;
$theme_class = '\Amenadiel\JpGraph\Themes\\' . DEFAULT_THEME_CLASS;
if (class_exists($theme_class)) {
$this->graph_theme = new $theme_class();
}
}
}
public function InitializeFrameAndMargin()
{
$this->doframe = true;
$this->frame_color = 'black';
$this->frame_weight = 1;
$this->titlebackground_framecolor = 'blue';
$this->titlebackground_framestyle = 2;
$this->titlebackground_frameweight = 1;
$this->titlebackground_bevelheight = 3;
$this->titlebkg_fillstyle = TITLEBKG_FILLSTYLE_SOLID;
$this->titlebkg_scolor1 = 'black';
$this->titlebkg_scolor2 = 'white';
$this->framebevel = false;
$this->framebeveldepth = 2;
$this->framebevelborder = false;
$this->framebevelbordercolor = 'black';
$this->framebevelcolor1 = 'white@0.4';
$this->framebevelcolor2 = 'black@0.4';
$this->margin_color = [250, 250, 250];
}
public function SetupCache($aFilename, $aTimeout = 60)
{
$this->cache_name = $aFilename;
$this->cache->SetTimeOut($aTimeout);
}
// Enable final image perspective transformation
public function Set3DPerspective($aDir = 1, $aHorizon = 100, $aSkewDist = 120, $aQuality = false, $aFillColor = '#FFFFFF', $aBorder = false, $aMinSize = true, $aHorizonPos = 0.5)
{
$this->iImgTrans = true;
$this->iImgTransHorizon = $aHorizon;
$this->iImgTransSkewDist = $aSkewDist;
$this->iImgTransDirection = $aDir;
$this->iImgTransMinSize = $aMinSize;
$this->iImgTransFillColor = $aFillColor;
$this->iImgTransHighQ = $aQuality;
$this->iImgTransBorder = $aBorder;
$this->iImgTransHorizonPos = $aHorizonPos;
}
public function SetUserFont($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
{
$this->img->ttf->SetUserFont($aNormal, $aBold, $aItalic, $aBoldIt);
}
public function SetUserFont1($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
{
$this->img->ttf->SetUserFont1($aNormal, $aBold, $aItalic, $aBoldIt);
}
public function SetUserFont2($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
{
$this->img->ttf->SetUserFont2($aNormal, $aBold, $aItalic, $aBoldIt);
}
public function SetUserFont3($aNormal, $aBold = '', $aItalic = '', $aBoldIt = '')
{
$this->img->ttf->SetUserFont3($aNormal, $aBold, $aItalic, $aBoldIt);
}
// Set Image format and optional quality
public function SetImgFormat($aFormat, $aQuality = 75)
{
$this->img->SetImgFormat($aFormat, $aQuality);
}
// Should the grid be in front or back of the plot?
public function SetGridDepth($aDepth)
{
$this->grid_depth = $aDepth;
}
public function SetIconDepth($aDepth)
{
$this->iIconDepth = $aDepth;
}
// Specify graph angle 0-360 degrees.
public function SetAngle($aAngle)
{
$this->img->SetAngle($aAngle);
}
public function SetAlphaBlending($aFlg = true)
{
$this->img->SetAlphaBlending($aFlg);
}
// Shortcut to image margin
public function SetMargin($lm, $rm, $tm, $bm)
{
$this->img->SetMargin($lm, $rm, $tm, $bm);
}
public function SetY2OrderBack($aBack = true)
{
$this->y2orderback = $aBack;
}
// Rotate the graph 90 degrees and set the margin
// when we have done a 90 degree rotation
public function Set90AndMargin($lm = 0, $rm = 0, $tm = 0, $bm = 0)
{
$lm = $lm == 0 ? floor(0.2 * $this->img->width) : $lm;
$rm = $rm == 0 ? floor(0.1 * $this->img->width) : $rm;
$tm = $tm == 0 ? floor(0.2 * $this->img->height) : $tm;
$bm = $bm == 0 ? floor(0.1 * $this->img->height) : $bm;
$adj = ($this->img->height - $this->img->width) / 2;
$this->img->SetMargin($tm - $adj, $bm - $adj, $rm + $adj, $lm + $adj);
$this->img->SetCenter(floor($this->img->width / 2), floor($this->img->height / 2));
$this->SetAngle(90);
if (empty($this->yaxis) || empty($this->xaxis)) {
Util\JpGraphError::RaiseL(25009); //('You must specify what scale to use with a call to Graph::SetScale()');
}
$this->xaxis->SetLabelAlign('right', 'center');
$this->yaxis->SetLabelAlign('center', 'bottom');
}
public function SetClipping($aFlg = true)
{
$this->iDoClipping = $aFlg;
}
// Add a plot object to the graph
public function Add($aPlot)
{
if ($aPlot == null) {
Util\JpGraphError::RaiseL(25010); //("Graph::Add() You tried to add a null plot to the graph.");
}
if (is_array($aPlot) && count($aPlot) > 0) {
$cl = $aPlot[0];
} else {
$cl = $aPlot;
}
if ($cl instanceof Text\Text) {
$this->AddText($aPlot);
} elseif (($cl instanceof Plot\PlotLine)) {
$this->AddLine($aPlot);
} elseif (($cl instanceof Plot\PlotBand)) {
$this->AddBand($aPlot);
} elseif (($cl instanceof Plot\IconPlot)) {
$this->AddIcon($aPlot);
} elseif (($cl instanceof Text\GTextTable)) {
$this->AddTable($aPlot);
} else {
if (is_array($aPlot)) {
$this->plots = array_merge($this->plots, $aPlot);
} else {
$this->plots[] = $aPlot;
}
}
if ($this->graph_theme) {
$this->graph_theme->SetupPlot($aPlot);
}
}
public function AddTable($aTable)
{
if (is_array($aTable)) {
for ($i = 0; $i < count($aTable); ++$i) {
$this->iTables[] = $aTable[$i];
}
} else {
$this->iTables[] = $aTable;
}
}
public function AddIcon($aIcon)
{
if (is_array($aIcon)) {
for ($i = 0; $i < count($aIcon); ++$i) {
$this->iIcons[] = $aIcon[$i];
}
} else {
$this->iIcons[] = $aIcon;
}
}
// Add plot to second Y-scale
public function AddY2($aPlot)
{
if ($aPlot == null) {
Util\JpGraphError::RaiseL(25011); //("Graph::AddY2() You tried to add a null plot to the graph.");
}
if (is_array($aPlot) && count($aPlot) > 0) {
$cl = $aPlot[0];
} else {
$cl = $aPlot;
}
if ($cl instanceof Text\Text) {
$this->AddText($aPlot, true);
} elseif (($cl instanceof Plot\PlotLine)) {
$this->AddLine($aPlot, true);
} elseif (($cl instanceof Plot\PlotBand)) {
$this->AddBand($aPlot, true);
} else {
$this->y2plots[] = $aPlot;
}
if ($this->graph_theme) {
$this->graph_theme->SetupPlot($aPlot);
}
}
// Add plot to the extra Y-axises
public function AddY($aN, $aPlot)
{
if ($aPlot == null) {
Util\JpGraphError::RaiseL(25012); //("Graph::AddYN() You tried to add a null plot to the graph.");
}
if (is_array($aPlot) && count($aPlot) > 0) {
$cl = $aPlot[0];
} else {
$cl = $aPlot;
}
if (($cl instanceof Text\Text) ||
($cl instanceof Plot\PlotLine) ||
($cl instanceof Plot\PlotBand)) {
Util\JpGraphError::RaiseL(25013); //('You can only add standard plots to multiple Y-axis');
} else {
$this->ynplots[$aN][] = $aPlot;
}
if ($this->graph_theme) {
$this->graph_theme->SetupPlot($aPlot);
}
}
// Add text object to the graph
public function AddText($aTxt, $aToY2 = false)
{
if ($aTxt == null) {
Util\JpGraphError::RaiseL(25014); //("Graph::AddText() You tried to add a null text to the graph.");
}
if ($aToY2) {
if (is_array($aTxt)) {
for ($i = 0; $i < count($aTxt); ++$i) {
$this->y2texts[] = $aTxt[$i];
}
} else {
$this->y2texts[] = $aTxt;
}
} else {
if (is_array($aTxt)) {
for ($i = 0; $i < count($aTxt); ++$i) {
$this->texts[] = $aTxt[$i];
}
} else {
$this->texts[] = $aTxt;
}
}
}
// Add a line object (class PlotLine) to the graph
public function AddLine($aLine, $aToY2 = false)
{
if ($aLine == null) {
Util\JpGraphError::RaiseL(25015); //("Graph::AddLine() You tried to add a null line to the graph.");
}
if ($aToY2) {
if (is_array($aLine)) {
for ($i = 0; $i < count($aLine); ++$i) {
//$this->y2lines[]=$aLine[$i];
$this->y2plots[] = $aLine[$i];
}
} else {
//$this->y2lines[] = $aLine;
$this->y2plots[] = $aLine;
}
} else {
if (is_array($aLine)) {
for ($i = 0; $i < count($aLine); ++$i) {
//$this->lines[]=$aLine[$i];
$this->plots[] = $aLine[$i];
}
} else {
//$this->lines[] = $aLine;
$this->plots[] = $aLine;
}
}
}
// Add vertical or horizontal band
public function AddBand($aBand, $aToY2 = false)
{
if ($aBand == null) {
Util\JpGraphError::RaiseL(25016); //(" Graph::AddBand() You tried to add a null band to the graph.");
}
if ($aToY2) {
if (is_array($aBand)) {
for ($i = 0; $i < count($aBand); ++$i) {
$this->y2bands[] = $aBand[$i];
}
} else {
$this->y2bands[] = $aBand;
}
} else {
if (is_array($aBand)) {
for ($i = 0; $i < count($aBand); ++$i) {
$this->bands[] = $aBand[$i];
}
} else {
$this->bands[] = $aBand;
}
}
}
public function SetPlotGradient($aFrom = 'navy', $aTo = 'silver', $aGradType = 2)
{
$this->plot_gradtype = $aGradType;
$this->plot_gradfrom = $aFrom;
$this->plot_gradto = $aTo;
}
public function SetBackgroundGradient($aFrom = 'navy', $aTo = 'silver', $aGradType = 2, $aStyle = BGRAD_FRAME)
{
$this->bkg_gradtype = $aGradType;
$this->bkg_gradstyle = $aStyle;
$this->bkg_gradfrom = $aFrom;
$this->bkg_gradto = $aTo;
}
// Set a country flag in the background
public function SetBackgroundCFlag($aName, $aBgType = BGIMG_FILLPLOT, $aMix = 100)
{
$this->background_cflag = $aName;
$this->background_cflag_type = $aBgType;
$this->background_cflag_mix = $aMix;
}
// Alias for the above method
public function SetBackgroundCountryFlag($aName, $aBgType = BGIMG_FILLPLOT, $aMix = 100)
{
$this->background_cflag = $aName;
$this->background_cflag_type = $aBgType;
$this->background_cflag_mix = $aMix;
}
// Specify a background image
public function SetBackgroundImage($aFileName, $aBgType = BGIMG_FILLPLOT, $aImgFormat = 'auto')
{
// Get extension to determine image type
if ($aImgFormat == 'auto') {
$e = explode('.', $aFileName);
if (empty($e)) {
Util\JpGraphError::RaiseL(25018, $aFileName); //('Incorrect file name for Graph::SetBackgroundImage() : '.$aFileName.' Must have a valid image extension (jpg,gif,png) when using autodetection of image type');
}
$valid_formats = ['png', 'jpg', 'gif'];
$aImgFormat = strtolower($e[count($e) - 1]);
if ($aImgFormat == 'jpeg') {
$aImgFormat = 'jpg';
} elseif (!in_array($aImgFormat, $valid_formats, true)) {
Util\JpGraphError::RaiseL(25019, $aImgFormat); //('Unknown file extension ($aImgFormat) in Graph::SetBackgroundImage() for filename: '.$aFileName);
}
}
$this->background_image = $aFileName;
$this->background_image_type = $aBgType;
$this->background_image_format = $aImgFormat;
}
public function SetBackgroundImageMix($aMix)
{
$this->background_image_mix = $aMix;
}
// Adjust background image position
public function SetBackgroundImagePos($aXpos, $aYpos)
{
$this->background_image_xpos = $aXpos;
$this->background_image_ypos = $aYpos;
}
// Specify axis style (boxed or single)
public function SetAxisStyle($aStyle)
{
$this->iAxisStyle = $aStyle;
}
// Set a frame around the plot area
public function SetBox($aDrawPlotFrame = true, $aPlotFrameColor = [0, 0, 0], $aPlotFrameWeight = 1)
{
$this->boxed = $aDrawPlotFrame;
$this->box_weight = $aPlotFrameWeight;
$this->box_color = $aPlotFrameColor;
}
// Specify color for the plotarea (not the margins)
public function SetColor($aColor)
{
$this->plotarea_color = $aColor;
}
// Specify color for the margins (all areas outside the plotarea)
public function SetMarginColor($aColor)
{
$this->margin_color = $aColor;
}
// Set a frame around the entire image
public function SetFrame($aDrawImgFrame = true, $aImgFrameColor = [0, 0, 0], $aImgFrameWeight = 1)
{
$this->doframe = $aDrawImgFrame;
$this->frame_color = $aImgFrameColor;
$this->frame_weight = $aImgFrameWeight;
}
public function SetFrameBevel($aDepth = 3, $aBorder = false, $aBorderColor = 'black', $aColor1 = 'white@0.4', $aColor2 = 'darkgray@0.4', $aFlg = true)
{
$this->framebevel = $aFlg;
$this->framebeveldepth = $aDepth;
$this->framebevelborder = $aBorder;
$this->framebevelbordercolor = $aBorderColor;
$this->framebevelcolor1 = $aColor1;
$this->framebevelcolor2 = $aColor2;
$this->doshadow = false;
}
// Set the shadow around the whole image
public function SetShadow($aShowShadow = true, $aShadowWidth = 5, $aShadowColor = 'darkgray')
{
$this->doshadow = $aShowShadow;
$this->shadow_color = $aShadowColor;
$this->shadow_width = $aShadowWidth;
$this->footer->iBottomMargin += $aShadowWidth;
$this->footer->iRightMargin += $aShadowWidth;
}
// Specify x,y scale. Note that if you manually specify the scale
// you must also specify the tick distance with a call to Ticks::Set()
public function SetScale($aAxisType, $aYMin = 1, $aYMax = 1, $aXMin = 1, $aXMax = 1)
{
$this->axtype = $aAxisType;
if ($aYMax < $aYMin || $aXMax < $aXMin) {
Util\JpGraphError::RaiseL(25020); //('Graph::SetScale(): Specified Max value must be larger than the specified Min value.');
}
$yt = substr($aAxisType, -3, 3);
if ($yt == 'lin') {
$this->yscale = new LinearScale($aYMin, $aYMax);
} elseif ($yt == 'int') {
$this->yscale = new LinearScale($aYMin, $aYMax);
$this->yscale->SetIntScale();
} elseif ($yt == 'log') {
$this->yscale = new LogScale($aYMin, $aYMax);
} else {
Util\JpGraphError::RaiseL(25021, $aAxisType); //("Unknown scale specification for Y-scale. ($aAxisType)");
}
$xt = substr($aAxisType, 0, 3);
if ($xt == 'lin' || $xt == 'tex') {
$this->xscale = new LinearScale($aXMin, $aXMax, 'x');
$this->xscale->textscale = ($xt == 'tex');
} elseif ($xt == 'int') {
$this->xscale = new LinearScale($aXMin, $aXMax, 'x');
$this->xscale->SetIntScale();
} elseif ($xt == 'dat') {
$this->xscale = new DateScale($aXMin, $aXMax, 'x');
} elseif ($xt == 'log') {
$this->xscale = new LogScale($aXMin, $aXMax, 'x');
} else {
Util\JpGraphError::RaiseL(25022, $aAxisType); //(" Unknown scale specification for X-scale. ($aAxisType)");
}
$this->xaxis = new Axis($this->img, $this->xscale);
$this->yaxis = new Axis($this->img, $this->yscale);
$this->xgrid = new Grid($this->xaxis);
$this->ygrid = new Grid($this->yaxis);
$this->ygrid->Show();
if (!$this->isRunningClear) {
$this->inputValues['aAxisType'] = $aAxisType;
$this->inputValues['aYMin'] = $aYMin;
$this->inputValues['aYMax'] = $aYMax;
$this->inputValues['aXMin'] = $aXMin;
$this->inputValues['aXMax'] = $aXMax;
if ($this->graph_theme) {
$this->graph_theme->ApplyGraph($this);
}
}
$this->isAfterSetScale = true;
}
// Specify secondary Y scale
public function SetY2Scale($aAxisType = 'lin', $aY2Min = 1, $aY2Max = 1)
{
if ($aAxisType == 'lin') {
$this->y2scale = new LinearScale($aY2Min, $aY2Max);
} elseif ($aAxisType == 'int') {
$this->y2scale = new LinearScale($aY2Min, $aY2Max);
$this->y2scale->SetIntScale();
} elseif ($aAxisType == 'log') {
$this->y2scale = new LogScale($aY2Min, $aY2Max);
} else {
Util\JpGraphError::RaiseL(25023, $aAxisType); //("JpGraph: Unsupported Y2 axis type: $aAxisType\nMust be one of (lin,log,int)");
}
$this->y2axis = new Axis($this->img, $this->y2scale);
$this->y2axis->scale->ticks->SetDirection(SIDE_LEFT);
$this->y2axis->SetLabelSide(SIDE_RIGHT);
$this->y2axis->SetPos('max');
$this->y2axis->SetTitleSide(SIDE_RIGHT);
// Deafult position is the max x-value
$this->y2grid = new Grid($this->y2axis);
if ($this->graph_theme) {
$this->graph_theme->ApplyGraph($this);
}
}
// Set the delta position (in pixels) between the multiple Y-axis
public function SetYDeltaDist($aDist)
{
$this->iYAxisDeltaPos = $aDist;
}
// Specify secondary Y scale
public function SetYScale($aN, $aAxisType = 'lin', $aYMin = 1, $aYMax = 1)
{
if ($aAxisType == 'lin') {
$this->ynscale[$aN] = new LinearScale($aYMin, $aYMax);
} elseif ($aAxisType == 'int') {
$this->ynscale[$aN] = new LinearScale($aYMin, $aYMax);
$this->ynscale[$aN]->SetIntScale();
} elseif ($aAxisType == 'log') {
$this->ynscale[$aN] = new LogScale($aYMin, $aYMax);
} else {
Util\JpGraphError::RaiseL(25024, $aAxisType); //("JpGraph: Unsupported Y axis type: $aAxisType\nMust be one of (lin,log,int)");
}
$this->ynaxis[$aN] = new Axis($this->img, $this->ynscale[$aN]);
$this->ynaxis[$aN]->scale->ticks->SetDirection(SIDE_LEFT);
$this->ynaxis[$aN]->SetLabelSide(SIDE_RIGHT);
if ($this->graph_theme) {
$this->graph_theme->ApplyGraph($this);
}
}
// Specify density of ticks when autoscaling 'normal', 'dense', 'sparse', 'verysparse'
// The dividing factor have been determined heuristically according to my aesthetic
// sense (or lack off) y.m.m.v !
public function SetTickDensity($aYDensity = TICKD_NORMAL, $aXDensity = TICKD_NORMAL)
{
$this->xtick_factor = 30;
$this->ytick_factor = 25;
switch ($aYDensity) {
case TICKD_DENSE:
$this->ytick_factor = 12;
break;
case TICKD_NORMAL:
$this->ytick_factor = 25;
break;
case TICKD_SPARSE:
$this->ytick_factor = 40;
break;
case TICKD_VERYSPARSE:
$this->ytick_factor = 100;
break;
default:
Util\JpGraphError::RaiseL(25025, $densy); //("JpGraph: Unsupported Tick density: $densy");
}
switch ($aXDensity) {
case TICKD_DENSE:
$this->xtick_factor = 15;
break;
case TICKD_NORMAL:
$this->xtick_factor = 30;
break;
case TICKD_SPARSE:
$this->xtick_factor = 45;
break;
case TICKD_VERYSPARSE:
$this->xtick_factor = 60;
break;
default:
Util\JpGraphError::RaiseL(25025, $densx); //("JpGraph: Unsupported Tick density: $densx");
}
}
// Get a string of all image map areas
public function GetCSIMareas()
{
if (!$this->iHasStroked) {
$this->Stroke(_CSIM_SPECIALFILE);
}
$csim = $this->title->GetCSIMAreas();
$csim .= $this->subtitle->GetCSIMAreas();
$csim .= $this->subsubtitle->GetCSIMAreas();
$csim .= $this->legend->GetCSIMAreas();
if ($this->y2axis != null) {
$csim .= $this->y2axis->title->GetCSIMAreas();
}
if ($this->texts != null) {
$n = count($this->texts);
for ($i = 0; $i < $n; ++$i) {
$csim .= $this->texts[$i]->GetCSIMAreas();
}
}
if ($this->y2texts != null && $this->y2scale != null) {
$n = count($this->y2texts);
for ($i = 0; $i < $n; ++$i) {
$csim .= $this->y2texts[$i]->GetCSIMAreas();
}
}
if ($this->yaxis != null && $this->xaxis != null) {
$csim .= $this->yaxis->title->GetCSIMAreas();
$csim .= $this->xaxis->title->GetCSIMAreas();
}
$n = count($this->plots);
for ($i = 0; $i < $n; ++$i) {
$csim .= $this->plots[$i]->GetCSIMareas();
}
$n = count($this->y2plots);
for ($i = 0; $i < $n; ++$i) {
$csim .= $this->y2plots[$i]->GetCSIMareas();
}
$n = count($this->ynaxis);
for ($i = 0; $i < $n; ++$i) {
$m = count($this->ynplots[$i]);
for ($j = 0; $j < $m; ++$j) {
$csim .= $this->ynplots[$i][$j]->GetCSIMareas();
}
}
$n = count($this->iTables);
for ($i = 0; $i < $n; ++$i) {
$csim .= $this->iTables[$i]->GetCSIMareas();
}
return $csim;
}
// Get a complete <MAP>..</MAP> tag for the final image map
public function GetHTMLImageMap($aMapName)
{
$im = "<map name=\"${aMapName}\" id=\"${aMapName}\" >\n";
$im .= $this->GetCSIMareas();
$im .= '</map>';
return $im;
}
public function CheckCSIMCache($aCacheName, $aTimeOut = 60)
{
global $_SERVER;
if ($aCacheName == 'auto') {
$aCacheName = basename($_SERVER['PHP_SELF']);
}
$urlarg = $this->GetURLArguments();
$this->csimcachename = CSIMCACHE_DIR . $aCacheName . $urlarg;
$this->csimcachetimeout = $aTimeOut;
// First determine if we need to check for a cached version
// This differs from the standard cache in the sense that the
// image and CSIM map HTML file is written relative to the directory
// the script executes in and not the specified cache directory.
// The reason for this is that the cache directory is not necessarily
// accessible from the HTTP server.
if ($this->csimcachename != '') {
$dir = dirname($this->csimcachename);
$base = basename($this->csimcachename);
$base = strtok($base, '.');
$suffix = strtok('.');
$basecsim = $dir . '/' . $base . '?' . $urlarg . '_csim_.html';
$baseimg = $dir . '/' . $base . '?' . $urlarg . '.' . $this->img->img_format;
$timedout = false;
// Does it exist at all ?
if (file_exists($basecsim) && file_exists($baseimg)) {
// Check that it hasn't timed out
$diff = time() - filemtime($basecsim);
if ($this->csimcachetimeout > 0 && ($diff > $this->csimcachetimeout * 60)) {
$timedout = true;
@unlink($basecsim);
@unlink($baseimg);
} else {
if ($fh = @fopen($basecsim, 'r')) {
fpassthru($fh);
return true;
}
Util\JpGraphError::RaiseL(25027, $basecsim); //(" Can't open cached CSIM \"$basecsim\" for reading.");
}
}
}
return false;
}
// Build the argument string to be used with the csim images
public static function GetURLArguments($aAddRecursiveBlocker = false)
{
if ($aAddRecursiveBlocker) {
// This is a JPGRAPH internal defined that prevents
// us from recursively coming here again
$urlarg = _CSIM_DISPLAY . '=1';
}
// Now reconstruct any user URL argument
reset($_GET);
while (list($key, $value) = each($_GET)) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$urlarg .= '&' . $key . '%5B' . $k . '%5D=' . urlencode($v);
}
} else {
$urlarg .= '&' . $key . '=' . urlencode($value);
}
}
// It's not ideal to convert POST argument to GET arguments
// but there is little else we can do. One idea for the
// future might be recreate the POST header in case.
reset($_POST);
while (list($key, $value) = each($_POST)) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$urlarg .= '&' . $key . '%5B' . $k . '%5D=' . urlencode($v);
}
} else {
$urlarg .= '&' . $key . '=' . urlencode($value);
}
}
return $urlarg;
}
public function SetCSIMImgAlt($aAlt)
{
$this->iCSIMImgAlt = $aAlt;
}
public function StrokeCSIM($aScriptName = 'auto', $aCSIMName = '', $aBorder = 0)
{
if ($aCSIMName == '') {
// create a random map name
srand((integer) (microtime() * 1000000));
$r = rand(0, 100000);