Avail feature updated
[ninja.git] / application / vendor / mfchart / MultipleBarChart.php
blob2fbf977db71ecd7b6450f0911e2cbb2ebb98190f
1 <?php
3 class MultipleBarChart extends BarChart {
5 protected $bar_width = 30;
7 protected $bar_gap = -20; // gap between the bars in a group
9 public $bar_colors = false;
11 public function __construct($width=NULL, $height=NULL)
13 parent::__construct($width, $height);
15 $this->type = 'multiple';
17 $this->colors['bar_color'] = array(
18 array('#e0d62e', NULL, NULL),
19 array('#bdb51c', NULL, NULL),
20 array('#9f9917', NULL, NULL),
21 array('#807a13', NULL, NULL),
22 array('#68640f', NULL, NULL),
23 array('#e6de51', NULL, NULL),
24 array('#eee988', NULL, NULL)
26 $this->colors['bar_border_color'] = array(
27 array('#747014', 25, NULL),
28 array('#9d971c', 25, NULL)
32 public function set_bar_gap($val=0)
34 $this->bar_gap = $val;
38 public function add_bar_colors($values=false)
40 if (empty($values))
41 return false;
44 foreach ($values as $val) {
45 #$rgb = utilities::hex2rgb($val);
46 $this->bar_colors[] = imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
49 $this->bar_colors = $values;
52 protected function draw_values($zeroline, $ratio)
54 #echo Kohana::debug($this->bar_colors);
55 #die();
56 $plot_sizes = $this->get_plot_area();
57 $font_bar_legend = $this->font_size < 1 ? $this->font_size : $this->font_size-1;
59 reset($this->values);
60 $total_bars_groups = count($this->values);
61 $bars_per_group = count(current($this->values)); // count of all bars
62 $gap = ($plot_sizes[0] - $total_bars_groups*$bars_per_group*$this->bar_width - $total_bars_groups*($bars_per_group-1)*$this->bar_gap) / ($total_bars_groups+1); // gap between bars -- 5 is gap between group bars
65 $i=0;
66 $a=0;
67 foreach ($this->values AS $key => $value)
69 $x1base = $this->margin_left + $gap + $i*($gap + (count($value)*($this->bar_width+$this->bar_gap)-$this->bar_gap) );
71 $fheight = $this->font_size; // default - recomputed later in the next cycle
73 $j = 0;
74 foreach ($value as $v)
76 $x1 = $x1base + $j*($this->bar_width+$this->bar_gap);
77 $x2 = $x1 + $this->bar_width;
78 $y2 = $this->height - $this->margin_bottom - $zeroline;
79 $y1 = $y2 - $v*$ratio;
81 if ($this->vertical_bar_lines)
82 imageline($this->image, $x1+$this->bar_width/2, $this->margin_top-1, $x1+$this->bar_width/2, $this->height-$this->margin_bottom-1, IMG_COLOR_STYLED);
84 $col = $this->bar_colors[$a];
85 if (!$col) {
86 # sla color
87 $this->bar_legend = false;
88 $rgb = utilities::hex2rgb(reports::$colors['lightblue']);
89 $col = imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
90 } else {
91 $this->bar_legend = true;
92 $rgb = utilities::hex2rgb($col);
93 $col = imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
96 imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $col); // draw bar
97 $this->add_occupied($x1, $y1, $x2, $y2);
99 if ($this->get_color('bar_border_color'))
100 { // draw bar border
101 imageline($this->image, $x1, $y1, $x1, $y2, $this->get_color('bar_border_color', $j)); // left
102 imageline($this->image, $x2, $y1, $x2, $y2, $this->get_color('bar_border_color', $j)); // right
103 imageline($this->image, $x1, $y1, $x2, $y1, $this->get_color('bar_border_color', $j)); // top
106 if ($v != 0)
107 imagefilledrectangle($this->image, $x2, $y1+($v > 0 ? 1 : -1), $x2+1, $y2, $this->get_color('shade_color')); // draw shade of bar
109 $box_points = imagettfbbox($this->font_size, 0, $this->font, $v);
110 $bar_middle = $x1 + $this->bar_width/2;
111 $bar_legend_hwidth = ($box_points[4]-$box_points[6]) / 2;
112 $fheight = $box_points[3]-$box_points[5];
114 // bar legend
115 if ($this->bar_legend)
117 if ($v >= 0)
119 #utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, $y1-$fheight-5, $bar_middle+$bar_legend_hwidth, $y1, ALIGN_CENTER, VALIGN_MIDDLE, 0, Reports_Controller::_format_report_value($v), $this->get_color('font_color3'));
121 #utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, 65, $bar_middle+$bar_legend_hwidth, 995, ALIGN_CENTER, VALIGN_MIDDLE, 0, Reports_Controller::_format_report_value($v), $this->get_color('font_color3'));
122 #utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, -150, $bar_middle+$bar_legend_hwidth, 700, ALIGN_LEFT, VALIGN_MIDDLE, 0, Reports_Controller::_format_report_value($v), $this->get_color('font_color3'));
123 utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, $y1-$fheight-5, $bar_middle+$bar_legend_hwidth, $y1, ALIGN_LEFT, VALIGN_MIDDLE, 0, reports::format_report_value($v), $this->get_color('font_color3'));
124 #$this->add_occupied($bar_middle-$bar_legend_hwidth, $y1-$fheight-5, $bar_middle+$bar_legend_hwidth, $y1);
125 $this->add_occupied($bar_middle-$bar_legend_hwidth, $y1-$fheight-5, $bar_middle+$bar_legend_hwidth, $y1);
127 else
129 utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, $y1+5, $bar_middle+$bar_legend_hwidth, $y1+$fheight+5, ALIGN_CENTER, VALIGN_MIDDLE, 0, $v, $this->get_color('font_color3'));
130 $this->add_occupied($bar_middle-$bar_legend_hwidth, $y1+5, $bar_middle+$bar_legend_hwidth, $y1+$fheight+5);
134 $j++;
135 $a++;
138 $box_points = imagettfbbox($this->font_size, 0, $this->font, $key);
139 $y_legend_hwidth = ($box_points[4]-$box_points[6]) / 2;
140 $group_bar_middle = $x1base + (count($value)*($this->bar_width+$this->bar_gap)-$this->bar_gap)/2;
142 // x axis legend
143 utilities::imagestringbox($this->image, $this->font, $this->font_size, $group_bar_middle-$y_legend_hwidth, $this->margin_top+$plot_sizes[1]+5, $group_bar_middle+$y_legend_hwidth, $this->margin_top+$plot_sizes[1]+5+$fheight, ALIGN_CENTER, VALIGN_MIDDLE, 0, $key, $this->get_color('font_color'));
145 $i++;
148 #******************************************************************
149 # Draw SLA graph legend to explain bar colors
150 #******************************************************************
152 # colors
153 $rgb = utilities::hex2rgb(reports::$colors['lightblue']);
154 $color = imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
156 $rgb_red = utilities::hex2rgb(reports::$colors['red']);
157 $red_color = imagecolorallocate($this->image, $rgb_red[0], $rgb_red[1], $rgb_red[2]);
159 $rgb_green = utilities::hex2rgb(reports::$colors['green']);
160 $green_color = imagecolorallocate($this->image, $rgb_green[0], $rgb_green[1], $rgb_green[2]);
162 # black col
163 $rgb = utilities::hex2rgb('#000000');
164 $col = imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
166 # legend boxes - 2 for each legend with one black box being 1 pixel larger to create a border
167 imagefilledrectangle($this->image, 34, $this->height-30, 55, $this->height-45, $col); // draw sla compliance border
168 imagefilledrectangle($this->image, 35, $this->height-31, 54, $this->height-44, $color); // draw sla compliance color legend
170 imagefilledrectangle($this->image, 34, $this->height-10, 55, $this->height-25, $col); // draw sla breach border
171 imagefilledrectangle($this->image, 35, $this->height-11, 54, $this->height-24, $red_color); // draw sla breach color legend
173 imagefilledrectangle($this->image, 184, $this->height-10, 205, $this->height-25, $col); // draw fulfilled sla border
174 imagefilledrectangle($this->image, 185, $this->height-11, 204, $this->height-24, $green_color); // draw fulfilled sla color legend
176 # text color
177 $rgb = utilities::hex2rgb('#000000');
178 $col = imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]);
180 # legend strings:
182 # sla compliance string
183 utilities::imagestringbox($this->image, $this->font, $this->font_size, 255, $this->height-70, -50, $this->height, ALIGN_CENTER, VALIGN_MIDDLE, 0, _('SLA Compliance'), $col);
185 # breached sla string
186 utilities::imagestringbox($this->image, $this->font, $this->font_size, 255, $this->height-32, -60, $this->height, ALIGN_CENTER, VALIGN_MIDDLE, 0, _('Breached SLA'), $col);
188 # Fulfilled sla string
189 utilities::imagestringbox($this->image, $this->font, $this->font_size, 545, $this->height-32, -60, $this->height, ALIGN_CENTER, VALIGN_MIDDLE, 0, _('Fulfilled SLA'), $col);