source: branches/version-2_12-dev/data/class/graph/SC_GraphLine.php @ 21441

Revision 21441, 10.9 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

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