1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | // 折れ線グラフ生成クラス |
---|
25 | class SC_Graph_Line extends SC_Graph_Base_Ex{ |
---|
26 | var $area_width; |
---|
27 | var $area_height; |
---|
28 | var $ygrid_on; |
---|
29 | var $graph_max; // グラフのエリア最大値(Y軸頂点の値) |
---|
30 | var $arrXLabel; |
---|
31 | var $XLabelAngle; // X軸ラベル角度 |
---|
32 | var $XTitle; // X軸タイトル |
---|
33 | var $YTitle; // Y軸タイトル |
---|
34 | var $arrDataList; // グラフデータを格納 |
---|
35 | var $arrPointList; // 折れ線座標を格納 |
---|
36 | var $line_max; // 複数の描画の場合に加算していく |
---|
37 | |
---|
38 | var $x_margin; |
---|
39 | var $y_margin; |
---|
40 | |
---|
41 | // コンストラクタ |
---|
42 | function __construct( |
---|
43 | $bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = LINE_LEFT, $top = LINE_TOP, |
---|
44 | $area_width = LINE_AREA_WIDTH, $area_height = LINE_AREA_HEIGHT) { |
---|
45 | parent::__construct($bgw, $bgh, $left, $top); |
---|
46 | $this->area_width = $area_width; |
---|
47 | $this->area_height = $area_height; |
---|
48 | $this->ygrid_on = true; |
---|
49 | $this->line_max = 0; |
---|
50 | $this->graph_max = 0; |
---|
51 | $this->XLabelAngle = 0; |
---|
52 | $this->x_margin = 0; |
---|
53 | $this->y_margin = 0; |
---|
54 | } |
---|
55 | |
---|
56 | // X軸ラベルの角度セット |
---|
57 | function setXLabelAngle($Angle) { |
---|
58 | $this->XLabelAngle = $Angle; |
---|
59 | } |
---|
60 | |
---|
61 | // Y軸タイトル |
---|
62 | function drawYTitle() { |
---|
63 | // Y軸にタイトルを入れる |
---|
64 | if ($this->YTitle != '') { |
---|
65 | $text_width = $this->getTextWidth($this->YTitle, FONT_SIZE); |
---|
66 | $x_pos = $this->left - ($text_width / 2); |
---|
67 | $y_pos = $this->top - FONT_SIZE - LINE_YTITLE_PAD; |
---|
68 | $this->setText(FONT_SIZE, $x_pos, $y_pos, $this->YTitle); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | // X軸タイトル |
---|
73 | function drawXTitle() { |
---|
74 | // Y軸にタイトルを入れる |
---|
75 | if ($this->XTitle != '') { |
---|
76 | $text_width = $this->getTextWidth($this->XTitle, FONT_SIZE); |
---|
77 | $x_pos = $this->left + $this->area_width - ($text_width / 2) + 30; |
---|
78 | $y_pos = $this->top + $this->area_height + LINE_XTITLE_PAD; |
---|
79 | $this->setText(FONT_SIZE, $x_pos, $y_pos, $this->XTitle); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | // Y軸の描画 |
---|
84 | function drawYLine() { |
---|
85 | imageline($this->image, $this->left, $this->top, $this->left, $this->top + $this->area_height, $this->flame_color); |
---|
86 | // 目盛り幅を求める(中間点は自動) |
---|
87 | $size = $this->area_height / (LINE_Y_SCALE * 2); |
---|
88 | // 上から目盛りを入れていく |
---|
89 | $pos = 0; |
---|
90 | for ($i = 0; $i < (LINE_Y_SCALE * 2); $i++) { |
---|
91 | // 目盛り幅 |
---|
92 | if (($i % 2) == 0) { |
---|
93 | $sw = LINE_SCALE_SIZE; |
---|
94 | if ($this->ygrid_on) { |
---|
95 | imageline($this->image, $this->left, $this->top + $pos, $this->left + $this->area_width, $this->top + $pos, $this->grid_color); |
---|
96 | } |
---|
97 | } else { |
---|
98 | $sw = LINE_SCALE_SIZE / 2; |
---|
99 | } |
---|
100 | imageline($this->image, $this->left, $this->top + $pos, $this->left + $sw, $this->top + $pos, $this->flame_color); |
---|
101 | $pos += $size; |
---|
102 | } |
---|
103 | // Y軸に目盛り値を入れる |
---|
104 | $this->setYScale(); |
---|
105 | $this->drawYTitle(); |
---|
106 | } |
---|
107 | |
---|
108 | // X軸の描画 |
---|
109 | function drawXLine($bar = false) { |
---|
110 | imageline($this->image, $this->left, $this->top + $this->area_height, $this->left + $this->area_width, $this->top + $this->area_height, $this->flame_color); |
---|
111 | $arrPointList = $this->arrPointList[0]; |
---|
112 | $count = count($arrPointList); |
---|
113 | |
---|
114 | // 棒グラフの場合は半目盛りずらす |
---|
115 | if ($bar) { |
---|
116 | $half_scale = intval($this->area_width / ($count + 1) / 2); |
---|
117 | } else { |
---|
118 | $half_scale = 0; |
---|
119 | } |
---|
120 | |
---|
121 | // ラベルの表示インターバルを算出 |
---|
122 | $interval = ceil($count / LINE_XLABEL_MAX); // 切り上げ |
---|
123 | for ($i = 0; $i < $count; $i++) { |
---|
124 | // X軸に目盛りを入れる |
---|
125 | $x = $arrPointList[$i][0]; |
---|
126 | $pos = $this->top + $this->area_height; |
---|
127 | imageline($this->image, $x - $half_scale, $pos, $x - $half_scale, $pos - LINE_SCALE_SIZE, $this->flame_color); |
---|
128 | // ラベルを入れる |
---|
129 | if (($i % $interval) == 0) { |
---|
130 | $text_width = $this->getTextWidth($this->arrXLabel[$i], FONT_SIZE); |
---|
131 | $x_pos = $x; |
---|
132 | |
---|
133 | if ($bar) { |
---|
134 | $bar_margin = -15; |
---|
135 | } else { |
---|
136 | $bar_margin = 0; |
---|
137 | } |
---|
138 | |
---|
139 | $this->setText(FONT_SIZE, $x_pos + $this->x_margin + $bar_margin, $pos + FONT_SIZE + $this->y_margin, $this->arrXLabel[$i], NULL, $this->XLabelAngle); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | // 棒グラフの場合は最後の目盛りを一つ追加する |
---|
144 | if ($bar) { |
---|
145 | imageline($this->image, $x + $half_scale, $pos, $x + $half_scale, $pos - LINE_SCALE_SIZE, $this->flame_color); |
---|
146 | } |
---|
147 | |
---|
148 | $this->drawXTitle(); |
---|
149 | } |
---|
150 | |
---|
151 | // グリッド表示 |
---|
152 | function setYGridOn($ygrid_on) { |
---|
153 | $this->ygrid_on = $ygrid_on; |
---|
154 | } |
---|
155 | |
---|
156 | // ポイントの描画 |
---|
157 | function setMark($line_no, $left, $top, $size = LINE_MARK_SIZE) { |
---|
158 | // 偶数に変換しておく |
---|
159 | $size += $size % 2; |
---|
160 | $array = array( |
---|
161 | $left, $top - ($size / 2), |
---|
162 | $left + ($size / 2), $top, |
---|
163 | $left, $top + ($size / 2), |
---|
164 | $left - ($size / 2), $top, |
---|
165 | ); |
---|
166 | imagefilledpolygon($this->image, $array, 4, $this->arrColor[$line_no]); |
---|
167 | imagepolygon($this->image, $array, 4, $this->flame_color); |
---|
168 | imagesetpixel ($this->image, $left, $top + ($size / 2), $this->flame_color); |
---|
169 | } |
---|
170 | |
---|
171 | // Y軸目盛りに値を入れる |
---|
172 | function setYScale() { |
---|
173 | // 1目盛りの値 |
---|
174 | $number = intval($this->graph_max / LINE_Y_SCALE); |
---|
175 | // 目盛り幅を求める |
---|
176 | $size = $this->area_height / LINE_Y_SCALE; |
---|
177 | $pos = 0; |
---|
178 | for ($i = 0; $i <= LINE_Y_SCALE; $i++) { |
---|
179 | $snumber = $number * (LINE_Y_SCALE - $i); |
---|
180 | $disp_number = number_format($snumber); |
---|
181 | $num_width = $this->getTextWidth($disp_number, FONT_SIZE); |
---|
182 | $this->setText(FONT_SIZE, $this->left - $num_width - 2, $this->top + $pos - (FONT_SIZE / 2), $disp_number); |
---|
183 | $pos += $size; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | // |
---|
188 | function setMax($arrData) { |
---|
189 | // データの最大値を取得する。 |
---|
190 | $data_max = max($arrData); |
---|
191 | // 10の何倍かを取得 |
---|
192 | $figure = strlen($data_max) - 1; |
---|
193 | // 次の桁を計算する |
---|
194 | $tenval = pow(10, $figure); |
---|
195 | // グラフ上での最大値を求める |
---|
196 | $this->graph_max = $tenval * (intval($data_max / $tenval) + 1); |
---|
197 | // 最大値が10未満の場合の対応 |
---|
198 | if ($this->graph_max < 10) { |
---|
199 | $this->graph_max = 10; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | // グラフの描画 |
---|
204 | function drawGraph() { |
---|
205 | // グラフ背景を描画 |
---|
206 | $this->drawYLine(); |
---|
207 | $this->drawXLine(); |
---|
208 | |
---|
209 | // 折れ線グラフ描画 |
---|
210 | for ($i = 0; $i < $this->line_max; $i++) { |
---|
211 | $this->drawLine($i); |
---|
212 | } |
---|
213 | |
---|
214 | // マークを描画 |
---|
215 | for ($i = 0; $i < $this->line_max; $i++) { |
---|
216 | $this->drawMark($i); |
---|
217 | } |
---|
218 | |
---|
219 | // ラベルを描画 |
---|
220 | for ($i = 0; $i < $this->line_max; $i++) { |
---|
221 | $this->drawLabel($i); |
---|
222 | } |
---|
223 | |
---|
224 | // 凡例の描画 |
---|
225 | $this->drawLegend(); |
---|
226 | } |
---|
227 | |
---|
228 | // ラインを描画する |
---|
229 | function drawLine($line_no) { |
---|
230 | $arrPointList = $this->arrPointList[$line_no]; |
---|
231 | |
---|
232 | $count = count($arrPointList); |
---|
233 | for ($i = 0; $i < $count; $i++) { |
---|
234 | $x = $arrPointList[$i][0]; |
---|
235 | $y = $arrPointList[$i][1]; |
---|
236 | if (isset($arrPointList[$i + 1])) { |
---|
237 | $next_x = $arrPointList[$i + 1][0]; |
---|
238 | $next_y = $arrPointList[$i + 1][1]; |
---|
239 | imageline($this->image, $x, $y, $next_x, $next_y, $this->arrColor[$line_no]); |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | // マークを描画する |
---|
245 | function drawMark($line_no) { |
---|
246 | $arrPointList = $this->arrPointList[$line_no]; |
---|
247 | $count = count($arrPointList); |
---|
248 | for ($i = 0; $i < $count; $i++) { |
---|
249 | $x = $arrPointList[$i][0]; |
---|
250 | $y = $arrPointList[$i][1]; |
---|
251 | $this->setMark($line_no, $x, $y); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | // ラベルを描画する |
---|
256 | function drawLabel($line_no) { |
---|
257 | $arrData = $this->arrDataList[$line_no]; |
---|
258 | $arrPointList = $this->arrPointList[$line_no]; |
---|
259 | $count = count($arrPointList); |
---|
260 | for ($i = 0; $i < $count; $i++) { |
---|
261 | $x = $arrPointList[$i][0]; |
---|
262 | $y = $arrPointList[$i][1]; |
---|
263 | $text_width = $this->getTextWidth(number_format($arrData[$i]), FONT_SIZE); |
---|
264 | $y_pos = $y - FONT_SIZE - 5; |
---|
265 | $x_pos = $x - $text_width / 2; |
---|
266 | $this->setText(FONT_SIZE, $x_pos, $y_pos, number_format($arrData[$i])); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | // データをセットする |
---|
271 | function setData($arrData) { |
---|
272 | $this->arrDataList[$this->line_max] = array_values((array)$arrData); |
---|
273 | $this->setMax($this->arrDataList[$this->line_max]); |
---|
274 | // 値の描画変換率 |
---|
275 | $rate = $this->area_height / $this->graph_max; |
---|
276 | // 描画率を計算 |
---|
277 | $count = count($this->arrDataList[$this->line_max]); |
---|
278 | $scale_width = $this->area_width / ($count + 1); |
---|
279 | $this->arrPointList[$this->line_max] = array(); |
---|
280 | for ($i = 0; $i < $count; $i++) { |
---|
281 | // X座標を求める |
---|
282 | $x = intval($this->left + ($scale_width * ($i + 1))); |
---|
283 | // Y座標を求める |
---|
284 | $y = intval($this->top + $this->area_height - ($this->arrDataList[$this->line_max][$i] * $rate)); |
---|
285 | // XY座標を保存する |
---|
286 | $this->arrPointList[$this->line_max][] = array($x, $y); |
---|
287 | } |
---|
288 | $this->line_max++; |
---|
289 | } |
---|
290 | |
---|
291 | // X軸ラベルをセットする |
---|
292 | function setXLabel($arrXLabel) { |
---|
293 | $this->arrXLabel = array_values((array)$arrXLabel); |
---|
294 | } |
---|
295 | |
---|
296 | // X軸タイトルをセットする |
---|
297 | function setXTitle($title) { |
---|
298 | $this->XTitle = $title; |
---|
299 | } |
---|
300 | |
---|
301 | // Y軸タイトルをセットする |
---|
302 | function setYTitle($title) { |
---|
303 | $this->YTitle = $title; |
---|
304 | } |
---|
305 | } |
---|