source: branches/version-2_13-dev/data/class/graph/SC_Graph_Line.php @ 23606

Revision 23606, 11.3 KB checked in by kimoto, 12 years ago (diff)

#2448 typo修正・ソース整形・ソースコメントの改善 for 2.13.3

 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/e0f27994-b3c7-4fc6-ad70-55d3cd63769b/patches

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2014 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// 折れ線グラフ生成クラス
25class SC_Graph_Line extends SC_Graph_Base_Ex
26{
27    public $area_width;
28    public $area_height;
29    public $ygrid_on;
30    public $graph_max;     // グラフのエリア最大値(Y軸頂点の値)
31    public $arrXLabel;
32    public $XLabelAngle;   // X軸ラベル角度
33    public $XTitle;        // X軸タイトル
34    public $YTitle;        // Y軸タイトル
35    public $arrDataList;   // グラフデータを格納
36    public $arrPointList;  // 折れ線座標を格納
37    public $line_max;      // 複数の描画の場合に加算していく
38
39    public $x_margin;
40    public $y_margin;
41
42    // コンストラクタ
43    public function __construct(
44        $bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = LINE_LEFT, $top = LINE_TOP,
45        $area_width = LINE_AREA_WIDTH, $area_height = LINE_AREA_HEIGHT) {
46        parent::__construct($bgw, $bgh, $left, $top);
47        $this->area_width = $area_width;
48        $this->area_height = $area_height;
49        $this->ygrid_on = true;
50        $this->line_max = 0;
51        $this->graph_max = 0;
52        $this->XLabelAngle = 0;
53        $this->x_margin = 0;
54        $this->y_margin = 0;
55    }
56
57    // X軸ラベルの角度セット
58
59    /**
60     * @param integer $Angle
61     */
62    public function setXLabelAngle($Angle)
63    {
64        $this->XLabelAngle = $Angle;
65    }
66
67    // Y軸タイトル
68    public function drawYTitle()
69    {
70        // Y軸にタイトルを入れる
71        if ($this->YTitle != '') {
72            $text_width = $this->getTextWidth($this->YTitle, FONT_SIZE);
73            $x_pos = $this->left - ($text_width / 2);
74            $y_pos = $this->top - FONT_SIZE - LINE_YTITLE_PAD;
75            $this->setText(FONT_SIZE, $x_pos, $y_pos, $this->YTitle);
76        }
77    }
78
79    // X軸タイトル
80    public function drawXTitle()
81    {
82        // Y軸にタイトルを入れる
83        if ($this->XTitle != '') {
84            $text_width = $this->getTextWidth($this->XTitle, FONT_SIZE);
85            $x_pos = $this->left + $this->area_width - ($text_width / 2) + 30;
86            $y_pos = $this->top + $this->area_height + LINE_XTITLE_PAD;
87            $this->setText(FONT_SIZE, $x_pos, $y_pos, $this->XTitle);
88        }
89    }
90
91    // Y軸の描画
92    public function drawYLine()
93    {
94        imageline($this->image, $this->left, $this->top, $this->left, $this->top + $this->area_height, $this->flame_color);
95        // 目盛り幅を求める(中間点は自動)
96        $size = $this->area_height / (LINE_Y_SCALE * 2);
97        // 上から目盛りを入れていく
98        $pos = 0;
99        for ($i = 0; $i < (LINE_Y_SCALE * 2); $i++) {
100            // 目盛り幅
101            if (($i % 2) == 0) {
102                $sw = LINE_SCALE_SIZE;
103                if ($this->ygrid_on) {
104                    imageline($this->image, $this->left, $this->top + $pos, $this->left + $this->area_width, $this->top + $pos, $this->grid_color);
105                }
106            } else {
107                $sw = LINE_SCALE_SIZE / 2;
108            }
109            imageline($this->image, $this->left, $this->top + $pos, $this->left + $sw, $this->top + $pos, $this->flame_color);
110            $pos += $size;
111        }
112        // Y軸に目盛り値を入れる
113        $this->setYScale();
114        $this->drawYTitle();
115    }
116
117    // X軸の描画
118    public function drawXLine($bar = false)
119    {
120        imageline($this->image, $this->left, $this->top + $this->area_height, $this->left + $this->area_width, $this->top + $this->area_height, $this->flame_color);
121        $arrPointList = $this->arrPointList[0];
122        $count = count($arrPointList);
123
124        // 棒グラフの場合は半目盛りずらす
125        if ($bar) {
126            $half_scale = intval($this->area_width / ($count + 1) / 2);
127        } else {
128            $half_scale = 0;
129        }
130
131        // ラベルの表示インターバルを算出
132        $interval = ceil($count / LINE_XLABEL_MAX); // 切り上げ
133        for ($i = 0; $i < $count; $i++) {
134            // X軸に目盛りを入れる
135            $x = $arrPointList[$i][0];
136            $pos = $this->top + $this->area_height;
137            imageline($this->image, $x - $half_scale, $pos, $x - $half_scale, $pos - LINE_SCALE_SIZE, $this->flame_color);
138            // ラベルを入れる
139            if (($i % $interval) == 0) {
140                $text_width = $this->getTextWidth($this->arrXLabel[$i], FONT_SIZE);
141                $x_pos = $x;
142
143                if ($bar) {
144                    $bar_margin = -15;
145                } else {
146                    $bar_margin = 0;
147                }
148
149                $this->setText(FONT_SIZE, $x_pos + $this->x_margin + $bar_margin, $pos + FONT_SIZE + $this->y_margin, $this->arrXLabel[$i], NULL, $this->XLabelAngle);
150            }
151        }
152
153        // 棒グラフの場合は最後の目盛りを一つ追加する
154        if ($bar) {
155            imageline($this->image, $x + $half_scale, $pos, $x + $half_scale, $pos - LINE_SCALE_SIZE, $this->flame_color);
156        }
157
158        $this->drawXTitle();
159    }
160
161    // グリッド表示
162    public function setYGridOn($ygrid_on)
163    {
164        $this->ygrid_on = $ygrid_on;
165    }
166
167    // ポイントの描画
168
169    /**
170     * @param integer $line_no
171     */
172    public function setMark($line_no, $left, $top, $size = LINE_MARK_SIZE)
173    {
174        // 偶数に変換しておく
175        $size += $size % 2;
176        $array = array(
177            $left, $top - ($size / 2),
178            $left + ($size / 2), $top,
179            $left, $top + ($size / 2),
180            $left - ($size / 2), $top,
181        );
182        imagefilledpolygon($this->image, $array, 4, $this->arrColor[$line_no]);
183        imagepolygon($this->image, $array, 4, $this->flame_color);
184        imagesetpixel($this->image, $left, $top + ($size / 2), $this->flame_color);
185    }
186
187    // Y軸目盛りに値を入れる
188    public function setYScale()
189    {
190        // 1目盛りの値
191        $number = intval($this->graph_max / LINE_Y_SCALE);
192        // 目盛り幅を求める
193        $size = $this->area_height / LINE_Y_SCALE;
194        $pos = 0;
195        for ($i = 0; $i <= LINE_Y_SCALE; $i++) {
196            $snumber = $number * (LINE_Y_SCALE - $i);
197            $disp_number = number_format($snumber);
198            $num_width = $this->getTextWidth($disp_number, FONT_SIZE);
199            $this->setText(FONT_SIZE, $this->left - $num_width - 2, $this->top + $pos - (FONT_SIZE / 2), $disp_number);
200            $pos += $size;
201        }
202    }
203
204    //
205    public function setMax($arrData)
206    {
207        // データの最大値を取得する。
208        $data_max = max($arrData);
209        // 10の何倍かを取得
210        $figure = strlen($data_max) - 1;
211        // 次の桁を計算する
212        $tenval = pow(10, $figure);
213        // グラフ上での最大値を求める
214        $this->graph_max = $tenval * (intval($data_max / $tenval) + 1);
215        // 最大値が10未満の場合の対応
216        if ($this->graph_max < 10) {
217            $this->graph_max = 10;
218        }
219    }
220
221    // グラフの描画
222    public function drawGraph()
223    {
224        // グラフ背景を描画
225        $this->drawYLine();
226        $this->drawXLine();
227
228        // 折れ線グラフ描画
229        for ($i = 0; $i < $this->line_max; $i++) {
230            $this->drawLine($i);
231        }
232
233        // マークを描画
234        for ($i = 0; $i < $this->line_max; $i++) {
235            $this->drawMark($i);
236        }
237
238        // ラベルを描画
239        for ($i = 0; $i < $this->line_max; $i++) {
240            $this->drawLabel($i);
241        }
242
243        // 凡例の描画
244        $this->drawLegend();
245    }
246
247    // ラインを描画する
248
249    /**
250     * @param integer $line_no
251     */
252    public function drawLine($line_no)
253    {
254        $arrPointList = $this->arrPointList[$line_no];
255
256        $count = count($arrPointList);
257        for ($i = 0; $i < $count; $i++) {
258            $x = $arrPointList[$i][0];
259            $y = $arrPointList[$i][1];
260            if (isset($arrPointList[$i + 1])) {
261                $next_x = $arrPointList[$i + 1][0];
262                $next_y = $arrPointList[$i + 1][1];
263                imageline($this->image, $x, $y, $next_x, $next_y, $this->arrColor[$line_no]);
264            }
265        }
266    }
267
268    // マークを描画する
269
270    /**
271     * @param integer $line_no
272     */
273    public function drawMark($line_no)
274    {
275        $arrPointList = $this->arrPointList[$line_no];
276        $count = count($arrPointList);
277        for ($i = 0; $i < $count; $i++) {
278            $x = $arrPointList[$i][0];
279            $y = $arrPointList[$i][1];
280            $this->setMark($line_no, $x, $y);
281        }
282    }
283
284    // ラベルを描画する
285
286    /**
287     * @param integer $line_no
288     */
289    public function drawLabel($line_no)
290    {
291        $arrData = $this->arrDataList[$line_no];
292        $arrPointList = $this->arrPointList[$line_no];
293        $count = count($arrPointList);
294        for ($i = 0; $i < $count; $i++) {
295            $x = $arrPointList[$i][0];
296            $y = $arrPointList[$i][1];
297            $text_width = $this->getTextWidth(number_format($arrData[$i]), FONT_SIZE);
298            $y_pos = $y - FONT_SIZE - 5;
299            $x_pos = $x - $text_width / 2;
300            $this->setText(FONT_SIZE, $x_pos, $y_pos, number_format($arrData[$i]));
301        }
302    }
303
304    // データをセットする
305    public function setData($arrData)
306    {
307        $this->arrDataList[$this->line_max] = array_values((array) $arrData);
308        $this->setMax($this->arrDataList[$this->line_max]);
309        // 値の描画変換率
310        $rate = $this->area_height / $this->graph_max;
311        // 描画率を計算
312        $count = count($this->arrDataList[$this->line_max]);
313        $scale_width = $this->area_width / ($count + 1);
314        $this->arrPointList[$this->line_max] = array();
315        for ($i = 0; $i < $count; $i++) {
316            // X座標を求める
317            $x = intval($this->left + ($scale_width * ($i + 1)));
318            // Y座標を求める
319            $y = intval($this->top + $this->area_height - ($this->arrDataList[$this->line_max][$i] * $rate));
320            // XY座標を保存する
321            $this->arrPointList[$this->line_max][] = array($x, $y);
322        }
323        $this->line_max++;
324    }
325
326    // X軸ラベルをセットする
327    public function setXLabel($arrXLabel)
328    {
329        $this->arrXLabel = array_values((array) $arrXLabel);
330    }
331
332    // X軸タイトルをセットする
333
334    /**
335     * @param string $title
336     */
337    public function setXTitle($title)
338    {
339        $this->XTitle = $title;
340    }
341
342    // Y軸タイトルをセットする
343
344    /**
345     * @param string $title
346     */
347    public function setYTitle($title)
348    {
349        $this->YTitle = $title;
350    }
351}
Note: See TracBrowser for help on using the repository browser.