source: temp/trunk/html/admin/total/class/SC_GraphLine.php @ 5316

Revision 5316, 8.2 KB checked in by kakinaka, 20 years ago (diff)

blank

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?
2$SC_GRAPHLINE_DIR = realpath(dirname( __FILE__));
3require_once($SC_GRAPHLINE_DIR . "/SC_GraphBase.php"); 
4
5// ÀÞ¤ìÀþ¥°¥é¥ÕÀ¸À®¥¯¥é¥¹
6class SC_GraphLine extends SC_GraphBase{
7    var $area_width;
8    var $area_height;
9    var $ygrid_on;
10    var $graph_max;     // ¥°¥é¥Õ¤Î¥¨¥ê¥¢ºÇÂçÃÍ(Y¼´ÄºÅÀ¤ÎÃÍ)
11    var $arrXLabel;
12    var $XLabelAngle;   // X¼´¥é¥Ù¥ë³ÑÅÙ   
13    var $XTitle;        // X¼´¥¿¥¤¥È¥ë
14    var $YTitle;        // Y¼´¥¿¥¤¥È¥ë
15    var $arrDataList;   // ¥°¥é¥Õ¥Ç¡¼¥¿¤ò³ÊǼ
16    var $arrPointList;  // ÀÞ¤ìÀþºÂɸ¤ò³ÊǼ
17    var $line_max;      // Ê£¿ô¤ÎÉÁ²è¤Î¾ì¹ç¤Ë²Ã»»¤·¤Æ¤¤¤¯
18   
19    var $x_margin;
20    var $y_margin;
21           
22    // ¥³¥ó¥¹¥È¥é¥¯¥¿
23    function SC_GraphLine(
24        $bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = LINE_LEFT, $top = LINE_TOP,
25        $area_width = LINE_AREA_WIDTH, $area_height = LINE_AREA_HEIGHT) {
26        parent::SC_GraphBase($bgw, $bgh, $left, $top); 
27        $this->area_width = $area_width;
28        $this->area_height = $area_height;
29        $this->ygrid_on = true;
30        $this->line_max = 0;
31        $this->graph_max = 0;
32        $this->XLabelAngle = 0;
33        $this->x_margin = 0;
34        $this->y_margin = 0;
35    }
36   
37    // X¼´¥é¥Ù¥ë¤Î³ÑÅÙ¥»¥Ã¥È
38    function setXLabelAngle($Angle) {
39        $this->XLabelAngle = $Angle;
40    }
41   
42    // Y¼´¥¿¥¤¥È¥ë
43    function drawYTitle() {
44        // Y¼´¤Ë¥¿¥¤¥È¥ë¤òÆþ¤ì¤ë
45        if($this->YTitle != "") {
46            $text_width = $this->getTextWidth($this->YTitle, FONT_SIZE);
47            $x_pos = $this->left - ($text_width / 2);
48            $y_pos = $this->top - FONT_SIZE - LINE_YTITLE_PAD;     
49            $this->setText(FONT_SIZE, $x_pos, $y_pos, $this->YTitle);
50        }
51    }
52   
53    // X¼´¥¿¥¤¥È¥ë
54    function drawXTitle() {
55        // Y¼´¤Ë¥¿¥¤¥È¥ë¤òÆþ¤ì¤ë
56        if($this->XTitle != "") {
57            $text_width = $this->getTextWidth($this->XTitle, FONT_SIZE);
58            $x_pos = $this->left ;//+ $this->area_width - ($text_width / 2);
59            $y_pos = $this->top ;//s+ $this->area_height + LINE_XTITLE_PAD;
60            $this->setText(FONT_SIZE, $x_pos, $y_pos, $this->XTitle);
61        }
62    }
63   
64    // Y¼´¤ÎÉÁ²è
65    function drawYLine() {
66        imageline($this->image, $this->left, $this->top, $this->left, $this->top + $this->area_height, $this->flame_color);
67        // ÌÜÀ¹¤êÉý¤òµá¤á¤ë(Ãæ´ÖÅÀ¤Ï¼«Æ°)
68        $size = $this->area_height / (LINE_Y_SCALE * 2);
69        // ¾å¤«¤éÌÜÀ¹¤ê¤òÆþ¤ì¤Æ¤¤¤¯
70        $pos = 0;
71        for($i = 0; $i < (LINE_Y_SCALE * 2); $i++) {
72            // ÌÜÀ¹¤êÉý
73            if(($i % 2) == 0) {
74                $sw = LINE_SCALE_SIZE;
75                if($this->ygrid_on) {
76                    imageline($this->image, $this->left, $this->top + $pos, $this->left + $this->area_width, $this->top + $pos, $this->grid_color);
77                }
78            } else {
79                $sw = LINE_SCALE_SIZE / 2;
80            }
81            imageline($this->image, $this->left, $this->top + $pos, $this->left + $sw, $this->top + $pos, $this->flame_color);
82            $pos += $size;
83        }
84        // Y¼´¤ËÌÜÀ¹¤êÃͤòÆþ¤ì¤ë
85        $this->setYScale();
86        $this->drawYTitle();   
87    }
88   
89    // X¼´¤ÎÉÁ²è
90    function drawXLine($bar = false) {
91        imageline($this->image, $this->left, $this->top + $this->area_height, $this->left + $this->area_width, $this->top + $this->area_height, $this->flame_color);
92        $arrPointList = $this->arrPointList[0];
93        $count = count($arrPointList);
94       
95        // ËÀ¥°¥é¥Õ¤Î¾ì¹ç¤ÏȾÌÜÀ¹¤ê¤º¤é¤¹
96        if($bar) {
97            $half_scale = intval($this->area_width / ($count + 1) / 2);
98        } else {
99            $half_scale = 0;
100        }
101       
102        // ¥é¥Ù¥ë¤Îɽ¼¨¥¤¥ó¥¿¡¼¥Ð¥ë¤ò»»½Ð
103        $interval = ceil($count / LINE_XLABEL_MAX); // ÀÚ¤ê¾å¤²             
104        for($i = 0; $i < $count; $i++) {
105            // X¼´¤ËÌÜÀ¹¤ê¤òÆþ¤ì¤ë
106            $x = $arrPointList[$i][0];
107            $pos = $this->top + $this->area_height;
108            imageline($this->image, $x - $half_scale, $pos, $x - $half_scale, $pos - LINE_SCALE_SIZE,  $this->flame_color);         
109            // ¥é¥Ù¥ë¤òÆþ¤ì¤ë
110            if(($i % $interval) == 0) {
111                $text_width = $this->getTextWidth($this->arrXLabel[$i], FONT_SIZE);
112                $x_pos = $x - ($text_width / 2);
113               
114                if ($this->XLabelAngle == "-45"){
115                    $this->x_margin = - $text_width / 4;
116                    $this->y_margin = $text_width / 2 + 5;
117                }
118               
119                $this->setText(FONT_SIZE, $x_pos + $this->x_margin, $pos + FONT_SIZE + $this->y_margin, $this->arrXLabel[$i], NULL, $this->XLabelAngle);
120            }
121        }
122       
123        // ËÀ¥°¥é¥Õ¤Î¾ì¹ç¤ÏºÇ¸å¤ÎÌÜÀ¹¤ê¤ò°ì¤ÄÄɲ乤ë
124        if($bar) {
125            imageline($this->image, $x + $half_scale, $pos, $x + $half_scale, $pos - LINE_SCALE_SIZE,  $this->flame_color);
126        }
127       
128        $this->drawXTitle();
129    }
130       
131    // ¥°¥ê¥Ã¥Éɽ¼¨
132    function setYGridOn($ygrid_on) {
133        $this->ygrid_on = $ygrid_on;
134    }
135   
136    // ¥Ý¥¤¥ó¥È¤ÎÉÁ²è
137    function setMark($line_no, $left, $top, $size = LINE_MARK_SIZE) {
138        // ¶ö¿ô¤ËÊÑ´¹¤·¤Æ¤ª¤¯
139        $size += $size % 2;
140        $array = array(
141            $left, $top - ($size / 2),
142            $left + ($size / 2), $top,
143            $left, $top + ($size / 2),
144            $left - ($size / 2), $top,
145        );     
146        imagefilledpolygon($this->image, $array, 4, $this->arrColor[$line_no]);
147        imagepolygon($this->image, $array, 4, $this->flame_color);
148        imagesetpixel ($this->image, $left, $top + ($size / 2), $this->flame_color);
149    }   
150   
151    // Y¼´ÌÜÀ¹¤ê¤ËÃͤòÆþ¤ì¤ë
152    function setYScale() {
153        // 1ÌÜÀ¹¤ê¤ÎÃÍ
154        $number = intval($this->graph_max / LINE_Y_SCALE);             
155        // ÌÜÀ¹¤êÉý¤òµá¤á¤ë
156        $size = $this->area_height / LINE_Y_SCALE;
157        $pos = 0;
158        for($i = 0; $i <= LINE_Y_SCALE; $i++) {
159            $snumber = $number * (LINE_Y_SCALE - $i);
160            $disp_number = number_format($snumber);
161            $num_width = $this->getTextWidth($disp_number, FONT_SIZE);
162            $this->setText(FONT_SIZE, $this->left - $num_width - 2, $this->top + $pos - (FONT_SIZE / 2), $disp_number);
163            $pos += $size;
164        }
165    }
166   
167    //
168    function setMax($arrData) {
169        // ¥Ç¡¼¥¿¤ÎºÇÂçÃͤò¼èÆÀ¤¹¤ë¡£
170        $data_max = max($arrData);
171        // 10¤Î²¿Çܤ«¤ò¼èÆÀ
172        $figure = strlen($data_max) - 1;
173        // ¼¡¤Î·å¤ò·×»»¤¹¤ë
174        $tenval = pow(10, $figure);
175        // ¥°¥é¥Õ¾å¤Ç¤ÎºÇÂçÃͤòµá¤á¤ë
176        $this->graph_max = $tenval * (intval($data_max / $tenval) + 1);
177        // ºÇÂçÃͤ¬10̤Ëþ¤Î¾ì¹ç¤ÎÂбþ
178        if($this->graph_max < 10) {
179            $this->graph_max = 10;
180        }   
181    }
182   
183    // ¥°¥é¥Õ¤ÎÉÁ²è
184    function drawGraph() {
185        // ¥°¥é¥ÕÇØ·Ê¤òÉÁ²è
186        $this->drawYLine();
187        $this->drawXLine();
188       
189        // ÀÞ¤ìÀþ¥°¥é¥ÕÉÁ²è
190        for($i = 0; $i < $this->line_max; $i++) {
191            $this->drawLine($i);
192        }
193       
194        // ¥Þ¡¼¥¯¤òÉÁ²è
195        for($i = 0; $i < $this->line_max; $i++) {
196            $this->drawMark($i);
197        }
198       
199        // ¥é¥Ù¥ë¤òÉÁ²è
200        for($i = 0; $i < $this->line_max; $i++) {
201            $this->drawLabel($i);       
202        }
203
204        // ËÞÎã¤ÎÉÁ²è
205        $this->drawLegend();   
206    }
207   
208    // ¥é¥¤¥ó¤òÉÁ²è¤¹¤ë
209    function drawLine($line_no) {
210        $arrPointList = $this->arrPointList[$line_no];
211       
212        $count = count($arrPointList);
213        for($i = 0; $i < $count; $i++) {
214            $x = $arrPointList[$i][0];
215            $y = $arrPointList[$i][1];
216            if(isset($arrPointList[$i + 1])) {
217                $next_x = $arrPointList[$i + 1][0];
218                $next_y = $arrPointList[$i + 1][1];
219                imageline($this->image, $x, $y, $next_x, $next_y, $this->arrColor[$line_no]);
220            }
221        }
222    }
223   
224    // ¥Þ¡¼¥¯¤òÉÁ²è¤¹¤ë
225    function drawMark($line_no) {
226        $arrPointList = $this->arrPointList[$line_no];
227        $count = count($arrPointList);
228        for($i = 0; $i < $count; $i++) {
229            $x = $arrPointList[$i][0];
230            $y = $arrPointList[$i][1];         
231            $this->setMark($line_no, $x, $y);
232        }
233    }
234   
235    // ¥é¥Ù¥ë¤òÉÁ²è¤¹¤ë
236    function drawLabel($line_no) {
237        $arrData = $this->arrDataList[$line_no];
238        $arrPointList = $this->arrPointList[$line_no];
239        $count = count($arrPointList);
240        for($i = 0; $i < $count; $i++) {
241            $x = $arrPointList[$i][0];
242            $y = $arrPointList[$i][1];
243            $text_width = $this->getTextWidth(number_format($arrData[$i]), FONT_SIZE);
244            $y_pos = $y - FONT_SIZE - 5;
245            $x_pos = $x - $text_width / 2;
246            $this->setText(FONT_SIZE, $x_pos, $y_pos, number_format($arrData[$i]));
247        }
248    }
249   
250    // ¥Ç¡¼¥¿¤ò¥»¥Ã¥È¤¹¤ë
251    function setData($arrData) {
252        $this->arrDataList[$this->line_max] = array_values((array)$arrData);
253        $this->setMax($this->arrDataList[$this->line_max]);
254        // ÃͤÎÉÁ²èÊÑ´¹Î¨
255        $rate = $this->area_height / $this->graph_max;
256        // ÉÁ²èΨ¤ò·×»»
257        $count = count($this->arrDataList[$this->line_max]);
258        $scale_width = $this->area_width / ($count + 1);       
259        $this->arrPointList[$this->line_max] = array();
260        for($i = 0; $i < $count; $i++) {
261            // XºÂɸ¤òµá¤á¤ë
262            $x = intval($this->left + ($scale_width * ($i + 1)));
263            // YºÂɸ¤òµá¤á¤ë
264            $y = intval($this->top + $this->area_height - ($this->arrDataList[$this->line_max][$i] * $rate));
265            // XYºÂɸ¤òÊݸ¤¹¤ë
266            $this->arrPointList[$this->line_max][] = array($x, $y);
267        }
268        $this->line_max++;
269    }
270   
271    // X¼´¥é¥Ù¥ë¤ò¥»¥Ã¥È¤¹¤ë
272    function setXLabel($arrXLabel) {
273        $this->arrXLabel = array_values((array)$arrXLabel);
274    }
275   
276    // X¼´¥¿¥¤¥È¥ë¤ò¥»¥Ã¥È¤¹¤ë
277    function setXTitle($title) {
278        $this->XTitle = $title;
279    }
280   
281    // Y¼´¥¿¥¤¥È¥ë¤ò¥»¥Ã¥È¤¹¤ë
282    function setYTitle($title) {
283        $this->YTitle = $title;
284    }   
285}
286?>
Note: See TracBrowser for help on using the repository browser.