source: branches/version-2_12-dev/data/class/graph/SC_Graph_Pie.php @ 22585

Revision 22585, 7.4 KB checked in by Seasoft, 13 years ago (diff)

#2163 (円グラフ 角度0°の値があると不適切な塗りつぶしが行なわれる)
#2044 (無駄な処理を改善する for 2.12.4)

  • 2つのループの統合

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)

  • for -> foreach
  • 既存の軽微な不具合について記載
  • 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_Pie extends SC_Graph_Base_Ex
26{
27    var $cw;
28    var $ch;
29    var $cz;
30    var $cx;
31    var $cy;
32    var $arrLabel;
33    var $arrData;
34
35    // コンストラクタ
36    function __construct($bgw = BG_WIDTH, $bgh = BG_HEIGHT, $left = PIE_LEFT, $top = PIE_TOP)
37    {
38        parent::__construct($bgw, $bgh, $left, $top);
39        // サイズ設定
40        $this->setSize(PIE_WIDTH, PIE_HEIGHT, PIE_THICK);
41        // 位置設定
42        $this->setPosition($this->left + ($this->cw / 2), $this->top + ($this->ch / 2));
43    }
44
45    // データを360°値に変換する
46    function getCircleData($array)
47    {
48        $total = '';
49        $new_total = '';
50        if (!is_array($array)) {
51            return;
52        }
53        $arrRet = array();
54        foreach ($array as $val) {
55            $total += $val;
56        }
57        if ($total <= 0) {
58            return;
59        }
60        $rate = 360 / $total;
61        // ラベル表示用
62        $p_rate = 100 / $total;
63        $cnt = 0;
64        foreach ($array as $val) {
65            $ret = round($val * $rate);
66            $new_total+= $ret;
67            $arrRet[] = $ret;
68            // パーセント表示用
69            $this->arrLabel[] = round($val * $p_rate) . ' %';
70            $cnt++;
71        }
72        // 合計が360になるように補正しておく
73        $arrRet[0] -= $new_total - 360;
74        return $arrRet;
75    }
76
77    // 円の位置設定を行う
78    function setPosition($cx, $cy)
79    {
80        $this->cx = $cx;
81        $this->cy = $cy;
82    }
83
84    // 円のサイズ設定を行う
85    function setSize($cw, $ch, $cz = 0)
86    {
87        $this->cw = $cw;
88        $this->ch = $ch;
89        $this->cz = $cz;
90    }
91
92    // 影の描画
93    function drawShade()
94    {
95        $move = 1;
96        for ($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
97            imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
98            $move += 0.5;
99        }
100    }
101
102    // データをセットする
103    function setData($arrData)
104    {
105        $this->arrData = array_values($arrData);
106    }
107
108    // 円グラフを描画する
109    function drawGraph()
110    {
111        $x = $this->cx;
112        $y = $this->cy;
113        $z = $this->cz;
114        $h = $this->ch;
115        $w = $this->cw;
116
117        // データの角度を取得する
118        $arrRad = $this->getCircleData($this->arrData);
119
120        // データが存在しない場合
121        if (empty($arrRad)) {
122            return;
123        }
124
125        // 影の描画
126        if ($this->shade_on) {
127            $this->drawShade();
128        }
129
130        // 色数の取得
131        $c_max = count($this->arrColor);
132        $dc_max = count($this->arrDarkColor);
133
134        // 側面の描画
135        for ($i = ($y + $z - 1); $i >= $y; $i--) {
136            $start = 0;
137            foreach ($arrRad as $rad) {
138                // 角度が0度以上の場合のみ側面を描画する。
139                if ($rad > 0) {
140                    $end = $start + $rad;
141                    if ($start == 0 && $end == 360) {
142                        // -90~270で指定すると円が描画できないので0~360に指定
143                        imagearc($this->image, $x, $i, $w, $h, 0, 360, $this->arrDarkColor[($j % $dc_max)]);
144                    } else {
145                        // -90°は12時の位置から開始するように補正している
146                        imagearc($this->image, $x, $i, $w, $h, $start - 90, $end - 90, $this->arrDarkColor[($j % $dc_max)]);
147                    }
148                    $start = $end;
149                }
150            }
151        }
152        // 底面の描画
153        imagearc($this->image, $x, $y + $z, $w, $h, 0, 180 , $this->flame_color);
154
155        // 上面の描画
156        $start = 0;
157        foreach ($arrRad as $key => $rad) {
158            $end = $start + $rad;
159            // 開始・終了が同一値だと、(imagefilledarc 関数における) 0°から360°として動作するようなので、スキップする。
160            // XXX 値ラベルは別ロジックなので、実質問題を生じないと考えている。
161            if ($start == $end) {
162                continue 1;
163            }
164            // -90°は12時の位置から開始するように補正するもの。
165            // 塗りつぶし
166            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($key % $c_max)], $style);
167            // FIXME 360°描画の場合、(imagefilledarc 関数における) 0°から360°として動作する。本来-90°から360°として動作すべき。
168            //       なお、360°と0°の組み合わせを考慮すると線が無いのも問題があるので、この処理をスキップする対応は不適当である。
169            // 縁取り線
170            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
171            $start = $end;
172        }
173
174        // 側面の縁取り
175        imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
176        imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
177        $start = 0;
178        foreach ($arrRad as $rad) {
179            $end = $start + $rad;
180            // 前面のみ
181            if ($end > 90 && $end < 270) {
182                list($ax, $ay) = $this->lfGetArcPos($x, $y, $w, $h, $end);
183                // ラインのずれを補正する
184                if ($end > 180) {
185                    $ax = $ax + 1;
186                }
187                imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
188            }
189            $start = $end;
190        }
191
192        // ラベルの描画
193        $this->drawLabel($arrRad);
194        // 凡例の描画
195        $this->drawLegend(count($this->arrData));
196    }
197
198    // 円グラフのラベルを描画する
199    function drawLabel($arrRad)
200    {
201        $start = 0;
202        foreach ($arrRad as $key => $rad) {
203            $center = $start + ($rad / 2);
204            $end = $start + $rad;
205            list($sx, $sy) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
206            list($ex, $ey) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
207            // 指示線の描画
208            imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
209            $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$key], NULL, 0, true);
210            $start = $end;
211        }
212    }
213}
Note: See TracBrowser for help on using the repository browser.