MkFramework
 All Data Structures Functions
plugin_chartSVG.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
24 
25  public static $PIE='PIE';
26  public static $HISTO='HISTO';
27  public static $LINES='LINES';
28  public static $BAR='BAR';
29 
30  private $iWidth;
31 
32  private $oChart;
33 
34  public function __construct($sType,$iWidth=null,$iHeight=null){
35  $this->iWidth=$iWidth;
36  $this->iHeight=$iHeight;
37 
38  if($sType==self::$PIE){
39  $this->oChart=new plugin_chartPieSVG($this->iWidth,$this->iHeight);
40  }else if($sType==self::$HISTO){
41  $this->oChart=new plugin_chartHistoSVG($this->iWidth,$this->iHeight);
42  }else if($sType==self::$LINES){
43  $this->oChart=new plugin_chartLineSVG($this->iWidth,$this->iHeight);
44  }else if($sType==self::$BAR){
45  $this->oChart=new plugin_chartBarSVG($this->iWidth,$this->iHeight);
46  }else{
47  throw new Exception('sType non reconnu, attendu: (PIE,HISTO,LINES,BAR)');
48  }
49  }
50 
56  public function setData($tData){
57  $this->oChart->setData($tData);
58  }
59 
65  public function show(){
66  return $this->oChart->show();
67  }
68 
69  public function addGroup($sLabel,$sColor){
70  $this->oChart->addGroup($sLabel,$sColor);
71  }
72  public function addPoint($x,$y){
73  $this->oChart->addPoint($x,$y);
74  }
75 
76  public function setMarginLeft($x){
77  $this->oChart->setMarginLeft($x);
78  }
79  public function setMaxX($x){
80  $this->oChart->setMaxX($x);
81  }
82  public function setMinX($x){
83  $this->oChart->setMinX($x);
84  }
85  public function setMaxY($x){
86  $this->oChart->setMaxY($x);
87  }
88  public function setMinY($x){
89  $this->oChart->setMinY($x);
90  }
91  public function addMarkerY($y,$color='#ccc'){
92  $this->oChart->addMarkerY($y,$color);
93  }
94  public function setPaddingX($padding){
95  $this->oChart->setPaddingX($padding);
96  }
97  public function setPaddingY($padding){
98  $this->oChart->setPaddingY($padding);
99  }
100  public function setGridY($y,$color){
101  $this->oChart->setGridY($y,$color);
102  }
103  public function setTextSizeLegend($size){
104  $this->oChart->setTextSizeLegend($size);
105  }
106  public function setCoordLegend($x,$y){
107  $this->oChart->setCoordLegend($x,$y);
108  }
109  public function setStepX($stepX){
110  $this->oChart->setStepX($stepX);
111  }
112  public function setStepY($stepY){
113  $this->oChart->setStepY($stepY);
114  }
115 
116 }
118  protected $tData;
119  protected $iWidth;
120  protected $height;
121 
122  protected $id;
123 
124  public static $uid=0;
125 
126  protected $iMax=0;
127 
128  protected $sHtml;
129  protected $sSvg;
130 
131  protected $tColor;
132 
133  protected $iMarginLeft;
134  protected $iMinX;
135  protected $iMaxX;
136  protected $iMinY;
137  protected $iMaxY;
138 
139  protected $tMarkerY=array();
140 
141  protected $paddingX;
142  protected $paddingY;
143 
144  protected $gridY;
145 
146  protected $textsizeLegend;
147 
148  protected $legendX=200;
149  protected $legendY=50;
150 
151  protected $stepX=null;
152  protected $stepY=null;
153 
154  public function __construct($iWidth=null,$iHeight=null){
155  $this->iWidth=$iWidth;
156  $this->iHeight=$iHeight;
157 
158  $this->tColor=array(
159  'green',
160  'blue',
161  'red',
162  );
163 
164  self::$uid+=1;
165 
166  $this->id='canvasPluginChart'.self::$uid;
167 
168  $this->iMarginLeft=0;
169  $this->textsizeLegend=12;
170 
171  $this->sSvg=null;
172  }
173  public function setData($tData){
174  $this->tData=$tData;
175  }
176  public function setColorTab($tColor){
177  $this->tColor=$tColor;
178  }
179  public function setMarginLeft($iMarginLeft){
180  $this->iMarginLeft=$iMarginLeft;
181  }
182  public function setMaxX($iMaxX){
183  $this->iMaxX=$iMaxX;
184  }
185  public function setMinX($iMinX){
186  $this->iMinX=$iMinX;
187  }
188  public function setMaxY($iMaxX){
189  $this->iMaxY=$iMaxX;
190  }
191  public function setMinY($iMinX){
192  $this->iMinY=$iMinX;
193  }
194  public function addMarkerY($y,$color='#444'){
195  $this->tMarkerY[]=array($y,$color);
196  }
197  public function setPaddingX($padding){
198  $this->paddingX=$padding;
199  }
200  public function setPaddingY($padding){
201  $this->paddingY=$padding;
202  }
203  public function setGridY($y,$color){
204  $this->gridY=array($y,$color);
205  }
206  public function setTextSizeLegend($size){
207  $this->textsizeLegend=$size;
208  }
209  public function setCoordLegend($x,$y){
210  $this->legendX=$x;
211  $this->legendY=$y;
212  }
213 
214  public function setStepX($stepX){
215  $this->stepX=$stepX;
216  }
217  public function setStepY($stepY){
218  $this->stepY=$stepY;
219  }
220 
221 
222  public function loadCanvas(){
223 
224  }
225 
226  public function startScript(){
227 
228  $this->sSvg.='<svg width="'.$this->iWidth.'px" height="'.$this->iHeight.'px"> ';
229 
230  $this->sSvg.='<style>
231  .chartRect{
232  cursor:help;
233  }
234  </style>';
235 
236  }
237  public function endScript(){
238  $this->sSvg.='</svg>';
239  }
240 
241  protected function rect($x,$y,$iWidth,$iHeight,$sColor,$alt=null){
242  $this->sSvg.='<rect class="chartRect" id="rect'.$x.$y.'" x="'.$x.'" y="'.$y.'" ';
243  $this->sSvg.='width="'.$iWidth.'" height="'.$iHeight.'" style="fill:'.$sColor.'">';
244  $this->sSvg.='<title>'.$alt.'</title>';
245  $this->sSvg.='</rect>';
246 
247 
248 
249  }
250 
251  protected function partPie($x,$y,$diameter,$degStart,$degEnd,$sColor){
252  $this->sHtml.='context.fillStyle="'.$sColor.'";'."\n";
253  $this->sHtml.='context.beginPath(); '."\n";
254  $this->sHtml.='context.arc('.$x.','.$y.','.$diameter.','.$degStart.','.$degEnd.');'."\n";
255  $this->sHtml.='context.lineTo('.$x.','.$y.');'."\n";
256  $this->sHtml.='context.fill();'."\n";
257 
258 
259 
260  $this->sSvg.='<path d="M'.$x.','.$y.' L10,10 A'.$x+($diameter/2).','.$y+($diameter/2).' 0 0,1 z" ';
261  $this->sSvg.='fill="'.$sColor.'" />';
262  }
263 
264  protected function text($x,$y,$sText,$sColor='black',$font='10px arial'){
265  $this->sHtml.='context.font="'.$font.'";'."\n";
266  $this->sHtml.='context.fillStyle="'.$sColor.'"; '."\n";
267  $this->sHtml.='context.fillText("'.$sText.'",'.$x.','.$y.');'."\n";
268 
269  $this->sSvg.='<text x="'.$x.'" y="'.$y.'" fill="'.$sColor.'">'.$sText.'</text>';
270  }
271 
272  protected function lineFromTo($x,$y,$x2,$y2,$sColor='black',$opacity=1){
273 
274  $this->sSvg.='<line x1="'.$x.'" y1="'.$y.'" x2="'.$x2.'" y2="'.$y2.'" ';
275  $this->sSvg.='style="stroke:'.$sColor.';stroke-width:2" stroke-opacity="'.$opacity.'" />';
276  }
277 }
279 
280  public function show(){
281 
282  $iTotal=0;
283  foreach($this->tData as $tLine){
284  list($sLabel,$iValue)=$tLine;
285 
286  $iTotal+=$iValue;
287  }
288 
289  $this->startScript();
290 
291  $diameter=($this->iWidth/4)-10;
292 
293  $x=$diameter+2;
294  $y=$diameter+2;
295 
296 
297  $degTotal=6.3;
298 
299  $degStart=0;
300 
301  $this->sHtml.='context.beginPath(); '."\n";
302  $this->sHtml.='context.arc('.$x.','.$y.','.$diameter.',0,Math.PI*2);'."\n";
303 
304  $tPct=array();
305 
306  foreach($this->tData as $j => $tLine){
307  list($sLabel,$iValue)=$tLine;
308 
309  $pct=($iValue/$iTotal);
310  $degEnd=$pct*$degTotal;
311  $degEnd+=$degStart;
312 
313  $tPct[$j]=$pct*100;
314 
315  $this->partPie($x,$y,$diameter,$degStart,$degEnd,$this->tColor[$j]);
316 
317  $degStart=$degEnd;
318 
319  }
320 
321  foreach($this->tData as $i => $tLine){
322  list($sLabel,$iValue)=$tLine;
323 
324  $x=$this->legendX;
325  $y=$i*20+$this->legendY;
326 
327  $this->rect($x,$y-8,10,10,$this->tColor[$i]);
328  $this->text($x+16,$y,$sLabel.': '.$tPct[$i].'%','#000',$this->textsizeLegend);
329 
330  }
331 
332  $this->endScript();
333 
334  return "pas encore disponible";
335 
336 
337  }
338 
339 
340 
341 }
343 
344  public function show(){
345 
346  foreach($this->tData as $tLine){
347  list($sLabel,$iValue)=$tLine;
348 
349  if($iValue > $this->iMax){
350  $this->iMax=$iValue;
351  }
352  }
353  $iWidthBar=($this->iWidth-200)/count($this->tData);
354  $iWidthBar=$iWidthBar*0.8;
355 
356  $this->startScript();
357 
358  $j=0;
359  foreach($this->tData as $j=> $tLine){
360  list($sLabel,$iValue)=$tLine;
361 
362  $iHeight=(($iValue/$this->iMax)*($this->iHeight-24));
363 
364  $this->rect($j*($iWidthBar+3),$this->iHeight-$iHeight,($iWidthBar),$iHeight,$this->tColor[$j],$iValue);
365 
366  $j++;
367  }
368 
369  //legend
370  $i=0;
371  foreach($this->tData as $j => $tDetail){
372  $sLabel=$tDetail[0];
373 
374  $x=$this->legendX;
375  $y=$i*20+$this->legendY;
376 
377  $this->rect($x,$y-8,10,10,$this->tColor[$j]);
378  $this->text($x+16,$y,$sLabel,'#000',$this->textsizeLegend);
379 
380  $i++;
381  }
382 
383  $this->lineFromTo(0,0,0,$this->iHeight);
384  $this->lineFromTo(0,$this->iHeight,$this->iWidth-200,$this->iHeight);
385 
386  $this->endScript();
387 
388  return $this->sSvg;
389  }
390 }
392 
393  private $tmpGroup;
394 
395 
396 
397 
398  public function show(){
399 
400  $iMaxX=0;
401  $iMaxY=0;
402 
403  $iMinX='';
404  $iMinY='';
405 
406 
407  if($this->tData){
408  foreach($this->tData as $sGroup => $tDetail){
409  foreach($tDetail['tPoint'] as $tPoint){
410 
411  list($x,$y)=$tPoint;
412 
413  if($iMaxX < $x){
414  $iMaxX=$x;
415  }
416  if($iMaxY < $y){
417  $iMaxY=$y;
418  }
419 
420  if($iMinX=='' or $iMinX > $x){
421  $iMinX=$x;
422  }
423  if($iMinY=='' or $iMinY > $y){
424  $iMinY=$y;
425  }
426 
427  }
428  }
429  }
430 
431 
432  if($this->iMaxX){
433  $iMaxX=$this->iMaxX;
434  }
435  if($this->iMinX){
436  $iMinX=$this->iMinX;
437  }
438  if($this->iMaxY){
439  $iMaxY=$this->iMaxY;
440  }
441  if($this->iMinY!=null){
442  $iMinY=$this->iMinY;
443  }
444 
445  if($this->paddingX ){
446  $iMinX-=$this->paddingX;
447  $iMaxX+=$this->paddingX;
448  }
449  if($this->paddingY ){
450  $iMinY-=$this->paddingY;
451  $iMaxY+=$this->paddingY;
452  }
453 
454  $this->startScript();
455 
456  $iHeight=$this->iHeight-10;
457  $iWidth=$this->iWidth-200-$this->iMarginLeft;
458 
459  if($this->gridY){
460  $step=$this->gridY[0];
461  $color=$this->gridY[1];
462 
463  for($y=$iMinY;$y<$iMaxY;$y+=$step){
464 
465  $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
466  $this->lineFromTo($this->iMarginLeft,$y2,$this->iWidth-200,$y2,$color,0.5 );
467  }
468 
469  }
470 
471  if($this->tMarkerY){
472  foreach($this->tMarkerY as $tLineY){
473 
474  list($y,$color)=$tLineY;
475  $y=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
476 
477  $this->lineFromTo($this->iMarginLeft,$y,$this->iWidth-200,$y,$color,0.5 );
478  }
479  }
480  if($this->tData){
481  foreach($this->tData as $sGroup => $tDetail){
482  $lastX=null;
483  $lastY=null;
484  foreach($tDetail['tPoint'] as $j => $tPoint){
485 
486  list($x,$y)=$tPoint;
487 
488  $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
489  $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
490 
491  $x3=$x2-3;
492  $y3=$y2-3;
493 
494  if($x3<=0){
495  $x3=0;
496  }
497  if($y3<=0){
498  $y3=0;
499  }
500 
501  $this->rect($x3,$y3,6,6,$tDetail['color'],$y);
502 
503  if($j>0){
504  $this->lineFromTo($lastX,$lastY,$x2,$y2,$tDetail['color']);
505 
506  }
507 
508  $lastX=$x2;
509  $lastY=$y2;
510 
511  }
512  }
513  }
514 
515  //legend
516  $i=0;
517  if($this->tData){
518  foreach($this->tData as $sGroup => $tDetail){
519  $sLabel=$sGroup;
520 
521  $x=$this->legendX;
522  $y=$i*20+$this->legendY;
523 
524  $this->rect($x,$y-8,10,10,$tDetail['color']);
525  $this->text($x+16,$y,$sLabel,'#000',$this->textsizeLegend);
526 
527  $i++;
528  }
529  }
530 
531  $this->lineFromTo($this->iMarginLeft,0,$this->iMarginLeft,$this->iHeight-10);
532  $this->lineFromTo($this->iMarginLeft,$this->iHeight-10,$this->iWidth-200,$this->iHeight-10);
533 
534 
535  //step
536  if($this->stepX !== null){
537  for($x=($iMinX);$x<$iMaxX;$x+=$this->stepX){
538  $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
539 
540  $this->lineFromTo($x2,($this->iHeight-10),$x2,($this->iHeight-5) );
541 
542  $this->text($x2+2,($this->iHeight),$x);
543  }
544  }else{
545  $this->text(0,$this->iHeight,$iMinX);
546 
547  $this->text($this->iWidth-200,$this->iHeight,$iMaxX);
548  }
549 
550  //step
551  if($this->stepY !== null){
552  for($y=($iMinY);$y<$iMaxY;$y+=$this->stepY){
553  $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
554 
555  $this->lineFromTo($this->iMarginLeft-5,$y2,$this->iMarginLeft,$y2 );
556 
557  $this->text(0,$y2,$y);
558  }
559  }else{
560  $this->text(0,10,$iMaxY);
561  $this->text(0,$this->iHeight-10 ,$iMinY);
562  }
563 
564 
565  $this->endScript();
566 
567  return $this->sSvg;
568  }
569 
570 
571  public function addGroup($sLabel,$sColor){
572  $this->tmpGroup=$sLabel;
573 
574  $this->tData[$this->tmpGroup]['label']=$sLabel;
575  $this->tData[$this->tmpGroup]['color']=$sColor;
576  }
577  public function addPoint($x,$y){
578  $this->tData[$this->tmpGroup]['tPoint'][]=array($x,$y);
579  }
580 
581 }
583 
584  private $tmpGroup;
585 
586 
587 
588 
589  public function show(){
590  $iMaxX=0;
591  $iMaxY=0;
592 
593  $iMinX='';
594  $iMinY='';
595 
596 
597  if($this->tData){
598  foreach($this->tData as $sGroup => $tDetail){
599  foreach($tDetail['tPoint'] as $tPoint){
600 
601  list($x,$y)=$tPoint;
602 
603  if($iMaxX < $x){
604  $iMaxX=$x;
605  }
606  if($iMaxY < $y){
607  $iMaxY=$y;
608  }
609 
610  if($iMinX=='' or $iMinX > $x){
611  $iMinX=$x;
612  }
613  if($iMinY=='' or $iMinY > $y){
614  $iMinY=$y;
615  }
616 
617  }
618  }
619  }
620 
621  if($this->iMaxX){
622  $iMaxX=$this->iMaxX;
623  }
624  if($this->iMinX){
625  $iMinX=$this->iMinX;
626  }
627  if($this->iMaxY){
628  $iMaxY=$this->iMaxY;
629  }
630  if($this->iMinY!=null){
631  $iMinY=$this->iMinY;
632  }
633 
634  if($this->paddingX ){
635  $iMinX-=$this->paddingX;
636  $iMaxX+=$this->paddingX;
637  }
638  if($this->paddingY ){
639  $iMinY-=$this->paddingY;
640  $iMaxY+=$this->paddingY;
641  }
642 
643  $this->startScript();
644 
645  $iHeight=$this->iHeight-10;
646  $iWidth=$this->iWidth-200-$this->iMarginLeft-(4);
647 
648  if($this->gridY){
649  $step=$this->gridY[0];
650  $color=$this->gridY[1];
651 
652  for($y=$iMinY;$y<$iMaxY;$y+=$step){
653 
654  $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
655  $this->lineFromTo($this->iMarginLeft,$y2,$this->iWidth-200,$y2,$color,0.5 );
656  }
657 
658  }
659 
660  if($this->tMarkerY){
661  foreach($this->tMarkerY as $tLineY){
662 
663  list($y,$color)=$tLineY;
664  $y=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
665 
666  $this->lineFromTo($this->iMarginLeft,$y,$this->iWidth-200,$y,$color,0.5 );
667  }
668  }
669 
670  $k=0;
671  if($this->tData){
672  foreach($this->tData as $sGroup => $tDetail){
673  $lastX=null;
674  $lastY=null;
675  foreach($tDetail['tPoint'] as $tPoint){
676 
677  list($x,$y)=$tPoint;
678 
679  $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
680  $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
681 
682  $x3=$x2;
683  $y3=$y2-3;
684 
685  if($x3<=0){
686  $x3=0;
687  }
688  if($y3<=0){
689  $y3=0;
690  }
691 
692  $this->rect($x3+($k*8),$y3,6,$iHeight-$y3,$tDetail['color'],$y);
693 
694 
695  $lastX=$x2;
696  $lastY=$y2;
697 
698  }
699  $k++;
700  }
701  }
702 
703  //legend
704  $i=0;
705  if($this->tData){
706  foreach($this->tData as $sGroup => $tDetail){
707  $sLabel=$sGroup;
708 
709  $x=$this->legendX;
710  $y=$i*20+$this->legendY;
711 
712  $this->rect($x,$y-8,10,10,$tDetail['color']);
713  $this->text($x+16,$y,$sLabel,'#000',$this->textsizeLegend);
714 
715  $i++;
716  }
717  }
718 
719  $this->lineFromTo($this->iMarginLeft,0,$this->iMarginLeft,$this->iHeight-10);
720  $this->lineFromTo($this->iMarginLeft,$this->iHeight-10,$this->iWidth-200,$this->iHeight-10);
721 
722 
723  //step
724  if($this->stepX !== null){
725  for($x=($iMinX);$x<$iMaxX;$x+=$this->stepX){
726  $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
727 
728  $this->lineFromTo($x2,($this->iHeight-10),$x2,($this->iHeight-5) );
729 
730  $this->text($x2+2,($this->iHeight),$x);
731  }
732  }else{
733  $this->text(0,$this->iHeight,$iMinX);
734 
735  $this->text($this->iWidth-200,$this->iHeight,$iMaxX);
736  }
737 
738  //step
739  if($this->stepY !== null){
740  for($y=($iMinY);$y<$iMaxY;$y+=$this->stepY){
741  $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
742 
743  $this->lineFromTo($this->iMarginLeft-5,$y2,$this->iMarginLeft,$y2 );
744 
745  $this->text(0,$y2,$y);
746  }
747  }else{
748  $this->text(0,10,$iMaxY);
749  $this->text(0,$this->iHeight-10 ,$iMinY);
750  }
751 
752 
753  $this->endScript();
754 
755  return $this->sSvg;
756  }
757 
758 
759  public function addGroup($sLabel,$sColor){
760  $this->tmpGroup=$sLabel;
761 
762  $this->tData[$this->tmpGroup]['label']=$sLabel;
763  $this->tData[$this->tmpGroup]['color']=$sColor;
764  }
765  public function addPoint($x,$y){
766  $this->tData[$this->tmpGroup]['tPoint'][]=array($x,$y);
767  }
768 
769 }