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

Revision 22567, 15.2 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/** TTFフォントファイル */
25define('FONT_REALFILE', DATA_REALDIR . 'fonts/wlmaru20044.ttf');
26
27/** フォントサイズ */
28define('FONT_SIZE', 8);
29
30/** タイトルフォントサイズ */
31define('TITLE_FONT_SIZE', 11);
32
33/** 背景幅 */
34define('BG_WIDTH', 720);
35
36/** 背景高さ */
37define('BG_HEIGHT', 400);
38
39/** 行間 */
40define('LINE_PAD', 5);
41
42/** フォント補正値(実際の描画幅/フォントサイズ) */
43define('TEXT_RATE', 0.75);
44
45// -----------------------------------------------------------------------------
46// 円グラフ
47// -----------------------------------------------------------------------------
48/** 円グラフ位置 */
49define('PIE_LEFT', 200);
50
51/** 円グラフ位置 */
52define('PIE_TOP', 150);
53
54/** 円グラフ幅 */
55define('PIE_WIDTH', 230);
56
57/** 円グラフ高さ */
58define('PIE_HEIGHT', 100);
59
60/** 円グラフ太さ */
61define('PIE_THICK', 30);
62
63/** 円グラフのラベル位置を上にあげる */
64define('PIE_LABEL_UP', 20);
65
66/** 値が大きいほど影が長くなる */
67define('PIE_SHADE_IMPACT', 0.1);
68
69// -----------------------------------------------------------------------------
70// 折れ線グラフ
71// -----------------------------------------------------------------------------
72/** Y軸の目盛り数 */
73define('LINE_Y_SCALE', 10);
74
75/** X軸の目盛り数 */
76define('LINE_X_SCALE', 10);
77
78/** 線グラフ位置 */
79define('LINE_LEFT', 60);
80
81/** 線グラフ位置 */
82define('LINE_TOP', 50);
83
84/** 線グラフ背景のサイズ */
85define('LINE_AREA_WIDTH', 600);
86
87/** 線グラフ背景のサイズ */
88define('LINE_AREA_HEIGHT', 300);
89
90/** 線グラフマークのサイズ */
91define('LINE_MARK_SIZE', 6);
92
93/** 目盛り幅 */
94define('LINE_SCALE_SIZE', 6);
95
96/** X軸のラベルの表示制限数 */
97define('LINE_XLABEL_MAX', 30);
98
99/** X軸のタイトルと軸の間隔 */
100define('LINE_XTITLE_PAD', -5);
101
102/** Y軸のタイトルと軸の間隔 */
103define('LINE_YTITLE_PAD', 15);
104
105// -----------------------------------------------------------------------------
106//  棒グラフ
107// -----------------------------------------------------------------------------
108/** グラフと目盛りの間隔 */
109define('BAR_PAD', 6);
110
111// -----------------------------------------------------------------------------
112//  タイトルラベル
113// -----------------------------------------------------------------------------
114/** 背景枠との上幅 */
115define('TITLE_TOP', 10);
116
117// -----------------------------------------------------------------------------
118//  凡例
119// -----------------------------------------------------------------------------
120/** 背景枠との上幅 */
121define('LEGEND_TOP', 10);
122
123/** 背景枠との右幅 */
124define('LEGEND_RIGHT', 10);
125
126/**
127 * SC_Graph 共通クラス.
128 *
129 * @package Graph
130 * @author LOCKON CO.,LTD.
131 * @version $Id$
132 */
133class SC_Graph_Base
134{
135
136    // {{{ properties
137
138    var $arrRGB;
139    var $arrColor;
140    var $arrDarkColor;
141    var $image;
142    var $left;
143    var $top;
144    var $shade_color;
145    var $flame_color;
146    var $shade_on;
147    var $text_color;
148    var $labelbg_color;
149    var $bgw;
150    var $bgh;
151    var $clabelbg_color;
152    var $title_color;
153    var $text_top;
154    var $mark_color;
155    var $arrLegend;
156
157    /** グラフ背景 */
158    var $ARR_GRAPH_RGB;
159
160    /** 背景色 */
161    var $ARR_BG_COLOR;
162
163    /** 影の色 */
164    var $ARR_SHADE_COLOR;
165
166    /** 縁の色 */
167    var $ARR_FLAME_COLOR;
168
169    /** 文字色 */
170    var $ARR_TEXT_COLOR;
171
172    /** ラベル背景 */
173    var $ARR_LABELBG_COLOR;
174
175    /** 凡例背景 */
176    var $ARR_LEGENDBG_COLOR;
177
178    /** タイトル文字色 */
179    var $ARR_TITLE_COLOR;
180
181    /** グリッド線色 */
182    var $ARR_GRID_COLOR;
183
184    // コンストラクタ
185    function __construct($bgw, $bgh, $left, $top)
186    {
187        $this->init();
188        // 画像作成
189        $this->bgw = $bgw;
190        $this->bgh = $bgh;
191        $this->image = imagecreatetruecolor($bgw, $bgh);
192        // アンチエイリアス有効
193        if (function_exists('imageantialias')) imageantialias($this->image, true);
194        // 背景色をセット
195        imagefill($this->image, 0, 0, $this->lfGetImageColor($this->image, $this->ARR_BG_COLOR));
196
197        // 使用色の生成
198        $this->setColorList($this->ARR_GRAPH_RGB);
199        // グラフ描画位置の設定
200        $this->left = $left;
201        $this->top = $top;
202        $this->shade_color = $this->lfGetImageColor($this->image, $this->ARR_SHADE_COLOR);
203        $this->flame_color = $this->lfGetImageColor($this->image, $this->ARR_FLAME_COLOR);
204        $this->text_color = $this->lfGetImageColor($this->image, $this->ARR_TEXT_COLOR);
205        $this->labelbg_color = $this->lfGetImageColor($this->image, $this->ARR_LABELBG_COLOR);
206        $this->clabelbg_color = $this->lfGetImageColor($this->image, $this->ARR_LEGENDBG_COLOR);
207        $this->title_color = $this->lfGetImageColor($this->image, $this->ARR_TITLE_COLOR);
208        $this->grid_color = $this->lfGetImageColor($this->image, $this->ARR_GRID_COLOR);
209
210        // 影あり
211        $this->shade_on = true;
212    }
213
214    // リサンプル(画像を滑らかに縮小する)
215    function resampled()
216    {
217        $new_width = $this->bgw * 0.8;
218        $new_height = $this->bgh * 0.8;
219        $tmp_image = imagecreatetruecolor($new_width, $new_height);
220        if (imagecopyresampled($tmp_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->bgw, $this->bgh)) {
221            $this->image = $tmp_image;
222        }
223    }
224
225    // オブジェクトカラーの設定
226    function setColorList($arrRGB)
227    {
228        $this->arrRGB = $arrRGB;
229        $count = count($this->arrRGB);
230        // 通常色の設定
231        for ($i = 0; $i < $count; $i++) {
232            $this->arrColor[$i] = $this->lfGetImageColor($this->image, $this->arrRGB[$i]);
233        }
234        // 暗色の設定
235        for ($i = 0; $i < $count; $i++) {
236            $this->arrDarkColor[$i] = $this->lfGetImageDarkColor($this->image, $this->arrRGB[$i]);
237        }
238    }
239
240    // 影のありなし
241    function setShadeOn($shade_on)
242    {
243        $this->shade_on = $shade_on;
244    }
245
246    // 画像を出力する
247    function outputGraph($header = true, $filename = '')
248    {
249        if ($header) {
250            header('Content-type: image/png');
251        }
252
253        if ($filename != '') {
254            imagepng($this->image, $filename);
255        } else {
256            imagepng($this->image);
257        }
258
259        imagedestroy($this->image);
260    }
261
262    // 描画時のテキスト幅を求める
263    function getTextWidth($text, $font_size)
264    {
265        $text_len = strlen($text);
266        $ret = $font_size * $text_len * TEXT_RATE;
267        /*
268            ※正確な値が取得できなかったので廃止
269            // テキスト幅の取得
270            $arrPos = imagettfbbox($font_size, 0, FONT_REALFILE, $text);
271            $ret = $arrPos[2] - $arrPos[0];
272        */
273        return $ret;
274    }
275
276    // テキストを出力する
277    function setText($font_size, $left, $top, $text, $color = NULL, $angle = 0, $labelbg = false)
278    {
279        // 時計回りに角度を変更
280        $angle = -$angle;
281        // ラベル背景
282        if ($labelbg) {
283            $text_width = $this->getTextWidth($text, $font_size);
284            imagefilledrectangle($this->image, $left - 2, $top - 2, $left + $text_width + 2, $top + $font_size + 2, $this->labelbg_color);
285        }
286        /*
287         * XXX EUC-JP にしないと Warning がでる.
288         *     --enable-gd-jis-conv も関係していそうだが, このオプションを
289         *     つけなくても出る.
290         *
291         *     Warning: imagettftext() [function.imagettftext]:
292         *     any2eucjp(): something happen in
293         *
294         *     PHP Bugs: #42218
295         *
296         *     http://www.php.net/imagettftext を見ると, UTF-8 にしろと
297         *     書いてあるのに...
298         *
299         */
300        $text = mb_convert_encoding($text, 'EUC-JP', CHAR_CODE);
301        //$text = mb_convert_encoding($text, CHAR_CODE);
302        if ($color != NULL) {
303            ImageTTFText($this->image, $font_size, $angle, $left, $top + $font_size, $color, FONT_REALFILE, $text);
304        } else {
305            ImageTTFText($this->image, $font_size, $angle, $left, $top + $font_size, $this->text_color, FONT_REALFILE, $text);
306        }
307    }
308
309    // タイトルを出力する
310    function drawTitle($text, $font_size = TITLE_FONT_SIZE)
311    {
312        // 出力位置の算出
313        $text_width = $this->getTextWidth($text, $font_size);
314        $left = ($this->bgw - $text_width) / 2;
315        $top = TITLE_TOP;
316        $this->setText($font_size, $left, $top, $text, $this->title_color);
317    }
318
319    // ログを出力する
320    function debugPrint($text)
321    {
322        $text = mb_convert_encoding($text, 'UTF-8', CHAR_CODE);
323        if (!isset($this->text_top)) {
324            $this->text_top = FONT_SIZE + LINE_PAD;
325        }
326        // テキスト描画
327        ImageTTFText($this->image, FONT_SIZE, 0, LINE_PAD, $this->text_top, $this->text_color, FONT_REALFILE, $text);
328        $this->text_top += FONT_SIZE + LINE_PAD;
329    }
330
331    // カラーラベルを描画
332    function drawLegend($legend_max = '', $clabelbg = true)
333    {
334        // 凡例が登録されていなければ中止
335        if (count($this->arrLegend) <= 0) {
336            return;
337        }
338
339        if ($legend_max != '') {
340            $label_max = $legend_max;
341        } else {
342            $label_max = count($this->arrLegend);
343        }
344
345        $height_max = 0;
346        $text_max = 0;
347        $width_max = 0;
348
349        // 一番文字数が多いものを取得
350        for ($i = 0; $i < $label_max; $i++) {
351            $text_len = strlen($this->arrLegend[$i]);
352            if ($text_max < $text_len) {
353                $text_max = $text_len;
354            }
355            $height_max += FONT_SIZE + LINE_PAD;
356        }
357        $width_max = FONT_SIZE * $text_max * TEXT_RATE;
358
359        //  カラーアイコンと文字間を含めた幅
360        $width_max += FONT_SIZE + (LINE_PAD * 2);
361        $left = $this->bgw - $width_max - LEGEND_RIGHT;
362        $top = LEGEND_TOP;
363        // カラーラベル背景の描画
364        if ($clabelbg) {
365            $this->drawClabelBG($left - LINE_PAD, $top, $left + $width_max, $top + $height_max + LINE_PAD);
366        }
367        $top += LINE_PAD;
368
369        // 色数の取得
370        $c_max = count($this->arrColor);
371        for ($i = 0; $i < $label_max; $i++) {
372            // カラーアイコンの表示
373            imagerectangle($this->image, $left, $top, $left + FONT_SIZE, $top + FONT_SIZE, $this->flame_color);
374            imagefilledrectangle($this->image, $left + 1, $top + 1, $left + FONT_SIZE - 1, $top + FONT_SIZE - 1, $this->arrColor[($i % $c_max)]);
375            // ラベルの表示
376            $this->setText(FONT_SIZE, $left + FONT_SIZE + LINE_PAD, $top, $this->arrLegend[$i]);
377            $top += FONT_SIZE + LINE_PAD;
378        }
379    }
380
381    // カラーラベル背景の描画
382    function drawClabelBG($left, $top, $right, $bottom)
383    {
384        // 影の描画
385        if ($this->shade_on) {
386            imagefilledrectangle($this->image, $left + 2, $top + 2, $right + 2, $bottom + 2, $this->shade_color);
387        }
388        // カラーラベル背景の描画
389        imagefilledrectangle($this->image, $left, $top, $right, $bottom, $this->clabelbg_color);
390        imagerectangle($this->image, $left, $top, $right, $bottom, $this->flame_color);
391    }
392
393    // 凡例をセットする
394    function setLegend($arrLegend)
395    {
396        $this->arrLegend = array_values((array)$arrLegend);
397    }
398
399    // }}}
400    // {{{ protected functions
401
402    /**
403     * クラスの初期化を行う.
404     *
405     * 表示色をメンバ変数にセットする.
406     *
407     * @access protected
408     * @return void
409     */
410    function init()
411    {
412        // 凡例背景
413        $this->ARR_LEGENDBG_COLOR = array(245,245,245);
414        // ラベル背景
415        $this->ARR_LABELBG_COLOR = array(255,255,255);
416        // グラフカラー
417        $this->ARR_GRAPH_RGB = array(
418            array(200,50,50),
419            array(50,50,200),
420            array(50,200,50),
421            array(255,255,255),
422            array(244,200,200),
423            array(200,200,255),
424            array(50,200,50),
425            array(255,255,255),
426            array(244,244,244),
427        );
428        // 影の色
429        $this->ARR_SHADE_COLOR = array(100,100,100);
430        // 縁の色
431        $this->ARR_FLAME_COLOR = array(0, 0, 0);
432        // 文字色
433        $this->ARR_TEXT_COLOR = array(0, 0, 0);
434        // 背景カラー
435        $this->ARR_BG_COLOR = array(255,255,255);
436        // タイトル文字色
437        $this->ARR_TITLE_COLOR = array(0, 0, 0);
438        // グリッド線色
439        $this->ARR_GRID_COLOR = array(200, 200, 200);
440        // マークの色
441        $this->ARR_MARK_COLOR = array(130, 130, 255);
442    }
443
444    /**
445     * 円の中心点と直径から弧の終端座標を算出する.
446     *
447     * @param integer $cx 中心点X座標
448     * @param integer $cy 中心点Y座標
449     * @param integer $r 半径
450     * @param integer $e 角度
451     * @return array 円の中心点と直径から弧の終端座標の配列
452     */
453    function lfGetArcPos($cx, $cy, $cw, $ch, $e)
454    {
455        // 三角関数用の角度を求める
456        $s = 90 - $e;
457        $r = $cw / 2;
458        // 位置を求める
459        $x = $cx + ($r * cos(deg2rad($s)));
460        $y = $cy - (($r * sin(deg2rad($s))) * ($ch / $cw));
461        return array(round($x), round($y));
462    }
463
464    /** 画像にテキストを描画する */
465    function lfImageText($dst_image, $text, $font_size, $left, $top, $font, $arrRGB)
466    {
467        $color = ImageColorAllocate($dst_image, $arrRGB[0], $arrRGB[1], $arrRGB[2]);
468        $text = mb_convert_encoding($text, 'UTF-8', CHAR_CODE);
469        // 表示角度
470        $angle = 0;
471        // テキスト描画
472        ImageTTFText($dst_image, $font_size, $angle, $left, $top, $color, $font, $text);
473    }
474
475    /** 表示色の取得 */
476    function lfGetImageColor($image, $array)
477    {
478        if (count($array) != 3) {
479            return NULL;
480        }
481        $ret = imagecolorallocate($image, $array[0], $array[1], $array[2]);
482        return $ret;
483    }
484
485    /** 影用表示色の取得 */
486    function lfGetImageDarkColor($image, $array)
487    {
488        if (count($array) != 3) {
489            return NULL;
490        }
491        $i = 0;
492        foreach ($array as $val) {
493            $dark[$i] = $val - 45;
494            if ($dark[$i] < 0) {
495                $dark[$i] = 0;
496            }
497            $i++;
498        }
499        $ret = imagecolorallocate($image, $dark[0], $dark[1], $dark[2]);
500        return $ret;
501    }
502}
Note: See TracBrowser for help on using the repository browser.