~pablocapeluto/cds-php/devel-3.1

« back to all changes in this revision

Viewing changes to jpgraph/src/jpgraph_stock.php

  • Committer: pcapeluto at gmail
  • Date: 2010-08-20 17:51:08 UTC
  • Revision ID: pcapeluto@gmail.com-20100820175108-jyi8dbyj15uy9p4i
Initial import

Show diffs side-by-side

added added

removed removed

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