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

Revision 23605, 7.6 KB checked in by kimoto, 12 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// 円グラフ生成クラス
25class SC_Graph_Pie extends SC_Graph_Base_Ex
26{
27    public $cw;
28    public $ch;
29    public $cz;
30    public $cx;
31    public $cy;
32    public $arrLabel;
33    public $arrData;
34
35    // コンストラクタ
36    public 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    public 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
75        return $arrRet;
76    }
77
78    // 円の位置設定を行う
79
80    /**
81     * @param double $cx
82     * @param double $cy
83     */
84    public function setPosition($cx, $cy)
85    {
86        $this->cx = $cx;
87        $this->cy = $cy;
88    }
89
90    // 円のサイズ設定を行う
91    public function setSize($cw, $ch, $cz = 0)
92    {
93        $this->cw = $cw;
94        $this->ch = $ch;
95        $this->cz = $cz;
96    }
97
98    // 影の描画
99    public function drawShade()
100    {
101        $move = 1;
102        for ($i = ($this->cy + $this->cz); $i <= ($this->cy + $this->cz + ($this->cz * PIE_SHADE_IMPACT)); $i++) {
103            imagefilledarc($this->image, $this->cx + $move, $i, $this->cw, $this->ch, 0, 360, $this->shade_color, IMG_ARC_PIE);
104            $move += 0.5;
105        }
106    }
107
108    // データをセットする
109    public function setData($arrData)
110    {
111        $this->arrData = array_values($arrData);
112    }
113
114    // 円グラフを描画する
115    public function drawGraph()
116    {
117        $x = $this->cx;
118        $y = $this->cy;
119        $z = $this->cz;
120        $h = $this->ch;
121        $w = $this->cw;
122
123        // データの角度を取得する
124        $arrRad = $this->getCircleData($this->arrData);
125
126        // データが存在しない場合
127        if (empty($arrRad)) {
128            return;
129        }
130
131        // 影の描画
132        if ($this->shade_on) {
133            $this->drawShade();
134        }
135
136        // 色数の取得
137        $c_max = count($this->arrColor);
138        $dc_max = count($this->arrDarkColor);
139
140        // 側面の描画
141        for ($i = ($y + $z - 1); $i >= $y; $i--) {
142            $start = 0;
143            foreach ($arrRad as $rad) {
144                // 角度が0度以上の場合のみ側面を描画する。
145                if ($rad > 0) {
146                    $end = $start + $rad;
147                    if ($start == 0 && $end == 360) {
148                        // -90~270で指定すると円が描画できないので0~360に指定
149                        imagearc($this->image, $x, $i, $w, $h, 0, 360, $this->arrDarkColor[($j % $dc_max)]);
150                    } else {
151                        // -90°は12時の位置から開始するように補正している
152                        imagearc($this->image, $x, $i, $w, $h, $start - 90, $end - 90, $this->arrDarkColor[($j % $dc_max)]);
153                    }
154                    $start = $end;
155                }
156            }
157        }
158        // 底面の描画
159        imagearc($this->image, $x, $y + $z, $w, $h, 0, 180, $this->flame_color);
160
161        // 上面の描画
162        $start = 0;
163        foreach ($arrRad as $key => $rad) {
164            $end = $start + $rad;
165            // 開始・終了が同一値だと、(imagefilledarc 関数における) 0°から360°として動作するようなので、スキップする。
166            // XXX 値ラベルは別ロジックなので、実質問題を生じないと考えている。
167            if ($start == $end) {
168                continue 1;
169            }
170            // -90°は12時の位置から開始するように補正するもの。
171            // 塗りつぶし
172            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->arrColor[($key % $c_max)], $style);
173            // FIXME 360°描画の場合、(imagefilledarc 関数における) 0°から360°として動作する。本来-90°から360°として動作すべき。
174            //       なお、360°と0°の組み合わせを考慮すると線が無いのも問題があるので、この処理をスキップする対応は不適当である。
175            // 縁取り線
176            imagefilledarc($this->image, $x, $y, $w, $h, $start - 90, $end - 90, $this->flame_color, IMG_ARC_EDGED|IMG_ARC_NOFILL);
177            $start = $end;
178        }
179
180        // 側面の縁取り
181        imageline($this->image, $x + ($w / 2), $y, $x + ($w / 2), $y + $z, $this->flame_color);
182        imageline($this->image, $x - ($w / 2), $y, $x - ($w / 2), $y + $z, $this->flame_color);
183        $start = 0;
184        foreach ($arrRad as $rad) {
185            $end = $start + $rad;
186            // 前面のみ
187            if ($end > 90 && $end < 270) {
188                list($ax, $ay) = $this->lfGetArcPos($x, $y, $w, $h, $end);
189                // ラインのずれを補正する
190                if ($end > 180) {
191                    $ax = $ax + 1;
192                }
193                imageline($this->image, $ax, $ay, $ax, $ay + $z, $this->flame_color);
194            }
195            $start = $end;
196        }
197
198        // ラベルの描画
199        $this->drawLabel($arrRad);
200        // 凡例の描画
201        $this->drawLegend(count($this->arrData));
202    }
203
204    // 円グラフのラベルを描画する
205    public function drawLabel($arrRad)
206    {
207        $start = 0;
208        foreach ($arrRad as $key => $rad) {
209            $center = $start + ($rad / 2);
210            $end = $start + $rad;
211            list($sx, $sy) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw / 1.5), ($this->ch / 1.5), $center);
212            list($ex, $ey) = $this->lfGetArcPos($this->cx, $this->cy, ($this->cw * 1.5), ($this->ch * 1.5), $center);
213            // 指示線の描画
214            imageline($this->image, $sx, $sy, $ex + 2, $ey - PIE_LABEL_UP, $this->flame_color);
215            $this->setText(FONT_SIZE, $ex - 10, $ey - PIE_LABEL_UP - FONT_SIZE, $this->arrLabel[$key], NULL, 0, true);
216            $start = $end;
217        }
218    }
219}
Note: See TracBrowser for help on using the repository browser.