[2] | 1 | <?php |
---|
| 2 | /*******************************************************************\ |
---|
| 3 | * eGroupWare - GD Graph * |
---|
| 4 | * http://www.egroupware.org * |
---|
| 5 | * This program is part of the GNU project, see http://www.gnu.org/ * |
---|
| 6 | * * |
---|
| 7 | * Written by Bettina Gille [ceb@phpgroupware.org] * |
---|
| 8 | * * |
---|
| 9 | * Creates graphical statistics using GD graphics library * |
---|
| 10 | * Copyright (C) 2003 Free Software Foundation, Inc * |
---|
| 11 | * ----------------------------------------------------------------- * |
---|
| 12 | * This class based on boGraph.php3 * |
---|
| 13 | * Double Choco Latte - Source Configuration Management System * |
---|
| 14 | * Copyright (C) 1999 Michael L. Dean & Tim R. Norman * |
---|
| 15 | * ----------------------------------------------------------------- * |
---|
| 16 | * This library is part of the eGroupWare API * |
---|
| 17 | * ----------------------------------------------------------------- * |
---|
| 18 | * This library is free software; you can redistribute it and/or * |
---|
| 19 | * modify it under the terms of the GNU General Public License as * |
---|
| 20 | * published by the Free Software Foundation; either version 2 of * |
---|
| 21 | * the License, or (at your option) any later version. * |
---|
| 22 | * * |
---|
| 23 | * This program is distributed in the hope that it will be useful, * |
---|
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 26 | * General Public License for more details. * |
---|
| 27 | * * |
---|
| 28 | * You should have received a copy of the GNU General Public License * |
---|
| 29 | * along with this program; if not, write to the Free Software * |
---|
| 30 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
---|
| 31 | \*******************************************************************/ |
---|
| 32 | |
---|
| 33 | class gdgraph |
---|
| 34 | { |
---|
| 35 | var $debug; |
---|
| 36 | var $title; |
---|
| 37 | var $caption_x; |
---|
| 38 | var $caption_y; |
---|
| 39 | var $lines_x; |
---|
| 40 | var $lines_y; |
---|
| 41 | var $line_captions_x; |
---|
| 42 | var $data; |
---|
| 43 | var $colors; |
---|
| 44 | var $color_legend; |
---|
| 45 | var $graph_width; |
---|
| 46 | var $graph_height; |
---|
| 47 | var $margin_top; |
---|
| 48 | var $margin_left; |
---|
| 49 | var $margin_bottom; |
---|
| 50 | var $margin_right; |
---|
| 51 | var $img; |
---|
| 52 | |
---|
| 53 | function gdgraph($debug = False) |
---|
| 54 | { |
---|
| 55 | $this->debug = $debug; |
---|
| 56 | |
---|
| 57 | $this->title = 'Gantt Chart'; |
---|
| 58 | |
---|
| 59 | $this->caption_x = 'x'; |
---|
| 60 | $this->caption_y = 'y'; |
---|
| 61 | |
---|
| 62 | $this->num_lines_x = 30; |
---|
| 63 | $this->num_lines_y = 10; |
---|
| 64 | |
---|
| 65 | $this->line_captions_x = array(); |
---|
| 66 | $this->line_captions_y = array(); |
---|
| 67 | |
---|
| 68 | $this->data = array(); |
---|
| 69 | |
---|
| 70 | $this->colors = array('red','green','blue','bright red','bright green','bright blue','dark red','dark green','dark blue'); |
---|
| 71 | $this->color_legend = array(); |
---|
| 72 | $this->color_extra = 'yellow'; |
---|
| 73 | |
---|
| 74 | $this->graph_width = 800; |
---|
| 75 | $this->graph_height = 400; |
---|
| 76 | |
---|
| 77 | $this->margin_top = 20; |
---|
| 78 | $this->margin_left = 80; |
---|
| 79 | $this->margin_bottom = 40; |
---|
| 80 | $this->margin_right = 20; |
---|
| 81 | |
---|
| 82 | $this->img = CreateObject('phpgwapi.gdimage'); |
---|
| 83 | $this->temp_file = $this->img->temp_file; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | function rRender() |
---|
| 87 | { |
---|
| 88 | // Initialize image - map white since it's our background |
---|
| 89 | $this->img->width = $this->graph_width; |
---|
| 90 | $this->img->height = $this->graph_height; |
---|
| 91 | $this->img->Init(); |
---|
| 92 | $this->img->SetColor(255, 255, 0); |
---|
| 93 | $this->img->ToBrowser(); |
---|
| 94 | $this->img->Done(); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | function Render() |
---|
| 98 | { |
---|
| 99 | // Initialize image - map white since it's our background |
---|
| 100 | $this->img->width = $this->graph_width; |
---|
| 101 | $this->img->height = $this->graph_height; |
---|
| 102 | $this->img->Init(); |
---|
| 103 | $this->img->SetColor(255, 255, 255, True); |
---|
| 104 | |
---|
| 105 | // Draw the captions |
---|
| 106 | $this->img->SetFont(3); |
---|
| 107 | $this->img->SetColor(0, 0, 0); |
---|
| 108 | $this->img->MoveTo($this->graph_width / 2, 2); |
---|
| 109 | $this->img->DrawText(array('text' => $this->title)); |
---|
| 110 | //$this->img->MoveTo(2, $this->graph_height / 2); |
---|
| 111 | //$this->img->DrawText($this->caption_y, 'up', 'center'); |
---|
| 112 | //$this->img->MoveTo($this->graph_width / 2, $this->graph_height - $this->img->GetFontHeight() - 2); |
---|
| 113 | //$this->img->DrawText($this->caption_x, '', 'center'); |
---|
| 114 | |
---|
| 115 | // Draw the two axis |
---|
| 116 | $this->img->Line($this->margin_left, $this->margin_top, $this->margin_left, $this->graph_height - $this->margin_bottom + 4); |
---|
| 117 | $this->img->Line($this->margin_left - 4, $this->graph_height - $this->margin_bottom, $this->graph_width - $this->margin_right, $this->graph_height - $this->margin_bottom); |
---|
| 118 | |
---|
| 119 | // Draw dashed lines for x axis |
---|
| 120 | $linespace = ($this->graph_width - $this->margin_left - $this->margin_right) / ($this->num_lines_x - 1); |
---|
[7655] | 121 | for ($i = 1; $i < $this->num_lines_x; ++$i) |
---|
[2] | 122 | { |
---|
| 123 | $x = $i * $linespace + $this->margin_left; |
---|
| 124 | $this->img->SetColor(0, 0, 0); |
---|
| 125 | $this->img->Line($x, $this->graph_height - $this->margin_bottom - 4, $x, $this->graph_height - $this->margin_bottom + 4); |
---|
| 126 | $this->img->SetColor(200, 200, 200); |
---|
| 127 | $this->img->Line($x, $this->margin_top, $x, $this->graph_height - $this->margin_bottom - 4, 'dashed'); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // Draw dashed lines for y axis |
---|
| 131 | $linespace = ($this->graph_height - $this->margin_top - $this->margin_bottom) / ($this->num_lines_y - 1); |
---|
[7655] | 132 | for ($i = 1; $i < $this->num_lines_y; ++$i) |
---|
[2] | 133 | { |
---|
| 134 | $y = $this->graph_height - $this->margin_bottom - ($i * $linespace); |
---|
| 135 | $this->img->SetColor(0, 0, 0); |
---|
| 136 | $this->img->Line($this->margin_left - 4, $y, $this->margin_left + 4, $y); |
---|
| 137 | $this->img->SetColor(200, 200, 200); |
---|
| 138 | $this->img->Line($this->margin_left + 4, $y, $this->graph_width - $this->margin_right, $y, 'dashed'); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /* Find the largest numeric value in data (an array of arrays representing data) |
---|
| 142 | $largest = 0; |
---|
| 143 | reset($this->data); |
---|
| 144 | while (list($junk, $line) = each($this->data)) |
---|
| 145 | { |
---|
| 146 | reset($line); |
---|
| 147 | while (list($junk2, $value) = each($line)) |
---|
| 148 | { |
---|
| 149 | if ($value > $largest) |
---|
| 150 | { |
---|
| 151 | $largest = $value; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | while ($largest < ($this->num_lines_y - 1)) |
---|
| 157 | { |
---|
| 158 | $largest = ($this->num_lines_y - 1); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | $spread = ceil($largest / ($this->num_lines_y - 1)); |
---|
| 162 | $largest = $spread * ($this->num_lines_y - 1);*/ |
---|
| 163 | |
---|
| 164 | $largest = $this->num_lines_x; |
---|
| 165 | |
---|
| 166 | // Draw the x axis text |
---|
| 167 | $this->img->SetColor(0, 0, 0); |
---|
| 168 | $this->img->SetFont(2); |
---|
| 169 | $linespace = ($this->graph_width - $this->margin_left - $this->margin_right) / ($this->num_lines_x - 1); |
---|
| 170 | reset($this->line_captions_x); |
---|
| 171 | $i = 0; |
---|
| 172 | while (list(,$text) = each($this->line_captions_x)) |
---|
| 173 | { |
---|
| 174 | $this->img->MoveTo($i * $linespace + $this->margin_left, $this->graph_height - $this->margin_bottom + 8); |
---|
| 175 | $this->img->DrawText(array('text' => $text['date_formatted'])); |
---|
[7655] | 176 | ++$i; |
---|
[2] | 177 | } |
---|
| 178 | |
---|
| 179 | // Draw the lines for the data |
---|
| 180 | |
---|
| 181 | $this->img->SetColor(255, 0, 0); |
---|
| 182 | reset($this->data); |
---|
| 183 | |
---|
| 184 | if($this->debug) |
---|
| 185 | { |
---|
| 186 | _debug_array($this->data); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | $i = 1; |
---|
| 190 | while (is_array($this->data) && list(,$line) = each($this->data)) |
---|
| 191 | { |
---|
| 192 | if($line['extracolor']) |
---|
| 193 | { |
---|
| 194 | $this->img->SetColorByName($line['extracolor']); |
---|
| 195 | } |
---|
| 196 | else |
---|
| 197 | { |
---|
| 198 | $this->img->SetColorByName($this->colors[$line['color']]); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | $x1 = $x2 = $y1 = $y2 = 0; |
---|
| 202 | |
---|
| 203 | $linespace = ($this->graph_height - $this->margin_top - $this->margin_bottom) / ($this->num_lines_y - 1); |
---|
| 204 | $y1 = $y2 = $this->graph_height - $this->margin_bottom - ($i * $linespace); |
---|
| 205 | |
---|
| 206 | $linespace = ($this->graph_width - $this->margin_left - $this->margin_right) / ($this->num_lines_x - 1); |
---|
| 207 | |
---|
| 208 | if ($line['sdate'] <= $this->line_captions_x[0]['date'] && $line['edate'] > $this->line_captions_x[0]['date']) |
---|
| 209 | { |
---|
| 210 | if($this->debug) |
---|
| 211 | { |
---|
| 212 | echo 'PRO sdate <= x sdate | PRO edate > x sdate<br>'; |
---|
| 213 | } |
---|
| 214 | $x1 = $this->margin_left; |
---|
| 215 | } |
---|
| 216 | elseif($line['sdate'] >= $this->line_captions_x[0]['date'] && $line['edate'] <= $this->line_captions_x[$largest]['date']) |
---|
| 217 | { |
---|
| 218 | if($this->debug) |
---|
| 219 | { |
---|
| 220 | echo 'PRO sdate >= date! pro_sdate = ' . $line['sdate'] . ', pro_edate = ' . $line['edate'] . '<br>'; |
---|
| 221 | echo 'PRO sdate >= date! pro_sdate_formatted = ' . $GLOBALS['phpgw']->common->show_date($line['sdate'],$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']) . ', pro_edate_formatted = ' . $GLOBALS['phpgw']->common->show_date($line['edate'],$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']) . '<br>'; |
---|
| 222 | echo 'x sdate: ' . $this->line_captions_x[0]['date'] . ', x edate: ' . $this->line_captions_x[$largest]['date'] . '<br><br>'; |
---|
| 223 | } |
---|
| 224 | |
---|
[7655] | 225 | for($y=0;$y<$largest;++$y) |
---|
[2] | 226 | { |
---|
| 227 | if($line['sdate'] == $this->line_captions_x[$y]['date']) |
---|
| 228 | { |
---|
| 229 | $x1 = $y * $linespace + $this->margin_left; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | else |
---|
| 234 | { |
---|
| 235 | $x1 = $largest * $linespace + $this->margin_left; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | if ($line['edate'] >= $this->line_captions_x[$largest]['date']) |
---|
| 239 | { |
---|
| 240 | if($this->debug) |
---|
| 241 | { |
---|
| 242 | echo 'PRO edate >= x edate! pro_edate = ' . $line['edate'] . '<br>'; |
---|
| 243 | echo 'PRO edate >= x edate! pro_edate_formatted = ' . $GLOBALS['phpgw']->common->show_date($line['edate'],$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']) . '<br>'; |
---|
| 244 | echo 'x edate: ' . $this->line_captions_x[$largest]['date'] . '<br>'; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | $x2 = $this->graph_width - $this->margin_right; |
---|
| 248 | } |
---|
| 249 | elseif($line['edate'] <= $this->line_captions_x[$largest]['date'] && $line['edate'] >= $this->line_captions_x[0]['date']) |
---|
| 250 | { |
---|
[7655] | 251 | for($y=0;$y<$largest;++$y) |
---|
[2] | 252 | { |
---|
| 253 | if($line['edate'] == $this->line_captions_x[$y]['date']) |
---|
| 254 | { |
---|
| 255 | $x2 = $y * $linespace + $this->margin_left; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
| 261 | $x2 = $largest * $linespace + $this->margin_left; |
---|
| 262 | } |
---|
| 263 | |
---|
[7655] | 264 | for ($w = -3; $w < 4; ++$w) |
---|
[2] | 265 | { |
---|
| 266 | $this->img->Line(1+$x1,$y1+$w,$x2,$y2+$w); |
---|
| 267 | } |
---|
[7655] | 268 | ++$color_index; |
---|
| 269 | ++$i; |
---|
[2] | 270 | } |
---|
| 271 | // Draw the y axis text |
---|
| 272 | $this->img->SetColor(0, 0, 0); |
---|
| 273 | $this->img->SetFont(2); |
---|
| 274 | $linespace = ($this->graph_height - $this->margin_top - $this->margin_bottom) / ($this->num_lines_y - 1); |
---|
| 275 | $space = 1; |
---|
[7673] | 276 | $data_count = count($this->data); |
---|
| 277 | for ($i = 0;$i<$data_count;++$i) |
---|
[2] | 278 | { |
---|
| 279 | $y = $this->graph_height - $this->margin_bottom - ($space * $linespace) - 7; |
---|
| 280 | $this->img->MoveTo($this->margin_left - 6, $y); |
---|
| 281 | $this->img->DrawText(array('text' => $this->data[$i]['title'],'justification' => 'right','margin_left' => $this->margin_left)); |
---|
[7655] | 282 | ++$space; |
---|
[2] | 283 | } |
---|
| 284 | |
---|
| 285 | $this->img->ToBrowser(); |
---|
| 286 | $this->img->Done(); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | function Open() |
---|
| 290 | { |
---|
| 291 | print('<script language="JavaScript">'); |
---|
| 292 | print('window.open(\'main.php3?menuAction=boGraph.Show&'); |
---|
| 293 | if(strstr($GLOBALS['HTTP_USER_AGENT'],'MSIE')) |
---|
| 294 | { |
---|
| 295 | print('DCLINFO=' . $GLOBALS['DCLINFO'] . '&'); |
---|
| 296 | } |
---|
| 297 | print($this->ToURL() . '\', \'graph\', \'width=' . ($this->graph_width + 20) . ',height=' . ($this->graph_height + 20) . ',resizable=yes,scrollbars=yes\');'); |
---|
| 298 | print('</script>'); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | function Show() |
---|
| 302 | { |
---|
| 303 | $this->FromURL(); |
---|
| 304 | $this->Render(); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | function FromURL() |
---|
| 308 | { |
---|
| 309 | $this->title = $GLOBALS['title']; |
---|
| 310 | $this->caption_x = $GLOBALS['caption_x']; |
---|
| 311 | $this->caption_y = $GLOBALS['caption_y']; |
---|
| 312 | $this->num_lines_x = $GLOBALS['num_lines_x']; |
---|
| 313 | $this->num_lines_y = $GLOBALS['num_lines_y']; |
---|
| 314 | $this->line_captions_x = explode(',', $GLOBALS['line_captions_x']); |
---|
| 315 | |
---|
| 316 | $dataURL = explode('~', $GLOBALS['data']); |
---|
| 317 | $this->data = array(); |
---|
| 318 | while (list($junk, $line) = each($dataURL)) |
---|
| 319 | { |
---|
| 320 | $this->data[] = explode(',', $line); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | $this->colors = explode(',', $GLOBALS['colors']); |
---|
| 324 | $this->color_legend = explode(',', $GLOBALS['color_legend']); |
---|
| 325 | $this->graph_width = $GLOBALS['graph_width']; |
---|
| 326 | $this->graph_height = $GLOBALS['graph_height']; |
---|
| 327 | $this->margin_top = $GLOBALS['margin_top']; |
---|
| 328 | $this->margin_left = $GLOBALS['margin_left']; |
---|
| 329 | $this->margin_bottom = $GLOBALS['margin_bottom']; |
---|
| 330 | $this->margin_right = $GLOBALS['margin_right']; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | function ToURL() |
---|
| 334 | { |
---|
| 335 | $url = 'title=' . rawurlencode($this->title) . '&'; |
---|
| 336 | $url .= 'caption_x=' . rawurlencode($this->caption_x) . '&'; |
---|
| 337 | $url .= 'caption_y=' . rawurlencode($this->caption_y) . '&'; |
---|
| 338 | $url .= 'num_lines_x=' . $this->num_lines_x . '&'; |
---|
| 339 | $url .= 'num_lines_y=' . $this->num_lines_y . '&'; |
---|
| 340 | $url .= 'line_captions_x=' . rawurlencode(implode(',', $this->line_captions_x)) . '&'; |
---|
| 341 | reset($this->data); |
---|
| 342 | $dataURL = ''; |
---|
| 343 | while(list($junk, $line) = each($this->data)) |
---|
| 344 | { |
---|
| 345 | if ($dataURL != '') |
---|
| 346 | { |
---|
| 347 | $dataURL .= '~'; |
---|
| 348 | } |
---|
| 349 | $dataURL .= implode(',', $line); |
---|
| 350 | } |
---|
| 351 | $url .= 'data=' . $dataURL . '&'; |
---|
| 352 | $url .= 'colors=' . implode(',', $this->colors) . '&'; |
---|
| 353 | $url .= 'color_legend=' . rawurlencode(implode(',', $this->color_legend)) . '&'; |
---|
| 354 | $url .= 'graph_width=' . $this->graph_width . '&'; |
---|
| 355 | $url .= 'graph_height=' . $this->graph_height . '&'; |
---|
| 356 | $url .= 'margin_top=' . $this->margin_top . '&'; |
---|
| 357 | $url .= 'margin_left=' . $this->margin_left . '&'; |
---|
| 358 | $url .= 'margin_bottom=' . $this->margin_bottom . '&'; |
---|
| 359 | $url .= 'margin_right=' . $this->margin_right; |
---|
| 360 | |
---|
| 361 | return $url; |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | ?> |
---|