source: branches/comu/html/admin/total/class/SC_GraphLine.php @ 12197

Revision 12197, 8.2 KB checked in by adati, 17 years ago (diff)

1.3.0正式版のマージ

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