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

Revision 23605, 11.2 KB checked in by kimoto, 10 years ago (diff)

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

Scrutinizer Auto-Fixes

This patch was automatically generated as part of the following inspection:
 https://scrutinizer-ci.com/g/nobuhiko/EC-CUBE/inspections/d8722894-69a6-4b1b-898d-43618035c60d

Enabled analysis tools:

  • PHP Analyzer
  • PHP PDepend
  • PHP Similarity Analyzer
  • PHP Change Tracking Analyzer
  • 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    public function setMark($line_no, $left, $top, $size = LINE_MARK_SIZE)
169    {
170        // 偶数に変換しておく
171        $size += $size % 2;
172        $array = array(
173            $left, $top - ($size / 2),
174            $left + ($size / 2), $top,
175            $left, $top + ($size / 2),
176            $left - ($size / 2), $top,
177        );
178        imagefilledpolygon($this->image, $array, 4, $this->arrColor[$line_no]);
179        imagepolygon($this->image, $array, 4, $this->flame_color);
180        imagesetpixel($this->image, $left, $top + ($size / 2), $this->flame_color);
181    }
182
183    // Y軸目盛りに値を入れる
184    public function setYScale()
185    {
186        // 1目盛りの値
187        $number = intval($this->graph_max / LINE_Y_SCALE);
188        // 目盛り幅を求める
189        $size = $this->area_height / LINE_Y_SCALE;
190        $pos = 0;
191        for ($i = 0; $i <= LINE_Y_SCALE; $i++) {
192            $snumber = $number * (LINE_Y_SCALE - $i);
193            $disp_number = number_format($snumber);
194            $num_width = $this->getTextWidth($disp_number, FONT_SIZE);
195            $this->setText(FONT_SIZE, $this->left - $num_width - 2, $this->top + $pos - (FONT_SIZE / 2), $disp_number);
196            $pos += $size;
197        }
198    }
199
200    //
201    public function setMax($arrData)
202    {
203        // データの最大値を取得する。
204        $data_max = max($arrData);
205        // 10の何倍かを取得
206        $figure = strlen($data_max) - 1;
207        // 次の桁を計算する
208        $tenval = pow(10, $figure);
209        // グラフ上での最大値を求める
210        $this->graph_max = $tenval * (intval($data_max / $tenval) + 1);
211        // 最大値が10未満の場合の対応
212        if ($this->graph_max < 10) {
213            $this->graph_max = 10;
214        }
215    }
216
217    // グラフの描画
218    public function drawGraph()
219    {
220        // グラフ背景を描画
221        $this->drawYLine();
222        $this->drawXLine();
223
224        // 折れ線グラフ描画
225        for ($i = 0; $i < $this->line_max; $i++) {
226            $this->drawLine($i);
227        }
228
229        // マークを描画
230        for ($i = 0; $i < $this->line_max; $i++) {
231            $this->drawMark($i);
232        }
233
234        // ラベルを描画
235        for ($i = 0; $i < $this->line_max; $i++) {
236            $this->drawLabel($i);
237        }
238
239        // 凡例の描画
240        $this->drawLegend();
241    }
242
243    // ラインを描画する
244
245    /**
246     * @param integer $line_no
247     */
248    public function drawLine($line_no)
249    {
250        $arrPointList = $this->arrPointList[$line_no];
251
252        $count = count($arrPointList);
253        for ($i = 0; $i < $count; $i++) {
254            $x = $arrPointList[$i][0];
255            $y = $arrPointList[$i][1];
256            if (isset($arrPointList[$i + 1])) {
257                $next_x = $arrPointList[$i + 1][0];
258                $next_y = $arrPointList[$i + 1][1];
259                imageline($this->image, $x, $y, $next_x, $next_y, $this->arrColor[$line_no]);
260            }
261        }
262    }
263
264    // マークを描画する
265
266    /**
267     * @param integer $line_no
268     */
269    public function drawMark($line_no)
270    {
271        $arrPointList = $this->arrPointList[$line_no];
272        $count = count($arrPointList);
273        for ($i = 0; $i < $count; $i++) {
274            $x = $arrPointList[$i][0];
275            $y = $arrPointList[$i][1];
276            $this->setMark($line_no, $x, $y);
277        }
278    }
279
280    // ラベルを描画する
281
282    /**
283     * @param integer $line_no
284     */
285    public function drawLabel($line_no)
286    {
287        $arrData = $this->arrDataList[$line_no];
288        $arrPointList = $this->arrPointList[$line_no];
289        $count = count($arrPointList);
290        for ($i = 0; $i < $count; $i++) {
291            $x = $arrPointList[$i][0];
292            $y = $arrPointList[$i][1];
293            $text_width = $this->getTextWidth(number_format($arrData[$i]), FONT_SIZE);
294            $y_pos = $y - FONT_SIZE - 5;
295            $x_pos = $x - $text_width / 2;
296            $this->setText(FONT_SIZE, $x_pos, $y_pos, number_format($arrData[$i]));
297        }
298    }
299
300    // データをセットする
301    public function setData($arrData)
302    {
303        $this->arrDataList[$this->line_max] = array_values((array) $arrData);
304        $this->setMax($this->arrDataList[$this->line_max]);
305        // 値の描画変換率
306        $rate = $this->area_height / $this->graph_max;
307        // 描画率を計算
308        $count = count($this->arrDataList[$this->line_max]);
309        $scale_width = $this->area_width / ($count + 1);
310        $this->arrPointList[$this->line_max] = array();
311        for ($i = 0; $i < $count; $i++) {
312            // X座標を求める
313            $x = intval($this->left + ($scale_width * ($i + 1)));
314            // Y座標を求める
315            $y = intval($this->top + $this->area_height - ($this->arrDataList[$this->line_max][$i] * $rate));
316            // XY座標を保存する
317            $this->arrPointList[$this->line_max][] = array($x, $y);
318        }
319        $this->line_max++;
320    }
321
322    // X軸ラベルをセットする
323    public function setXLabel($arrXLabel)
324    {
325        $this->arrXLabel = array_values((array) $arrXLabel);
326    }
327
328    // X軸タイトルをセットする
329    public function setXTitle($title)
330    {
331        $this->XTitle = $title;
332    }
333
334    // Y軸タイトルをセットする
335    public function setYTitle($title)
336    {
337        $this->YTitle = $title;
338    }
339}
Note: See TracBrowser for help on using the repository browser.