2 /*=======================================================================
3 // File: JPGRAPH_STOCK.PHP
4 // Description: Stock plot extension for JpGraph
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_stock.php,v 1.5 2003/02/10 16:38:04 aditus Exp $
9 // License: This code is released under QPL
10 // Copyright (C) 2003 Johan Persson
11 //========================================================================
14 //===================================================
16 //===================================================
17 class StockPlot extends Plot {
20 var $iStockColor1='white',$iStockColor2='darkred',$iStockColor3='darkred';
24 function StockPlot(&$datay,$datax=false) {
25 if( count($datay) % $this->iTupleSize ) {
26 JpGraphError::Raise('Data values for Stock charts must contain an even multiple of '.$this->iTupleSize.' data points.');
28 $this->Plot($datay,$datax);
29 $this->numpoints /= $this->iTupleSize;
34 function SetColor($aColor,$aColor1='white',$aColor2='darkred',$aColor3='darkred') {
35 $this->color = $aColor;
36 $this->iStockColor1 = $aColor1;
37 $this->iStockColor2 = $aColor2;
38 $this->iStockColor3 = $aColor3;
41 function SetWidth($aWidth) {
43 $this->iWidth = 2*floor($aWidth/2)+1;
46 function HideEndLines($aHide=true) {
47 $this->iEndLines = !$aHide;
50 // Gets called before any axis are stroked
51 function PreStrokeAdjust(&$graph) {
58 $graph->xaxis->scale->ticks->SetXLabelOffset($a);
59 $graph->SetTextScaleOff($b);
63 function Stroke($img,$xscale,$yscale) {
65 if( $this->center ) $n--;
66 if( isset($this->coords[1]) ) {
67 if( count($this->coords[1])!=$n )
68 JpGraphError::Raise("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])." Number of Y-points:$numpoints");
76 $xs=$this->coords[1][0];
80 $ts = $this->iTupleSize;
81 $this->csimareas = '';
82 for( $i=0; $i<$n; ++$i) {
84 //If value is NULL, then don't draw a bar at all
85 if ($this->coords[0][$i] === null) continue;
87 if( $exist_x ) $x=$this->coords[1][$i];
89 $xt = $xscale->Translate($x);
91 $neg = $this->coords[0][$i*4] > $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]);
97 $dx = floor($this->iWidth/2);
102 $img->SetColor($this->iStockColor3);
104 $img->SetColor($this->iStockColor1);
105 $img->FilledRectangle($xl,$yopen,$xr,$yclose);
106 $img->SetLineWeight($this->weight);
108 $img->SetColor($this->iStockColor2);
110 $img->SetColor($this->color);
112 $img->Rectangle($xl,$yopen,$xr,$yclose);
114 if( $yopen < $yclose ) {
122 $img->SetColor($this->color);
123 $img->Line($xt,$ytop,$xt,$ymax);
124 $img->Line($xt,$ybottom,$xt,$ymin);
126 if( $this->iEndLines ) {
127 $img->Line($xl,$ymax,$xr,$ymax);
128 $img->Line($xl,$ymin,$xr,$ymin);
131 // A chance for subclasses to add things to the bar
133 $this->ModBox($img,$xscale,$yscale,$i,$xl,$xr,$neg);
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 .= " alt=\"$sval\" title=\"$sval\" ";
145 $this->csimareas.= ">\n";
151 // A hook for subclasses to modify the plot
152 function ModBox($img,$xscale,$yscale,$i,$xl,$xr,$neg) {}
156 //===================================================
158 //===================================================
159 class BoxPlot extends StockPlot {
160 var $iPColor='black',$iNColor='white';
161 function BoxPlot($datay,$datax=false) {
163 parent::StockPlot($datay,$datax);
166 function SetMedianColor($aPos,$aNeg) {
167 $this->iPColor = $aPos;
168 $this->iNColor = $aNeg;
171 function ModBox($img,$xscale,$yscale,$i,$xl,$xr,$neg) {
173 $img->SetColor($this->iNColor);
175 $img->SetColor($this->iPColor);
177 $y = $yscale->Translate($this->coords[0][$i*5+4]);
178 $img->Line($xl,$y,$xr,$y);