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