summaryrefslogtreecommitdiffstats
path: root/Under-Testing/Website/class/pData.class.php
blob: 9cfbc3fde7e03050c1e585b9f3db48aeb8d327e5 (plain) (blame)
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
<?php
 /*
     pDraw - class to manipulate data arrays

     Version     : 2.1.2
     Made by     : Jean-Damien POGOLOTTI
     Last Update : 03/08/11

     This file can be distributed under the license you can find at :

                       http://www.pchart.net/license

     You can find the whole class documentation on the pChart web site.
 */

 /* Axis configuration */
 define("AXIS_FORMAT_DEFAULT"		, 680001);
 define("AXIS_FORMAT_TIME"		, 680002);
 define("AXIS_FORMAT_DATE"		, 680003);
 define("AXIS_FORMAT_METRIC"		, 680004);
 define("AXIS_FORMAT_CURRENCY"		, 680005);

 /* Axis position */
 define("AXIS_POSITION_LEFT"		, 681001);
 define("AXIS_POSITION_RIGHT"		, 681002);
 define("AXIS_POSITION_TOP"		, 681001);
 define("AXIS_POSITION_BOTTOM"		, 681002);

 /* Families of data points */
 define("SERIE_SHAPE_FILLEDCIRCLE"	, 681011);
 define("SERIE_SHAPE_FILLEDTRIANGLE"	, 681012);
 define("SERIE_SHAPE_FILLEDSQUARE"	, 681013);
 define("SERIE_SHAPE_FILLEDDIAMOND"	, 681017);
 define("SERIE_SHAPE_CIRCLE"		, 681014);
 define("SERIE_SHAPE_TRIANGLE"		, 681015);
 define("SERIE_SHAPE_SQUARE"		, 681016);
 define("SERIE_SHAPE_DIAMOND"		, 681018);

 /* Axis position */
 define("AXIS_X"			, 682001);
 define("AXIS_Y"			, 682002);

 /* Define value limits */
 define("ABSOLUTE_MIN"          	, -10000000000000);
 define("ABSOLUTE_MAX"          	, 10000000000000);

 /* Replacement to the PHP NULL keyword */
 define("VOID"                  	, 0.123456789);

 /* pData class definition */
 class pData
  {
   var $Data;

   var $Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
                        "1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100),
                        "2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100),
                        "3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100),
                        "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100),
                        "5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100),
                        "6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100),
                        "7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100));

   /* Class creator */
   function pData()
    {
     $this->Data = "";
     $this->Data["XAxisDisplay"] = AXIS_FORMAT_DEFAULT;
     $this->Data["XAxisFormat"]  = NULL;
     $this->Data["XAxisName"]    = NULL;
     $this->Data["XAxisUnit"]    = NULL;
     $this->Data["Abscissa"]     = NULL;
     $this->Data["Axis"][0]["Display"]  = AXIS_FORMAT_DEFAULT;
     $this->Data["Axis"][0]["Position"] = AXIS_POSITION_LEFT;
     $this->Data["Axis"][0]["Identity"] = AXIS_Y;
    }

   /* Add a single point or an array to the given serie */
   function addPoints($Values,$SerieName="Serie1")
    {
     if (!isset($this->Data["Series"][$SerieName]))
      $this->initialise($SerieName);

     if ( is_array($Values) )
      {
       foreach($Values as $Key => $Value)
        { $this->Data["Series"][$SerieName]["Data"][] = $Value; }
      }
     else
      $this->Data["Series"][$SerieName]["Data"][] = $Values;

     if ( $Values != VOID )
      {
       $this->Data["Series"][$SerieName]["Max"] = max($this->stripVOID($this->Data["Series"][$SerieName]["Data"]));
       $this->Data["Series"][$SerieName]["Min"] = min($this->stripVOID($this->Data["Series"][$SerieName]["Data"]));
      }
    }

   /* Strip VOID values */
   function stripVOID($Values)
    { $Result = array(); foreach($Values as $Key => $Value) { if ( $Value != VOID ) { $Result[] = $Value; } } return($Result); }

   /* Return the number of values contained in a given serie */
   function getSerieCount($Serie)
    { if (isset($this->Data["Series"][$Serie]["Data"])) { return(sizeof($this->Data["Series"][$Serie]["Data"])); } else { return(0); } }

   /* Remove a serie from the pData object */
   function removeSerie($Series)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie])) { unset($this->Data["Series"][$Serie]); } }
    }

   /* Return a value from given serie & index */
   function getValueAt($Serie,$Index=0)
    { if (isset($this->Data["Series"][$Serie]["Data"][$Index])) { return($this->Data["Series"][$Serie]["Data"][$Index]); } else { return(NULL); } }

   /* Return the values array */
   function getValues($Serie)
    { if (isset($this->Data["Series"][$Serie]["Data"])) { return($this->Data["Series"][$Serie]["Data"]); } else { return(NULL); } }

   /* Reverse the values in the given serie */
   function reverseSerie($Series)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]["Data"])) { $this->Data["Series"][$Serie]["Data"] = array_reverse($this->Data["Series"][$Serie]["Data"]); } }
    }

   /* Return the sum of the serie values */
   function getSum($Serie)
    { if (isset($this->Data["Series"][$Serie])) { return(array_sum($this->Data["Series"][$Serie]["Data"])); } else { return(NULL); } }

   /* Return the max value of a given serie */
   function getMax($Serie)
    { if (isset($this->Data["Series"][$Serie]["Max"])) { return($this->Data["Series"][$Serie]["Max"]); } else { return(NULL); } }

   /* Return the min value of a given serie */
   function getMin($Serie)
    { if (isset($this->Data["Series"][$Serie]["Min"])) { return($this->Data["Series"][$Serie]["Min"]); } else { return(NULL); } }

   /* Set the description of a given serie */
   function setSerieShape($Series,$Shape=SERIE_SHAPE_FILLEDCIRCLE)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Shape"] = $Shape; } }
    }

   /* Set the description of a given serie */
   function setSerieDescription($Series,$Description="My serie")
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Description"] = $Description; } }
    }

   /* Set a serie as "drawable" while calling a rendering function */
   function setSerieDrawable($Series,$Drawable=TRUE)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["isDrawable"] = $Drawable; } }
    }

   /* Set the icon associated to a given serie */
   function setSeriePicture($Series,$Picture=NULL)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if (isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Picture"] = $Picture; } }
    }

   /* Set the name of the X Axis */
   function setXAxisName($Name)
    { $this->Data["XAxisName"] = $Name; }

   /* Set the display mode of the  X Axis */
   function setXAxisDisplay($Mode,$Format=NULL)
    { $this->Data["XAxisDisplay"] = $Mode; $this->Data["XAxisFormat"]  = $Format; }

   /* Set the unit that will be displayed on the X axis */
   function setXAxisUnit($Unit)
    { $this->Data["XAxisUnit"] = $Unit; }

   /* Set the serie that will be used as abscissa */
   function setAbscissa($Serie)
    { if (isset($this->Data["Series"][$Serie])) { $this->Data["Abscissa"] = $Serie; } }

   /* Set the name of the abscissa axis */
   function setAbscissaName($Name)
    { $this->Data["AbscissaName"] = $Name; }

   /* Create a scatter group specifyin X and Y data series */
   function setScatterSerie($SerieX,$SerieY,$ID=0)
    { if (isset($this->Data["Series"][$SerieX]) && isset($this->Data["Series"][$SerieY]) ) { $this->initScatterSerie($ID); $this->Data["ScatterSeries"][$ID]["X"] = $SerieX; $this->Data["ScatterSeries"][$ID]["Y"] = $SerieY; } }

   /* Set the shape of a given sctatter serie */
   function setScatterSerieShape($ID,$Shape=SERIE_SHAPE_FILLEDCIRCLE)
    { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Shape"] = $Shape; } }

   /* Set the description of a given scatter serie */
   function setScatterSerieDescription($ID,$Description="My serie")
    { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Description"] = $Description; } }

   /* Set the icon associated to a given scatter serie */
   function setScatterSeriePicture($ID,$Picture=NULL)
    { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Picture"] = $Picture; } }

   /* Set a scatter serie as "drawable" while calling a rendering function */
   function setScatterSerieDrawable($ID ,$Drawable=TRUE)
    { if (isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["isDrawable"] = $Drawable; } }

   /* Define if a scatter serie should be draw with ticks */
   function setScatterSerieTicks($ID,$Width=0)
    { if ( isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Ticks"] = $Width; } }

   /* Define if a scatter serie should be draw with a special weight */
   function setScatterSerieWeight($ID,$Weight=0)
    { if ( isset($this->Data["ScatterSeries"][$ID]) ) { $this->Data["ScatterSeries"][$ID]["Weight"] = $Weight; } }

   /* Associate a color to a scatter serie */
   function setScatterSerieColor($ID,$Format)
    {
     $R	    = isset($Format["R"]) ? $Format["R"] : 0;
     $G	    = isset($Format["G"]) ? $Format["G"] : 0;
     $B	    = isset($Format["B"]) ? $Format["B"] : 0;
     $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100;

     if ( isset($this->Data["ScatterSeries"][$ID]) )
      {
       $this->Data["ScatterSeries"][$ID]["Color"]["R"] = $R;
       $this->Data["ScatterSeries"][$ID]["Color"]["G"] = $G;
       $this->Data["ScatterSeries"][$ID]["Color"]["B"] = $B;
       $this->Data["ScatterSeries"][$ID]["Color"]["Alpha"] = $Alpha;
      }
    }

   /* Compute the series limits for an individual and global point of view */
   function limits()
    {
     $GlobalMin = ABSOLUTE_MAX;
     $GlobalMax = ABSOLUTE_MIN;

     foreach($this->Data["Series"] as $Key => $Value)
      {
       if ( $this->Data["Abscissa"] != $Key && $this->Data["Series"][$Key]["isDrawable"] == TRUE)
        {
         if ( $GlobalMin > $this->Data["Series"][$Key]["Min"] ) { $GlobalMin = $this->Data["Series"][$Key]["Min"]; }
         if ( $GlobalMax < $this->Data["Series"][$Key]["Max"] ) { $GlobalMax = $this->Data["Series"][$Key]["Max"]; }
        }
      }
     $this->Data["Min"] = $GlobalMin;
     $this->Data["Max"] = $GlobalMax;

     return(array($GlobalMin,$GlobalMax));
    }

   /* Mark all series as drawable */
   function drawAll()
    { foreach($this->Data["Series"] as $Key => $Value) { if ( $this->Data["Abscissa"] != $Key ) { $this->Data["Series"][$Key]["isDrawable"]=TRUE; } } }    

   /* Return the average value of the given serie */
   function getSerieAverage($Serie)
    {
     if ( isset($this->Data["Series"][$Serie]) )
      {
       $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]);
       return(array_sum($SerieData)/sizeof($SerieData));
      }
     else
      return(NULL);
    }

   /* Return the geometric mean of the given serie */
   function getGeometricMean($Serie)
    {
     if ( isset($this->Data["Series"][$Serie]) )
      {
       $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]);
       $Seriesum  = 1; foreach($SerieData as $Key => $Value) { $Seriesum = $Seriesum * $Value; }
       return(pow($Seriesum,1/sizeof($SerieData)));
      }
     else
      return(NULL);
    }

   /* Return the harmonic mean of the given serie */
   function getHarmonicMean($Serie)
    {
     if ( isset($this->Data["Series"][$Serie]) )
      {
       $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]);
       $Seriesum  = 0; foreach($SerieData as $Key => $Value) { $Seriesum = $Seriesum + 1/$Value; }
       return(sizeof($SerieData)/$Seriesum);
      }
     else
      return(NULL);
    }

   /* Return the standard deviation of the given serie */
   function getStandardDeviation($Serie)
    {
     if ( isset($this->Data["Series"][$Serie]) )
      {
       $Average   = $this->getSerieAverage($Serie);
       $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]);

       $DeviationSum = 0;
       foreach($SerieData as $Key => $Value)
        $DeviationSum = $DeviationSum + ($Value-$Average)*($Value-$Average);

       $Deviation = sqrt($DeviationSum/count($SerieData));

       return($Deviation);
      }
     else
      return(NULL);
    }

   /* Return the Coefficient of variation of the given serie */
   function getCoefficientOfVariation($Serie)
    {
     if ( isset($this->Data["Series"][$Serie]) )
      {
       $Average           = $this->getSerieAverage($Serie);
       $StandardDeviation = $this->getStandardDeviation($Serie);

       if ( $StandardDeviation != 0 )
        return($StandardDeviation/$Average);
       else
        return(NULL);
      }
     else
      return(NULL);
    }

   /* Return the median value of the given serie */
   function getSerieMedian($Serie)
    {
     if ( isset($this->Data["Series"][$Serie]) )
      {
       $SerieData = $this->stripVOID($this->Data["Series"][$Serie]["Data"]);
       sort($SerieData);
       $SerieCenter = floor(sizeof($SerieData)/2);

       if ( isset($SerieData[$SerieCenter]) )
        return($SerieData[$SerieCenter]);
       else
        return(NULL);
      }
     else
      return(NULL);
    }

   /* Return the x th percentil of the given serie */
   function getSeriePercentile($Serie="Serie1",$Percentil=95)
    {
     if (!isset($this->Data["Series"][$Serie]["Data"])) { return(NULL); }

     $Values = count($this->Data["Series"][$Serie]["Data"])-1;
     if ( $Values < 0 ) { $Values = 0; }

     $PercentilID  = floor(($Values/100)*$Percentil+.5);
     $SortedValues = $this->Data["Series"][$Serie]["Data"];
     sort($SortedValues);

     if ( is_numeric($SortedValues[$PercentilID]) )
      return($SortedValues[$PercentilID]);
     else
      return(NULL);
    }

   /* Add random values to a given serie */
   function addRandomValues($SerieName="Serie1",$Options="")
    {
     $Values    = isset($Options["Values"]) ? $Options["Values"] : 20;
     $Min       = isset($Options["Min"]) ? $Options["Min"] : 0;
     $Max       = isset($Options["Max"]) ? $Options["Max"] : 100;
     $withFloat = isset($Options["withFloat"]) ? $Options["withFloat"] : FALSE;

     for ($i=0;$i<=$Values;$i++)
      {
       if ( $withFloat ) { $Value = rand($Min*100,$Max*100)/100; } else { $Value = rand($Min,$Max); }
       $this->addPoints($Value,$SerieName);
      }
    }

   /* Test if we have valid data */
   function containsData()
    {
     if (!isset($this->Data["Series"])) { return(FALSE); }

     $Result = FALSE;
     foreach($this->Data["Series"] as $Key => $Value)
      { if ( $this->Data["Abscissa"] != $Key && $this->Data["Series"][$Key]["isDrawable"]==TRUE) { $Result=TRUE; } }
     return($Result);
    }

   /* Set the display mode of an Axis */
   function setAxisDisplay($AxisID,$Mode=AXIS_FORMAT_DEFAULT,$Format=NULL)
    {
     if ( isset($this->Data["Axis"][$AxisID] ) )
      {
       $this->Data["Axis"][$AxisID]["Display"] = $Mode;
       if ( $Format != NULL ) { $this->Data["Axis"][$AxisID]["Format"] = $Format; }
      }
    }

   /* Set the position of an Axis */
   function setAxisPosition($AxisID,$Position=AXIS_POSITION_LEFT)
    { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Position"] = $Position; } }

   /* Associate an unit to an axis */
   function setAxisUnit($AxisID,$Unit)
    { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Unit"] = $Unit; } }

   /* Associate a name to an axis */
   function setAxisName($AxisID,$Name)
    { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Name"] = $Name; } }

   /* Associate a color to an axis */
   function setAxisColor($AxisID,$Format)
    {
     $R	    = isset($Format["R"]) ? $Format["R"] : 0;
     $G	    = isset($Format["G"]) ? $Format["G"] : 0;
     $B	    = isset($Format["B"]) ? $Format["B"] : 0;
     $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100;

     if ( isset($this->Data["Axis"][$AxisID] ) )
      {
       $this->Data["Axis"][$AxisID]["Color"]["R"] = $R;
       $this->Data["Axis"][$AxisID]["Color"]["G"] = $G;
       $this->Data["Axis"][$AxisID]["Color"]["B"] = $B;
       $this->Data["Axis"][$AxisID]["Color"]["Alpha"] = $Alpha;
      }
    }


   /* Design an axis as X or Y member */
   function setAxisXY($AxisID,$Identity=AXIS_Y)
    { if ( isset($this->Data["Axis"][$AxisID] ) ) { $this->Data["Axis"][$AxisID]["Identity"] = $Identity; } }

   /* Associate one data serie with one axis */
   function setSerieOnAxis($Series,$AxisID)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie)
      {
       $PreviousAxis = $this->Data["Series"][$Serie]["Axis"];

       /* Create missing axis */
       if ( !isset($this->Data["Axis"][$AxisID] ) )
        { $this->Data["Axis"][$AxisID]["Position"] = AXIS_POSITION_LEFT; $this->Data["Axis"][$AxisID]["Identity"] = AXIS_Y;}

       $this->Data["Series"][$Serie]["Axis"] = $AxisID;

       /* Cleanup unused axis */
       $Found = FALSE;
       foreach($this->Data["Series"] as $SerieName => $Values) { if ( $Values["Axis"] == $PreviousAxis ) { $Found = TRUE; } }
       if (!$Found) { unset($this->Data["Axis"][$PreviousAxis]); }
      }
    }

   /* Define if a serie should be draw with ticks */
   function setSerieTicks($Series,$Width=0)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if ( isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Ticks"] = $Width; } }
    }

   /* Define if a serie should be draw with a special weight */
   function setSerieWeight($Series,$Weight=0)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }
     foreach($Series as $Key => $Serie) { if ( isset($this->Data["Series"][$Serie]) ) { $this->Data["Series"][$Serie]["Weight"] = $Weight; } }
    }

   /* Returns the palette of the given serie */
   function getSeriePalette($Serie)
    {
     if ( !isset($this->Data["Series"][$Serie]) ) { return(NULL); }

     $Result = "";
     $Result["R"] = $this->Data["Series"][$Serie]["Color"]["R"];
     $Result["G"] = $this->Data["Series"][$Serie]["Color"]["G"];
     $Result["B"] = $this->Data["Series"][$Serie]["Color"]["B"];
     $Result["Alpha"] = $this->Data["Series"][$Serie]["Color"]["Alpha"];

     return($Result);
    }

   /* Set the color of one serie */
   function setPalette($Series,$Format=NULL)
    {
     if ( !is_array($Series) ) { $Series = $this->convertToArray($Series); }

     foreach($Series as $Key => $Serie)
      {
       $R	    = isset($Format["R"]) ? $Format["R"] : 0;
       $G	    = isset($Format["G"]) ? $Format["G"] : 0;
       $B	    = isset($Format["B"]) ? $Format["B"] : 0;
       $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100;

       if ( isset($this->Data["Series"][$Serie]) )
        {
         $OldR = $this->Data["Series"][$Serie]["Color"]["R"]; $OldG = $this->Data["Series"][$Serie]["Color"]["G"]; $OldB = $this->Data["Series"][$Serie]["Color"]["B"];
         $this->Data["Series"][$Serie]["Color"]["R"] = $R;
         $this->Data["Series"][$Serie]["Color"]["G"] = $G;
         $this->Data["Series"][$Serie]["Color"]["B"] = $B;
         $this->Data["Series"][$Serie]["Color"]["Alpha"] = $Alpha;

         /* Do reverse processing on the internal palette array */
         foreach ($this->Palette as $Key => $Value)
          { if ($Value["R"] == $OldR && $Value["G"] == $OldG && $Value["B"] == $OldB) { $this->Palette[$Key]["R"] = $R; $this->Palette[$Key]["G"] = $G; $this->Palette[$Key]["B"] = $B; $this->Palette[$Key]["Alpha"] = $Alpha;} }
        }
      }
    }

   /* Load a palette file */
   function loadPalette($FileName,$Overwrite=FALSE)
    {
     if ( !file_exists($FileName) ) { return(-1); }
     if ( $Overwrite ) { $this->Palette = ""; }

     $fileHandle = @fopen($FileName, "r");
     if (!$fileHandle) { return(-1); }
     while (!feof($fileHandle))
      {
       $buffer = fgets($fileHandle, 4096);
       if ( preg_match("/,/",$buffer) )
        {
         list($R,$G,$B,$Alpha) = preg_split("/,/",$buffer);
         if ( $this->Palette == "" ) { $ID = 0; } else { $ID = count($this->Palette); }
         $this->Palette[$ID] = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha);
        }
      }
     fclose($fileHandle);

     /* Apply changes to current series */
     $ID = 0;
     if ( isset($this->Data["Series"]))
      {
       foreach($this->Data["Series"] as $Key => $Value)
        {
         if ( !isset($this->Palette[$ID]) )
          $this->Data["Series"][$Key]["Color"] = array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>0);
         else
          $this->Data["Series"][$Key]["Color"] = $this->Palette[$ID];
         $ID++;
        }
      }
    }

   /* Initialise a given scatter serie */
   function initScatterSerie($ID)
    {
     if ( isset($this->Data["ScatterSeries"][$ID]) ) { return(0); }

     $this->Data["ScatterSeries"][$ID]["Description"]	= "Scatter ".$ID;
     $this->Data["ScatterSeries"][$ID]["isDrawable"]	= TRUE;
     $this->Data["ScatterSeries"][$ID]["Picture"]	= NULL;
     $this->Data["ScatterSeries"][$ID]["Ticks"]		= 0;
     $this->Data["ScatterSeries"][$ID]["Weight"]	= 0;

     if ( isset($this->Palette[$ID]) )
      $this->Data["ScatterSeries"][$ID]["Color"] = $this->Palette[$ID];
     else
      {
       $this->Data["ScatterSeries"][$ID]["Color"]["R"] = rand(0,255);
       $this->Data["ScatterSeries"][$ID]["Color"]["G"] = rand(0,255);
       $this->Data["ScatterSeries"][$ID]["Color"]["B"] = rand(0,255);
       $this->Data["ScatterSeries"][$ID]["Color"]["Alpha"] = 100;
      }
    }

   /* Initialise a given serie */
   function initialise($Serie)
    {
     if ( isset($this->Data["Series"]) ) { $ID = count($this->Data["Series"]); } else { $ID = 0; }

     $this->Data["Series"][$Serie]["Description"]	= $Serie;
     $this->Data["Series"][$Serie]["isDrawable"]	= TRUE;
     $this->Data["Series"][$Serie]["Picture"]		= NULL;
     $this->Data["Series"][$Serie]["Max"]		= NULL;
     $this->Data["Series"][$Serie]["Min"]		= NULL;
     $this->Data["Series"][$Serie]["Axis"]		= 0;
     $this->Data["Series"][$Serie]["Ticks"]		= 0;
     $this->Data["Series"][$Serie]["Weight"]		= 0;
     $this->Data["Series"][$Serie]["Shape"]		= SERIE_SHAPE_FILLEDCIRCLE;

     if ( isset($this->Palette[$ID]) )
      $this->Data["Series"][$Serie]["Color"] = $this->Palette[$ID];
     else
      {
       $this->Data["Series"][$Serie]["Color"]["R"] = rand(0,255);
       $this->Data["Series"][$Serie]["Color"]["G"] = rand(0,255);
       $this->Data["Series"][$Serie]["Color"]["B"] = rand(0,255);
       $this->Data["Series"][$Serie]["Color"]["Alpha"] = 100;
      }
    }
     
   function normalize($NormalizationFactor=100,$UnitChange=NULL,$Round=1)
    {
     $Abscissa = $this->Data["Abscissa"];

     $SelectedSeries = "";
     $MaxVal         = 0;
     foreach($this->Data["Axis"] as $AxisID => $Axis)
      {
       if ( $UnitChange != NULL ) { $this->Data["Axis"][$AxisID]["Unit"] = $UnitChange; }

       foreach($this->Data["Series"] as $SerieName => $Serie)
        {
         if ($Serie["Axis"] == $AxisID && $Serie["isDrawable"] == TRUE && $SerieName != $Abscissa)
          {
           $SelectedSeries[$SerieName] = $SerieName;

           if ( count($Serie["Data"] ) > $MaxVal ) { $MaxVal = count($Serie["Data"]); }
          }
        }
      }

     for($i=0;$i<=$MaxVal-1;$i++)
      {
       $Factor = 0;
       foreach ($SelectedSeries as $Key => $SerieName )
        {
         $Value = $this->Data["Series"][$SerieName]["Data"][$i];
         if ( $Value != VOID )
          $Factor = $Factor + abs($Value);
        }

       if ( $Factor != 0 )
        {
         $Factor = $NormalizationFactor / $Factor;

         foreach ($SelectedSeries as $Key => $SerieName )
          {
           $Value = $this->Data["Series"][$SerieName]["Data"][$i];

           if ( $Value != VOID && $Factor != $NormalizationFactor )
            $this->Data["Series"][$SerieName]["Data"][$i] = round(abs($Value)*$Factor,$Round);
           elseif ( $Value == VOID || $Value == 0 )
            $this->Data["Series"][$SerieName]["Data"][$i] = VOID;
           elseif ( $Factor == $NormalizationFactor )
            $this->Data["Series"][$SerieName]["Data"][$i] = $NormalizationFactor;
          }
        }
      }

     foreach ($SelectedSeries as $Key => $SerieName )
      {
       $this->Data["Series"][$SerieName]["Max"] = max($this->stripVOID($this->Data["Series"][$SerieName]["Data"]));
       $this->Data["Series"][$SerieName]["Min"] = min($this->stripVOID($this->Data["Series"][$SerieName]["Data"]));
      }
    }

   /* Load data from a CSV (or similar) data source */
   function importFromCSV($FileName,$Options="")
    {
     $Delimiter		= isset($Options["Delimiter"]) ? $Options["Delimiter"] : ",";
     $GotHeader		= isset($Options["GotHeader"]) ? $Options["GotHeader"] : FALSE;
     $SkipColumns	= isset($Options["SkipColumns"]) ? $Options["SkipColumns"] : array(-1);
     $DefaultSerieName	= isset($Options["DefaultSerieName"]) ? $Options["DefaultSerieName"] : "Serie";

     $Handle = @fopen($FileName,"r");
     if ($Handle)
      {
       $HeaderParsed = FALSE; $SerieNames = "";
       while (!feof($Handle))
        {
         $Buffer = fgets($Handle, 4096);
         $Buffer = str_replace(chr(10),"",$Buffer);
         $Buffer = str_replace(chr(13),"",$Buffer);
         $Values = preg_split("/".$Delimiter."/",$Buffer);

         if ( $Buffer != "" )
          {
           if ( $GotHeader && !$HeaderParsed )
            {
             foreach($Values as $Key => $Name) { if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $Name; } }
             $HeaderParsed = TRUE;
            }
           else
            {
             if ($SerieNames == "" ) { foreach($Values as $Key => $Name) {  if ( !in_array($Key,$SkipColumns) ) { $SerieNames[$Key] = $DefaultSerieName.$Key; } } }
             foreach($Values as $Key => $Value) {  if ( !in_array($Key,$SkipColumns) ) { $this->addPoints($Value,$SerieNames[$Key]); } }
            }
          }
        }
       fclose($Handle);
      }
    }

   /* Return the data & configuration of the series */
   function getData()
    { return($this->Data); }

   /* Save a palette element */
   function savePalette($ID,$Color)
    { $this->Palette[$ID] = $Color; }

   /* Return the palette of the series */
   function getPalette()
    { return($this->Palette); }

   /* Called by the scaling algorithm to save the config */
   function saveAxisConfig($Axis) { $this->Data["Axis"]=$Axis; }

   /* Save the Y Margin if set */
   function saveYMargin($Value) { $this->Data["YMargin"]=$Value; }

   /* Save extended configuration to the pData object */
   function saveExtendedData($Tag,$Values) { $this->Data["Extended"][$Tag]=$Values; }

   /* Called by the scaling algorithm to save the orientation of the scale */
   function saveOrientation($Orientation) { $this->Data["Orientation"]=$Orientation; }

   /* Convert a string to a single elements array */
   function convertToArray($Value)
    { $Values = ""; $Values[] = $Value; return($Values); }

   /* Class string wrapper */
   function __toString()
    { return("pData object."); }
  }
?>