source: branches/version-2_12-dev/data/class/graph/SC_Graph_Line.php @ 22567

Revision 22567, 10.9 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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