source: branches/feature-module-update/data/class/graph/SC_GraphLine.php @ 15628

Revision 15628, 10.1 KB checked in by nanasess, 17 years ago (diff)

未定義変数の修正

  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
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) {
120                    $bar_margin = -15;
121                } else {
122                    $bar_margin = 0;
123                }
124
125                $this->setText(FONT_SIZE, $x_pos + $this->x_margin + $bar_margin, $pos + FONT_SIZE + $this->y_margin, $this->arrXLabel[$i], NULL, $this->XLabelAngle);
126            }
127        }
128
129        // 棒グラフの場合は最後の目盛りを一つ追加する
130        if($bar) {
131            imageline($this->image, $x + $half_scale, $pos, $x + $half_scale, $pos - LINE_SCALE_SIZE,  $this->flame_color);
132        }
133
134        $this->drawXTitle();
135    }
136
137    // グリッド表示
138    function setYGridOn($ygrid_on) {
139        $this->ygrid_on = $ygrid_on;
140    }
141
142    // ポイントの描画
143    function setMark($line_no, $left, $top, $size = LINE_MARK_SIZE) {
144        // 偶数に変換しておく
145        $size += $size % 2;
146        $array = array(
147            $left, $top - ($size / 2),
148            $left + ($size / 2), $top,
149            $left, $top + ($size / 2),
150            $left - ($size / 2), $top,
151        );
152        imagefilledpolygon($this->image, $array, 4, $this->arrColor[$line_no]);
153        imagepolygon($this->image, $array, 4, $this->flame_color);
154        imagesetpixel ($this->image, $left, $top + ($size / 2), $this->flame_color);
155    }
156
157    // Y軸目盛りに値を入れる
158    function setYScale() {
159        // 1目盛りの値
160        $number = intval($this->graph_max / LINE_Y_SCALE);
161        // 目盛り幅を求める
162        $size = $this->area_height / LINE_Y_SCALE;
163        $pos = 0;
164        for($i = 0; $i <= LINE_Y_SCALE; $i++) {
165            $snumber = $number * (LINE_Y_SCALE - $i);
166            $disp_number = number_format($snumber);
167            $num_width = $this->getTextWidth($disp_number, FONT_SIZE);
168            $this->setText(FONT_SIZE, $this->left - $num_width - 2, $this->top + $pos - (FONT_SIZE / 2), $disp_number);
169            $pos += $size;
170        }
171    }
172
173    //
174    function setMax($arrData) {
175        // データの最大値を取得する。
176        $data_max = max($arrData);
177        // 10の何倍かを取得
178        $figure = strlen($data_max) - 1;
179        // 次の桁を計算する
180        $tenval = pow(10, $figure);
181        // グラフ上での最大値を求める
182        $this->graph_max = $tenval * (intval($data_max / $tenval) + 1);
183        // 最大値が10未満の場合の対応
184        if($this->graph_max < 10) {
185            $this->graph_max = 10;
186        }
187    }
188
189    // グラフの描画
190    function drawGraph() {
191        // グラフ背景を描画
192        $this->drawYLine();
193        $this->drawXLine();
194
195        // 折れ線グラフ描画
196        for($i = 0; $i < $this->line_max; $i++) {
197            $this->drawLine($i);
198        }
199
200        // マークを描画
201        for($i = 0; $i < $this->line_max; $i++) {
202            $this->drawMark($i);
203        }
204
205        // ラベルを描画
206        for($i = 0; $i < $this->line_max; $i++) {
207            $this->drawLabel($i);
208        }
209
210        // 凡例の描画
211        $this->drawLegend();
212    }
213
214    // ラインを描画する
215    function drawLine($line_no) {
216        $arrPointList = $this->arrPointList[$line_no];
217
218        $count = count($arrPointList);
219        for($i = 0; $i < $count; $i++) {
220            $x = $arrPointList[$i][0];
221            $y = $arrPointList[$i][1];
222            if(isset($arrPointList[$i + 1])) {
223                $next_x = $arrPointList[$i + 1][0];
224                $next_y = $arrPointList[$i + 1][1];
225                imageline($this->image, $x, $y, $next_x, $next_y, $this->arrColor[$line_no]);
226            }
227        }
228    }
229
230    // マークを描画する
231    function drawMark($line_no) {
232        $arrPointList = $this->arrPointList[$line_no];
233        $count = count($arrPointList);
234        for($i = 0; $i < $count; $i++) {
235            $x = $arrPointList[$i][0];
236            $y = $arrPointList[$i][1];
237            $this->setMark($line_no, $x, $y);
238        }
239    }
240
241    // ラベルを描画する
242    function drawLabel($line_no) {
243        $arrData = $this->arrDataList[$line_no];
244        $arrPointList = $this->arrPointList[$line_no];
245        $count = count($arrPointList);
246        for($i = 0; $i < $count; $i++) {
247            $x = $arrPointList[$i][0];
248            $y = $arrPointList[$i][1];
249            $text_width = $this->getTextWidth(number_format($arrData[$i]), FONT_SIZE);
250            $y_pos = $y - FONT_SIZE - 5;
251            $x_pos = $x - $text_width / 2;
252            $this->setText(FONT_SIZE, $x_pos, $y_pos, number_format($arrData[$i]));
253        }
254    }
255
256    // データをセットする
257    function setData($arrData) {
258        $this->arrDataList[$this->line_max] = array_values((array)$arrData);
259        $this->setMax($this->arrDataList[$this->line_max]);
260        // 値の描画変換率
261        $rate = $this->area_height / $this->graph_max;
262        // 描画率を計算
263        $count = count($this->arrDataList[$this->line_max]);
264        $scale_width = $this->area_width / ($count + 1);
265        $this->arrPointList[$this->line_max] = array();
266        for($i = 0; $i < $count; $i++) {
267            // X座標を求める
268            $x = intval($this->left + ($scale_width * ($i + 1)));
269            // Y座標を求める
270            $y = intval($this->top + $this->area_height - ($this->arrDataList[$this->line_max][$i] * $rate));
271            // XY座標を保存する
272            $this->arrPointList[$this->line_max][] = array($x, $y);
273        }
274        $this->line_max++;
275    }
276
277    // X軸ラベルをセットする
278    function setXLabel($arrXLabel) {
279        $this->arrXLabel = array_values((array)$arrXLabel);
280    }
281
282    // X軸タイトルをセットする
283    function setXTitle($title) {
284        $this->XTitle = $title;
285    }
286
287    // Y軸タイトルをセットする
288    function setYTitle($title) {
289        $this->YTitle = $title;
290    }
291}
292?>
Note: See TracBrowser for help on using the repository browser.