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

Revision 23605, 15.5 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/** 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    public $arrRGB;
136    public $arrColor;
137    public $arrDarkColor;
138    public $image;
139    public $left;
140    public $top;
141    public $shade_color;
142    public $flame_color;
143    public $shade_on;
144    public $text_color;
145    public $labelbg_color;
146    public $bgw;
147    public $bgh;
148    public $clabelbg_color;
149    public $title_color;
150    public $text_top;
151    public $mark_color;
152    public $arrLegend;
153
154    /** グラフ背景 */
155    public $ARR_GRAPH_RGB;
156
157    /** 背景色 */
158    public $ARR_BG_COLOR;
159
160    /** 影の色 */
161    public $ARR_SHADE_COLOR;
162
163    /** 縁の色 */
164    public $ARR_FLAME_COLOR;
165
166    /** 文字色 */
167    public $ARR_TEXT_COLOR;
168
169    /** ラベル背景 */
170    public $ARR_LABELBG_COLOR;
171
172    /** 凡例背景 */
173    public $ARR_LEGENDBG_COLOR;
174
175    /** タイトル文字色 */
176    public $ARR_TITLE_COLOR;
177
178    /** グリッド線色 */
179    public $ARR_GRID_COLOR;
180
181    // コンストラクタ
182    public function __construct($bgw, $bgh, $left, $top)
183    {
184        $this->init();
185        // 画像作成
186        $this->bgw = $bgw;
187        $this->bgh = $bgh;
188        $this->image = imagecreatetruecolor($bgw, $bgh);
189        // アンチエイリアス有効
190        if (function_exists('imageantialias')) imageantialias($this->image, true);
191        // 背景色をセット
192        imagefill($this->image, 0, 0, $this->lfGetImageColor($this->image, $this->ARR_BG_COLOR));
193
194        // 使用色の生成
195        $this->setColorList($this->ARR_GRAPH_RGB);
196        // グラフ描画位置の設定
197        $this->left = $left;
198        $this->top = $top;
199        $this->shade_color = $this->lfGetImageColor($this->image, $this->ARR_SHADE_COLOR);
200        $this->flame_color = $this->lfGetImageColor($this->image, $this->ARR_FLAME_COLOR);
201        $this->text_color = $this->lfGetImageColor($this->image, $this->ARR_TEXT_COLOR);
202        $this->labelbg_color = $this->lfGetImageColor($this->image, $this->ARR_LABELBG_COLOR);
203        $this->clabelbg_color = $this->lfGetImageColor($this->image, $this->ARR_LEGENDBG_COLOR);
204        $this->title_color = $this->lfGetImageColor($this->image, $this->ARR_TITLE_COLOR);
205        $this->grid_color = $this->lfGetImageColor($this->image, $this->ARR_GRID_COLOR);
206
207        // 影あり
208        $this->shade_on = true;
209    }
210
211    // リサンプル(画像を滑らかに縮小する)
212    public function resampled()
213    {
214        $new_width = $this->bgw * 0.8;
215        $new_height = $this->bgh * 0.8;
216        $tmp_image = imagecreatetruecolor($new_width, $new_height);
217        if (imagecopyresampled($tmp_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->bgw, $this->bgh)) {
218            $this->image = $tmp_image;
219        }
220    }
221
222    // オブジェクトカラーの設定
223    public function setColorList($arrRGB)
224    {
225        $this->arrRGB = $arrRGB;
226        $count = count($this->arrRGB);
227        // 通常色の設定
228        for ($i = 0; $i < $count; $i++) {
229            $this->arrColor[$i] = $this->lfGetImageColor($this->image, $this->arrRGB[$i]);
230        }
231        // 暗色の設定
232        for ($i = 0; $i < $count; $i++) {
233            $this->arrDarkColor[$i] = $this->lfGetImageDarkColor($this->image, $this->arrRGB[$i]);
234        }
235    }
236
237    // 影のありなし
238    public function setShadeOn($shade_on)
239    {
240        $this->shade_on = $shade_on;
241    }
242
243    // 画像を出力する
244    public function outputGraph($header = true, $filename = '')
245    {
246        if ($header) {
247            header('Content-type: image/png');
248        }
249
250        if ($filename != '') {
251            imagepng($this->image, $filename);
252        } else {
253            imagepng($this->image);
254        }
255
256        imagedestroy($this->image);
257    }
258
259    // 描画時のテキスト幅を求める
260    public function getTextWidth($text, $font_size)
261    {
262        $text_len = strlen($text);
263        $ret = $font_size * $text_len * TEXT_RATE;
264        /*
265            ※正確な値が取得できなかったので廃止
266            // テキスト幅の取得
267            $arrPos = imagettfbbox($font_size, 0, FONT_REALFILE, $text);
268            $ret = $arrPos[2] - $arrPos[0];
269        */
270
271        return $ret;
272    }
273
274    // テキストを出力する
275
276    /**
277     * @param integer $color
278     */
279    public function setText($font_size, $left, $top, $text, $color = NULL, $angle = 0, $labelbg = false)
280    {
281        // 時計回りに角度を変更
282        $angle = -$angle;
283        // ラベル背景
284        if ($labelbg) {
285            $text_width = $this->getTextWidth($text, $font_size);
286            imagefilledrectangle($this->image, $left - 2, $top - 2, $left + $text_width + 2, $top + $font_size + 2, $this->labelbg_color);
287        }
288        /*
289         * XXX EUC-JP にしないと Warning がでる.
290         *     --enable-gd-jis-conv も関係していそうだが, このオプションを
291         *     つけなくても出る.
292         *
293         *     Warning: imagettftext() [function.imagettftext]:
294         *     any2eucjp(): something happen in
295         *
296         *     PHP Bugs: #42218
297         *
298         *     http://www.php.net/imagettftext を見ると, UTF-8 にしろと
299         *     書いてあるのに...
300         *
301         */
302        $text = mb_convert_encoding($text, 'EUC-JP', CHAR_CODE);
303        //$text = mb_convert_encoding($text, CHAR_CODE);
304        if ($color != NULL) {
305            ImageTTFText($this->image, $font_size, $angle, $left, $top + $font_size, $color, FONT_REALFILE, $text);
306        } else {
307            ImageTTFText($this->image, $font_size, $angle, $left, $top + $font_size, $this->text_color, FONT_REALFILE, $text);
308        }
309    }
310
311    // タイトルを出力する
312
313    /**
314     * @param string $text
315     */
316    public function drawTitle($text, $font_size = TITLE_FONT_SIZE)
317    {
318        // 出力位置の算出
319        $text_width = $this->getTextWidth($text, $font_size);
320        $left = ($this->bgw - $text_width) / 2;
321        $top = TITLE_TOP;
322        $this->setText($font_size, $left, $top, $text, $this->title_color);
323    }
324
325    // ログを出力する
326    public function debugPrint($text)
327    {
328        $text = mb_convert_encoding($text, 'UTF-8', CHAR_CODE);
329        if (!isset($this->text_top)) {
330            $this->text_top = FONT_SIZE + LINE_PAD;
331        }
332        // テキスト描画
333        ImageTTFText($this->image, FONT_SIZE, 0, LINE_PAD, $this->text_top, $this->text_color, FONT_REALFILE, $text);
334        $this->text_top += FONT_SIZE + LINE_PAD;
335    }
336
337    // カラーラベルを描画
338    public function drawLegend($legend_max = '', $clabelbg = true)
339    {
340        // 凡例が登録されていなければ中止
341        if (count($this->arrLegend) <= 0) {
342            return;
343        }
344
345        if ($legend_max != '') {
346            $label_max = $legend_max;
347        } else {
348            $label_max = count($this->arrLegend);
349        }
350
351        $height_max = 0;
352        $text_max = 0;
353        $width_max = 0;
354
355        // 一番文字数が多いものを取得
356        for ($i = 0; $i < $label_max; $i++) {
357            $text_len = strlen($this->arrLegend[$i]);
358            if ($text_max < $text_len) {
359                $text_max = $text_len;
360            }
361            $height_max += FONT_SIZE + LINE_PAD;
362        }
363        $width_max = FONT_SIZE * $text_max * TEXT_RATE;
364
365        //  カラーアイコンと文字間を含めた幅
366        $width_max += FONT_SIZE + (LINE_PAD * 2);
367        $left = $this->bgw - $width_max - LEGEND_RIGHT;
368        $top = LEGEND_TOP;
369        // カラーラベル背景の描画
370        if ($clabelbg) {
371            $this->drawClabelBG($left - LINE_PAD, $top, $left + $width_max, $top + $height_max + LINE_PAD);
372        }
373        $top += LINE_PAD;
374
375        // 色数の取得
376        $c_max = count($this->arrColor);
377        for ($i = 0; $i < $label_max; $i++) {
378            // カラーアイコンの表示
379            imagerectangle($this->image, $left, $top, $left + FONT_SIZE, $top + FONT_SIZE, $this->flame_color);
380            imagefilledrectangle($this->image, $left + 1, $top + 1, $left + FONT_SIZE - 1, $top + FONT_SIZE - 1, $this->arrColor[($i % $c_max)]);
381            // ラベルの表示
382            $this->setText(FONT_SIZE, $left + FONT_SIZE + LINE_PAD, $top, $this->arrLegend[$i]);
383            $top += FONT_SIZE + LINE_PAD;
384        }
385    }
386
387    // カラーラベル背景の描画
388
389    /**
390     * @param double $left
391     * @param double $right
392     * @param integer $bottom
393     */
394    public function drawClabelBG($left, $top, $right, $bottom)
395    {
396        // 影の描画
397        if ($this->shade_on) {
398            imagefilledrectangle($this->image, $left + 2, $top + 2, $right + 2, $bottom + 2, $this->shade_color);
399        }
400        // カラーラベル背景の描画
401        imagefilledrectangle($this->image, $left, $top, $right, $bottom, $this->clabelbg_color);
402        imagerectangle($this->image, $left, $top, $right, $bottom, $this->flame_color);
403    }
404
405    // 凡例をセットする
406    public function setLegend($arrLegend)
407    {
408        $this->arrLegend = array_values((array) $arrLegend);
409    }
410
411    /**
412     * クラスの初期化を行う.
413     *
414     * 表示色をメンバ変数にセットする.
415     *
416     * @access protected
417     * @return void
418     */
419    public function init()
420    {
421        // 凡例背景
422        $this->ARR_LEGENDBG_COLOR = array(245,245,245);
423        // ラベル背景
424        $this->ARR_LABELBG_COLOR = array(255,255,255);
425        // グラフカラー
426        $this->ARR_GRAPH_RGB = array(
427            array(200,50,50),
428            array(50,50,200),
429            array(50,200,50),
430            array(255,255,255),
431            array(244,200,200),
432            array(200,200,255),
433            array(50,200,50),
434            array(255,255,255),
435            array(244,244,244),
436        );
437        // 影の色
438        $this->ARR_SHADE_COLOR = array(100,100,100);
439        // 縁の色
440        $this->ARR_FLAME_COLOR = array(0, 0, 0);
441        // 文字色
442        $this->ARR_TEXT_COLOR = array(0, 0, 0);
443        // 背景カラー
444        $this->ARR_BG_COLOR = array(255,255,255);
445        // タイトル文字色
446        $this->ARR_TITLE_COLOR = array(0, 0, 0);
447        // グリッド線色
448        $this->ARR_GRID_COLOR = array(200, 200, 200);
449        // マークの色
450        $this->ARR_MARK_COLOR = array(130, 130, 255);
451    }
452
453    /**
454     * 円の中心点と直径から弧の終端座標を算出する.
455     *
456     * @param  integer $cx 中心点X座標
457     * @param  integer $cy 中心点Y座標
458     * @param  integer $e  角度
459     * @return double[]   円の中心点と直径から弧の終端座標の配列
460     */
461    public function lfGetArcPos($cx, $cy, $cw, $ch, $e)
462    {
463        // 三角関数用の角度を求める
464        $s = 90 - $e;
465        $r = $cw / 2;
466        // 位置を求める
467        $x = $cx + ($r * cos(deg2rad($s)));
468        $y = $cy - (($r * sin(deg2rad($s))) * ($ch / $cw));
469
470        return array(round($x), round($y));
471    }
472
473    /** 画像にテキストを描画する */
474    public function lfImageText($dst_image, $text, $font_size, $left, $top, $font, $arrRGB)
475    {
476        $color = ImageColorAllocate($dst_image, $arrRGB[0], $arrRGB[1], $arrRGB[2]);
477        $text = mb_convert_encoding($text, 'UTF-8', CHAR_CODE);
478        // 表示角度
479        $angle = 0;
480        // テキスト描画
481        ImageTTFText($dst_image, $font_size, $angle, $left, $top, $color, $font, $text);
482    }
483
484    /** 表示色の取得 */
485    public function lfGetImageColor($image, $array)
486    {
487        if (count($array) != 3) {
488            return NULL;
489        }
490        $ret = imagecolorallocate($image, $array[0], $array[1], $array[2]);
491
492        return $ret;
493    }
494
495    /** 影用表示色の取得 */
496    public function lfGetImageDarkColor($image, $array)
497    {
498        if (count($array) != 3) {
499            return NULL;
500        }
501        $i = 0;
502        foreach ($array as $val) {
503            $dark[$i] = $val - 45;
504            if ($dark[$i] < 0) {
505                $dark[$i] = 0;
506            }
507            $i++;
508        }
509        $ret = imagecolorallocate($image, $dark[0], $dark[1], $dark[2]);
510
511        return $ret;
512    }
513}
Note: See TracBrowser for help on using the repository browser.