25 public static $PIE=
'PIE';
26 public static $HISTO=
'HISTO';
27 public static $LINES=
'LINES';
28 public static $BAR=
'BAR';
34 public function __construct($sType,$iWidth=null,$iHeight=null){
35 $this->iWidth=$iWidth;
36 $this->iHeight=$iHeight;
38 if($sType==self::$PIE){
40 }
else if($sType==self::$HISTO){
42 }
else if($sType==self::$LINES){
44 }
else if($sType==self::$BAR){
47 throw new Exception(
'sType non reconnu, attendu: (PIE,HISTO,LINES,BAR)');
57 $this->oChart->setData($tData);
66 return $this->oChart->show();
76 $this->oChart->addGroup($sLabel,$sColor);
85 $this->oChart->addPoint($x,$y);
88 public function setMarginLeft($x){
89 $this->oChart->setMarginLeft($x);
91 public function setMaxX($x){
92 $this->oChart->setMaxX($x);
94 public function setMinX($x){
95 $this->oChart->setMinX($x);
97 public function setMaxY($x){
98 $this->oChart->setMaxY($x);
100 public function setMinY($x){
101 $this->oChart->setMinY($x);
103 public function addMarkerY($y,$color=
'#ccc'){
104 $this->oChart->addMarkerY($y,$color);
106 public function setPaddingX($padding){
107 $this->oChart->setPaddingX($padding);
109 public function setPaddingY($padding){
110 $this->oChart->setPaddingY($padding);
112 public function setGridY($y,$color){
113 $this->oChart->setGridY($y,$color);
115 public function setTextSizeLegend($size){
116 $this->oChart->setTextSizeLegend($size);
118 public function setCoordLegend($x,$y){
119 $this->oChart->setCoordLegend($x,$y);
121 public function setStepX($stepX){
122 $this->oChart->setStepX($stepX);
124 public function setStepY($stepY){
125 $this->oChart->setStepY($stepY);
136 public static $uid=0;
144 protected $iMarginLeft;
150 protected $tMarkerY=array();
157 protected $textsizeLegend;
159 protected $legendX=200;
160 protected $legendY=50;
162 protected $stepX=null;
163 protected $stepY=null;
165 public function __construct($iWidth=null,$iHeight=null){
166 $this->iWidth=$iWidth;
167 $this->iHeight=$iHeight;
177 $this->
id=
'canvasPluginChart'.self::$uid;
179 $this->iMarginLeft=0;
180 $this->textsizeLegend=12;
182 public function setData($tData){
185 public function setColorTab($tColor){
186 $this->tColor=$tColor;
188 public function setMarginLeft($iMarginLeft){
189 $this->iMarginLeft=$iMarginLeft;
191 public function setMaxX($iMaxX){
194 public function setMinX($iMinX){
197 public function setMaxY($iMaxX){
200 public function setMinY($iMinX){
203 public function addMarkerY($y,$color=
'#444'){
204 $this->tMarkerY[]=array($y,$color);
206 public function setPaddingX($padding){
207 $this->paddingX=$padding;
209 public function setPaddingY($padding){
210 $this->paddingY=$padding;
212 public function setGridY($y,$color){
213 $this->gridY=array($y,$color);
215 public function setTextSizeLegend($size){
216 $this->textsizeLegend=$size;
218 public function setCoordLegend($x,$y){
223 public function setStepX($stepX){
226 public function setStepY($stepY){
231 public function loadCanvas(){
232 $this->sHtml.=
'<canvas id="'.$this->id.
'" width="'.$this->iWidth.
'px" height="'.$this->iHeight.
'px" ></canvas>';
234 $this->startScript();
236 $this->sHtml.=
'var canvas = document.getElementById("'.$this->id.
'"); ';
237 $this->sHtml.=
'var context = canvas.getContext("2d")';
242 public function startScript(){
243 $this->sHtml.=
'<script>';
245 public function endScript(){
246 $this->sHtml.=
'</script>';
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";
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";
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";
270 protected function lineFromTo($x,$y,$x2,$y2,$sColor=
'black',$opacity=1){
272 $this->sHtml.=
'context.globalAlpha='.$opacity.
';'.
"\n";
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";
280 $this->sHtml.=
'context.globalAlpha=1;'.
"\n";
285 public function show(){
291 foreach($this->tData as $tLine){
292 list($sLabel,$iValue)=$tLine;
297 $this->startScript();
299 $diameter=($this->iWidth/4)-10;
309 $this->sHtml.=
'context.beginPath(); '.
"\n";
310 $this->sHtml.=
'context.arc('.$x.
','.$y.
','.$diameter.
',0,Math.PI*2);'.
"\n";
314 foreach($this->tData as $j => $tLine){
315 list($sLabel,$iValue)=$tLine;
317 $pct=($iValue/$iTotal);
318 $degEnd=$pct*$degTotal;
323 $this->partPie($x,$y,$diameter,$degStart,$degEnd,$this->tColor[$j]);
329 foreach($this->tData as $i => $tLine){
330 list($sLabel,$iValue)=$tLine;
333 $y=$i*20+$this->legendY;
335 $this->rect($x,$y-8,10,10,$this->tColor[$i]);
336 $this->text($x+16,$y,$sLabel.
': '.$tPct[$i].
'%',
'#000',$this->textsizeLegend);
350 public function show(){
353 foreach($this->tData as $tLine){
354 list($sLabel,$iValue)=$tLine;
356 if($iValue > $this->iMax){
360 $iWidthBar=($this->iWidth-200)/count($this->tData);
361 $iWidthBar=$iWidthBar*0.8;
363 $this->startScript();
368 foreach($this->tData as $j=> $tLine){
369 list($sLabel,$iValue)=$tLine;
371 $iHeight=1-(($iValue/$this->iMax)*($this->iHeight-24));
373 $this->rect($j*($iWidthBar+3),$this->iHeight,($iWidthBar),$iHeight,$this->tColor[$j]);
380 foreach($this->tData as $j => $tDetail){
384 $y=$i*20+$this->legendY;
386 $this->rect($x,$y-8,10,10,$this->tColor[$j]);
387 $this->text($x+16,$y,$sLabel,
'#000',$this->textsizeLegend);
392 $this->lineFromTo(0,0,0,$this->iHeight);
393 $this->lineFromTo(0,$this->iHeight,$this->iWidth-200,$this->iHeight);
407 public function show(){
418 foreach($this->tData as $sGroup => $tDetail){
419 foreach($tDetail[
'tPoint'] as $tPoint){
430 if($iMinX==
'' or $iMinX > $x){
433 if($iMinY==
'' or $iMinY > $y){
450 if($this->iMinY!=null){
454 if($this->paddingX ){
455 $iMinX-=$this->paddingX;
456 $iMaxX+=$this->paddingX;
458 if($this->paddingY ){
459 $iMinY-=$this->paddingY;
460 $iMaxY+=$this->paddingY;
463 $this->startScript();
465 $iHeight=$this->iHeight-10;
466 $iWidth=$this->iWidth-200-$this->iMarginLeft;
469 $step=$this->gridY[0];
470 $color=$this->gridY[1];
472 for($y=$iMinY;$y<$iMaxY;$y+=$step){
474 $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
475 $this->lineFromTo($this->iMarginLeft,$y2,$this->iWidth-200,$y2,$color,0.5 );
481 foreach($this->tMarkerY as $tLineY){
483 list($y,$color)=$tLineY;
484 $y=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
486 $this->lineFromTo($this->iMarginLeft,$y,$this->iWidth-200,$y,$color,0.5 );
490 foreach($this->tData as $sGroup => $tDetail){
493 foreach($tDetail[
'tPoint'] as $j => $tPoint){
497 $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
498 $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
510 $this->rect($x3,$y3,6,6,$tDetail[
'color']);
513 $this->lineFromTo($lastX,$lastY,$x2,$y2,$tDetail[
'color']);
526 foreach($this->tData as $sGroup => $tDetail){
530 $y=$i*20+$this->legendY;
532 $this->rect($x,$y-8,10,10,$tDetail[
'color']);
533 $this->text($x+16,$y,$sLabel,
'#000',$this->textsizeLegend);
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);
544 if($this->stepX !== null){
545 for($x=($iMinX);$x<$iMaxX;$x+=$this->stepX){
546 $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
548 $this->lineFromTo($x2,($this->iHeight-10),$x2,($this->iHeight-5) );
550 $this->text($x2+2,($this->iHeight),$x);
553 $this->text(0,$this->iHeight,$iMinX);
555 $this->text($this->iWidth-200,$this->iHeight,$iMaxX);
559 if($this->stepY !== null){
560 for($y=($iMinY);$y<$iMaxY;$y+=$this->stepY){
561 $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
563 $this->lineFromTo($this->iMarginLeft-5,$y2,$this->iMarginLeft,$y2 );
565 $this->text(0,$y2,$y);
568 $this->text(0,10,$iMaxY);
569 $this->text(0,$this->iHeight-10 ,$iMinY);
579 public function addGroup($sLabel,$sColor){
580 $this->tmpGroup=$sLabel;
582 $this->tData[$this->tmpGroup][
'label']=$sLabel;
583 $this->tData[$this->tmpGroup][
'color']=$sColor;
585 public function addPoint($x,$y){
586 $this->tData[$this->tmpGroup][
'tPoint'][]=array($x,$y);
597 public function show(){
608 foreach($this->tData as $sGroup => $tDetail){
609 foreach($tDetail[
'tPoint'] as $tPoint){
620 if($iMinX==
'' or $iMinX > $x){
623 if($iMinY==
'' or $iMinY > $y){
640 if($this->iMinY!=null){
644 if($this->paddingX ){
645 $iMinX-=$this->paddingX;
646 $iMaxX+=$this->paddingX;
648 if($this->paddingY ){
649 $iMinY-=$this->paddingY;
650 $iMaxY+=$this->paddingY;
653 $this->startScript();
655 $iHeight=$this->iHeight-10;
656 $iWidth=$this->iWidth-200-$this->iMarginLeft-(4);
659 $step=$this->gridY[0];
660 $color=$this->gridY[1];
662 for($y=$iMinY;$y<$iMaxY;$y+=$step){
664 $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
665 $this->lineFromTo($this->iMarginLeft,$y2,$this->iWidth-200,$y2,$color,0.5 );
671 foreach($this->tMarkerY as $tLineY){
673 list($y,$color)=$tLineY;
674 $y=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
676 $this->lineFromTo($this->iMarginLeft,$y,$this->iWidth-200,$y,$color,0.5 );
682 foreach($this->tData as $sGroup => $tDetail){
685 foreach($tDetail[
'tPoint'] as $tPoint){
689 $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
690 $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
702 $this->rect($x3+($k*8),$y3,6,$iHeight-$y3,$tDetail[
'color']);
715 foreach($this->tData as $sGroup => $tDetail){
719 $y=$i*20+$this->legendY;
721 $this->rect($x,$y-8,10,10,$tDetail[
'color']);
722 $this->text($x+16,$y,$sLabel,
'#000',$this->textsizeLegend);
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);
732 if($this->stepX !== null){
733 for($x=($iMinX);$x<$iMaxX;$x+=$this->stepX){
734 $x2=(($x-$iMinX)/($iMaxX-$iMinX))*$iWidth+$this->iMarginLeft;
736 $this->lineFromTo($x2,($this->iHeight-10),$x2,($this->iHeight-5) );
738 $this->text($x2+2,($this->iHeight),$x);
741 $this->text(0,$this->iHeight,$iMinX);
743 $this->text($this->iWidth-200,$this->iHeight,$iMaxX);
747 if($this->stepY !== null){
748 for($y=($iMinY);$y<$iMaxY;$y+=$this->stepY){
749 $y2=(1-($y-$iMinY)/($iMaxY-$iMinY))*$iHeight;
751 $this->lineFromTo($this->iMarginLeft-5,$y2,$this->iMarginLeft,$y2 );
753 $this->text(0,$y2,$y);
756 $this->text(0,10,$iMaxY);
757 $this->text(0,$this->iHeight-10 ,$iMinY);
767 public function addGroup($sLabel,$sColor){
768 $this->tmpGroup=$sLabel;
770 $this->tData[$this->tmpGroup][
'label']=$sLabel;
771 $this->tData[$this->tmpGroup][
'color']=$sColor;
773 public function addPoint($x,$y){
774 $this->tData[$this->tmpGroup][
'tPoint'][]=array($x,$y);
addGroup($sLabel, $sColor)