[2] | 1 | <?php
|
---|
| 2 | /*=======================================================================
|
---|
[267] | 3 | // File: JPGRAPH_STOCK.PHP
|
---|
| 4 | // Description: Stock plot extension for JpGraph
|
---|
| 5 | // Created: 2003-01-27
|
---|
| 6 | // Ver: $Id: jpgraph_stock.php 957 2007-12-01 14:00:29Z ljp $
|
---|
| 7 | //
|
---|
| 8 | // Copyright (c) Aditus Consulting. All rights reserved.
|
---|
| 9 | //========================================================================
|
---|
| 10 | */
|
---|
[2] | 11 |
|
---|
| 12 | //===================================================
|
---|
| 13 | // CLASS StockPlot
|
---|
| 14 | //===================================================
|
---|
| 15 | class StockPlot extends Plot {
|
---|
| 16 | protected $iTupleSize = 4;
|
---|
| 17 | private $iWidth=9;
|
---|
| 18 | private $iEndLines=1;
|
---|
| 19 | private $iStockColor1='white',$iStockColor2='darkred',$iStockColor3='darkred';
|
---|
[267] | 20 | //---------------
|
---|
| 21 | // CONSTRUCTOR
|
---|
| 22 | function StockPlot($datay,$datax=false) {
|
---|
| 23 | if( count($datay) % $this->iTupleSize ) {
|
---|
| 24 | JpGraphError::RaiseL(21001,$this->iTupleSize);
|
---|
| 25 | //('Data values for Stock charts must contain an even multiple of '.$this->iTupleSize.' data points.');
|
---|
| 26 | }
|
---|
| 27 | $this->Plot($datay,$datax);
|
---|
| 28 | $this->numpoints /= $this->iTupleSize;
|
---|
[2] | 29 | }
|
---|
[267] | 30 | //---------------
|
---|
| 31 | // PUBLIC METHODS
|
---|
| 32 |
|
---|
[2] | 33 | function SetColor($aColor,$aColor1='white',$aColor2='darkred',$aColor3='darkred') {
|
---|
[267] | 34 | $this->color = $aColor;
|
---|
| 35 | $this->iStockColor1 = $aColor1;
|
---|
| 36 | $this->iStockColor2 = $aColor2;
|
---|
| 37 | $this->iStockColor3 = $aColor3;
|
---|
[2] | 38 | }
|
---|
| 39 |
|
---|
| 40 | function SetWidth($aWidth) {
|
---|
[267] | 41 | // Make sure it's odd
|
---|
| 42 | $this->iWidth = 2*floor($aWidth/2)+1;
|
---|
[2] | 43 | }
|
---|
| 44 |
|
---|
| 45 | function HideEndLines($aHide=true) {
|
---|
[267] | 46 | $this->iEndLines = !$aHide;
|
---|
[2] | 47 | }
|
---|
| 48 |
|
---|
| 49 | // Gets called before any axis are stroked
|
---|
| 50 | function PreStrokeAdjust($graph) {
|
---|
[267] | 51 | if( $this->center ) {
|
---|
| 52 | $a=0.5; $b=0.5;
|
---|
| 53 | $this->numpoints++;
|
---|
| 54 | } else {
|
---|
| 55 | $a=0; $b=0;
|
---|
| 56 | }
|
---|
| 57 | $graph->xaxis->scale->ticks->SetXLabelOffset($a);
|
---|
| 58 | $graph->SetTextScaleOff($b);
|
---|
[2] | 59 | }
|
---|
[267] | 60 |
|
---|
[2] | 61 | // Method description
|
---|
| 62 | function Stroke($img,$xscale,$yscale) {
|
---|
[267] | 63 | $n=$this->numpoints;
|
---|
| 64 | if( $this->center ) $n--;
|
---|
| 65 | if( isset($this->coords[1]) ) {
|
---|
| 66 | if( count($this->coords[1])!=$n )
|
---|
| 67 | JpGraphError::RaiseL(2003,count($this->coords[1]),$n);
|
---|
| 68 | //("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])." Number of Y-points:$numpoints");
|
---|
| 69 | else
|
---|
| 70 | $exist_x = true;
|
---|
| 71 | }
|
---|
| 72 | else
|
---|
| 73 | $exist_x = false;
|
---|
[2] | 74 |
|
---|
[267] | 75 | if( $exist_x )
|
---|
| 76 | $xs=$this->coords[1][0];
|
---|
| 77 | else
|
---|
| 78 | $xs=0;
|
---|
| 79 |
|
---|
| 80 | $ts = $this->iTupleSize;
|
---|
| 81 | $this->csimareas = '';
|
---|
| 82 | for( $i=0; $i<$n; ++$i) {
|
---|
[2] | 83 |
|
---|
[267] | 84 | //If value is NULL, then don't draw a bar at all
|
---|
| 85 | if ($this->coords[0][$i] === null) continue;
|
---|
[2] | 86 |
|
---|
[267] | 87 | if( $exist_x ) $x=$this->coords[1][$i];
|
---|
| 88 | else $x=$i;
|
---|
| 89 | $xt = $xscale->Translate($x);
|
---|
| 90 |
|
---|
| 91 | $neg = $this->coords[0][$i*$ts] > $this->coords[0][$i*$ts+1] ;
|
---|
| 92 | $yopen = $yscale->Translate($this->coords[0][$i*$ts]);
|
---|
| 93 | $yclose = $yscale->Translate($this->coords[0][$i*$ts+1]);
|
---|
| 94 | $ymin = $yscale->Translate($this->coords[0][$i*$ts+2]);
|
---|
| 95 | $ymax = $yscale->Translate($this->coords[0][$i*$ts+3]);
|
---|
[2] | 96 |
|
---|
[267] | 97 | $dx = floor($this->iWidth/2);
|
---|
| 98 | $xl = $xt - $dx;
|
---|
| 99 | $xr = $xt + $dx;
|
---|
[2] | 100 |
|
---|
[267] | 101 | if( $neg )
|
---|
| 102 | $img->SetColor($this->iStockColor3);
|
---|
| 103 | else
|
---|
| 104 | $img->SetColor($this->iStockColor1);
|
---|
| 105 | $img->FilledRectangle($xl,$yopen,$xr,$yclose);
|
---|
| 106 | $img->SetLineWeight($this->weight);
|
---|
| 107 | if( $neg )
|
---|
| 108 | $img->SetColor($this->iStockColor2);
|
---|
| 109 | else
|
---|
| 110 | $img->SetColor($this->color);
|
---|
| 111 |
|
---|
| 112 | $img->Rectangle($xl,$yopen,$xr,$yclose);
|
---|
[2] | 113 |
|
---|
[267] | 114 | if( $yopen < $yclose ) {
|
---|
| 115 | $ytop = $yopen ;
|
---|
| 116 | $ybottom = $yclose ;
|
---|
| 117 | }
|
---|
| 118 | else {
|
---|
| 119 | $ytop = $yclose ;
|
---|
| 120 | $ybottom = $yopen ;
|
---|
| 121 | }
|
---|
| 122 | $img->SetColor($this->color);
|
---|
| 123 | $img->Line($xt,$ytop,$xt,$ymax);
|
---|
| 124 | $img->Line($xt,$ybottom,$xt,$ymin);
|
---|
[2] | 125 |
|
---|
[267] | 126 | if( $this->iEndLines ) {
|
---|
| 127 | $img->Line($xl,$ymax,$xr,$ymax);
|
---|
| 128 | $img->Line($xl,$ymin,$xr,$ymin);
|
---|
| 129 | }
|
---|
[2] | 130 |
|
---|
[267] | 131 | // A chance for subclasses to add things to the bar
|
---|
| 132 | // for data point i
|
---|
| 133 | $this->ModBox($img,$xscale,$yscale,$i,$xl,$xr,$neg);
|
---|
[2] | 134 |
|
---|
[267] | 135 | // Setup image maps
|
---|
| 136 | if( !empty($this->csimtargets[$i]) ) {
|
---|
| 137 | $this->csimareas.= '<area shape="rect" coords="'.
|
---|
| 138 | round($xl).','.round($ytop).','.
|
---|
| 139 | round($xr).','.round($ybottom).'" ';
|
---|
| 140 | $this->csimareas .= ' href="'.$this->csimtargets[$i].'"';
|
---|
| 141 | if( !empty($this->csimalts[$i]) ) {
|
---|
| 142 | $sval=$this->csimalts[$i];
|
---|
| 143 | $this->csimareas .= " title=\"$sval\" alt=\"$sval\" ";
|
---|
| 144 | }
|
---|
| 145 | $this->csimareas.= " />\n";
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | return true;
|
---|
[2] | 149 | }
|
---|
| 150 |
|
---|
| 151 | // A hook for subclasses to modify the plot
|
---|
| 152 | function ModBox($img,$xscale,$yscale,$i,$xl,$xr,$neg) {}
|
---|
| 153 |
|
---|
| 154 | } // Class
|
---|
| 155 |
|
---|
| 156 | //===================================================
|
---|
| 157 | // CLASS BoxPlot
|
---|
| 158 | //===================================================
|
---|
| 159 | class BoxPlot extends StockPlot {
|
---|
| 160 | private $iPColor='black',$iNColor='white';
|
---|
[267] | 161 | function BoxPlot($datay,$datax=false) {
|
---|
| 162 | $this->iTupleSize=5;
|
---|
| 163 | parent::StockPlot($datay,$datax);
|
---|
[2] | 164 | }
|
---|
| 165 |
|
---|
| 166 | function SetMedianColor($aPos,$aNeg) {
|
---|
[267] | 167 | $this->iPColor = $aPos;
|
---|
| 168 | $this->iNColor = $aNeg;
|
---|
[2] | 169 | }
|
---|
| 170 |
|
---|
| 171 | function ModBox($img,$xscale,$yscale,$i,$xl,$xr,$neg) {
|
---|
[267] | 172 | if( $neg )
|
---|
| 173 | $img->SetColor($this->iNColor);
|
---|
| 174 | else
|
---|
| 175 | $img->SetColor($this->iPColor);
|
---|
| 176 |
|
---|
| 177 | $y = $yscale->Translate($this->coords[0][$i*5+4]);
|
---|
| 178 | $img->Line($xl,$y,$xr,$y);
|
---|
[2] | 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /* EOF */
|
---|
[267] | 183 | ?>
|
---|