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