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

Revision 5334, 8.0 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) + 30;
59            $y_pos = $this->top + $this->area_height;
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;
113               
114                $this->setText(FONT_SIZE, $x_pos + $this->x_margin, $pos + FONT_SIZE + $this->y_margin, $this->arrXLabel[$i], NULL, $this->XLabelAngle);
115            }
116        }
117       
118        // ËÀ¥°¥é¥Õ¤Î¾ì¹ç¤ÏºÇ¸å¤ÎÌÜÀ¹¤ê¤ò°ì¤ÄÄɲ乤ë
119        if($bar) {
120            imageline($this->image, $x + $half_scale, $pos, $x + $half_scale, $pos - LINE_SCALE_SIZE,  $this->flame_color);
121        }
122       
123        $this->drawXTitle();
124    }
125       
126    // ¥°¥ê¥Ã¥Éɽ¼¨
127    function setYGridOn($ygrid_on) {
128        $this->ygrid_on = $ygrid_on;
129    }
130   
131    // ¥Ý¥¤¥ó¥È¤ÎÉÁ²è
132    function setMark($line_no, $left, $top, $size = LINE_MARK_SIZE) {
133        // ¶ö¿ô¤ËÊÑ´¹¤·¤Æ¤ª¤¯
134        $size += $size % 2;
135        $array = array(
136            $left, $top - ($size / 2),
137            $left + ($size / 2), $top,
138            $left, $top + ($size / 2),
139            $left - ($size / 2), $top,
140        );     
141        imagefilledpolygon($this->image, $array, 4, $this->arrColor[$line_no]);
142        imagepolygon($this->image, $array, 4, $this->flame_color);
143        imagesetpixel ($this->image, $left, $top + ($size / 2), $this->flame_color);
144    }   
145   
146    // Y¼´ÌÜÀ¹¤ê¤ËÃͤòÆþ¤ì¤ë
147    function setYScale() {
148        // 1ÌÜÀ¹¤ê¤ÎÃÍ
149        $number = intval($this->graph_max / LINE_Y_SCALE);             
150        // ÌÜÀ¹¤êÉý¤òµá¤á¤ë
151        $size = $this->area_height / LINE_Y_SCALE;
152        $pos = 0;
153        for($i = 0; $i <= LINE_Y_SCALE; $i++) {
154            $snumber = $number * (LINE_Y_SCALE - $i);
155            $disp_number = number_format($snumber);
156            $num_width = $this->getTextWidth($disp_number, FONT_SIZE);
157            $this->setText(FONT_SIZE, $this->left - $num_width - 2, $this->top + $pos - (FONT_SIZE / 2), $disp_number);
158            $pos += $size;
159        }
160    }
161   
162    //
163    function setMax($arrData) {
164        // ¥Ç¡¼¥¿¤ÎºÇÂçÃͤò¼èÆÀ¤¹¤ë¡£
165        $data_max = max($arrData);
166        // 10¤Î²¿Çܤ«¤ò¼èÆÀ
167        $figure = strlen($data_max) - 1;
168        // ¼¡¤Î·å¤ò·×»»¤¹¤ë
169        $tenval = pow(10, $figure);
170        // ¥°¥é¥Õ¾å¤Ç¤ÎºÇÂçÃͤòµá¤á¤ë
171        $this->graph_max = $tenval * (intval($data_max / $tenval) + 1);
172        // ºÇÂçÃͤ¬10̤Ëþ¤Î¾ì¹ç¤ÎÂбþ
173        if($this->graph_max < 10) {
174            $this->graph_max = 10;
175        }   
176    }
177   
178    // ¥°¥é¥Õ¤ÎÉÁ²è
179    function drawGraph() {
180        // ¥°¥é¥ÕÇØ·Ê¤òÉÁ²è
181        $this->drawYLine();
182        $this->drawXLine();
183       
184        // ÀÞ¤ìÀþ¥°¥é¥ÕÉÁ²è
185        for($i = 0; $i < $this->line_max; $i++) {
186            $this->drawLine($i);
187        }
188       
189        // ¥Þ¡¼¥¯¤òÉÁ²è
190        for($i = 0; $i < $this->line_max; $i++) {
191            $this->drawMark($i);
192        }
193       
194        // ¥é¥Ù¥ë¤òÉÁ²è
195        for($i = 0; $i < $this->line_max; $i++) {
196            $this->drawLabel($i);       
197        }
198
199        // ËÞÎã¤ÎÉÁ²è
200        $this->drawLegend();   
201    }
202   
203    // ¥é¥¤¥ó¤òÉÁ²è¤¹¤ë
204    function drawLine($line_no) {
205        $arrPointList = $this->arrPointList[$line_no];
206       
207        $count = count($arrPointList);
208        for($i = 0; $i < $count; $i++) {
209            $x = $arrPointList[$i][0];
210            $y = $arrPointList[$i][1];
211            if(isset($arrPointList[$i + 1])) {
212                $next_x = $arrPointList[$i + 1][0];
213                $next_y = $arrPointList[$i + 1][1];
214                imageline($this->image, $x, $y, $next_x, $next_y, $this->arrColor[$line_no]);
215            }
216        }
217    }
218   
219    // ¥Þ¡¼¥¯¤òÉÁ²è¤¹¤ë
220    function drawMark($line_no) {
221        $arrPointList = $this->arrPointList[$line_no];
222        $count = count($arrPointList);
223        for($i = 0; $i < $count; $i++) {
224            $x = $arrPointList[$i][0];
225            $y = $arrPointList[$i][1];         
226            $this->setMark($line_no, $x, $y);
227        }
228    }
229   
230    // ¥é¥Ù¥ë¤òÉÁ²è¤¹¤ë
231    function drawLabel($line_no) {
232        $arrData = $this->arrDataList[$line_no];
233        $arrPointList = $this->arrPointList[$line_no];
234        $count = count($arrPointList);
235        for($i = 0; $i < $count; $i++) {
236            $x = $arrPointList[$i][0];
237            $y = $arrPointList[$i][1];
238            $text_width = $this->getTextWidth(number_format($arrData[$i]), FONT_SIZE);
239            $y_pos = $y - FONT_SIZE - 5;
240            $x_pos = $x - $text_width / 2;
241            $this->setText(FONT_SIZE, $x_pos, $y_pos, number_format($arrData[$i]));
242        }
243    }
244   
245    // ¥Ç¡¼¥¿¤ò¥»¥Ã¥È¤¹¤ë
246    function setData($arrData) {
247        $this->arrDataList[$this->line_max] = array_values((array)$arrData);
248        $this->setMax($this->arrDataList[$this->line_max]);
249        // ÃͤÎÉÁ²èÊÑ´¹Î¨
250        $rate = $this->area_height / $this->graph_max;
251        // ÉÁ²èΨ¤ò·×»»
252        $count = count($this->arrDataList[$this->line_max]);
253        $scale_width = $this->area_width / ($count + 1);       
254        $this->arrPointList[$this->line_max] = array();
255        for($i = 0; $i < $count; $i++) {
256            // XºÂɸ¤òµá¤á¤ë
257            $x = intval($this->left + ($scale_width * ($i + 1)));
258            // YºÂɸ¤òµá¤á¤ë
259            $y = intval($this->top + $this->area_height - ($this->arrDataList[$this->line_max][$i] * $rate));
260            // XYºÂɸ¤òÊݸ¤¹¤ë
261            $this->arrPointList[$this->line_max][] = array($x, $y);
262        }
263        $this->line_max++;
264    }
265   
266    // X¼´¥é¥Ù¥ë¤ò¥»¥Ã¥È¤¹¤ë
267    function setXLabel($arrXLabel) {
268        $this->arrXLabel = array_values((array)$arrXLabel);
269    }
270   
271    // X¼´¥¿¥¤¥È¥ë¤ò¥»¥Ã¥È¤¹¤ë
272    function setXTitle($title) {
273        $this->XTitle = $title;
274    }
275   
276    // Y¼´¥¿¥¤¥È¥ë¤ò¥»¥Ã¥È¤¹¤ë
277    function setYTitle($title) {
278        $this->YTitle = $title;
279    }   
280}
281?>
Note: See TracBrowser for help on using the repository browser.