histogram: Make histograms crash less
[ninja.git] / application / vendor / mfchart / StackedBarChart.php
blob0b7756016ac9a8f94e0fba328ae26effc22c581b
1 <?php
3 class StackedBarChart extends BarChart {
5 public function __construct($width=NULL, $height=NULL)
7 parent::__construct($width, $height);
9 $this->type = 'stacked';
11 $this->colors['bar_color'] = array(
12 array('#e0d62e', NULL, NULL),
13 array('#bdb51c', NULL, NULL),
14 array('#9f9917', NULL, NULL),
15 array('#807a13', NULL, NULL),
16 array('#68640f', NULL, NULL),
17 array('#e6de51', NULL, NULL),
18 array('#eee988', NULL, NULL)
20 $this->colors['bar_border_color'] = array(
21 array('#747014', 25, NULL),
22 array('#9d971c', 25, NULL)
27 protected function draw_values($zeroline, $ratio)
29 $plot_sizes = $this->get_plot_area();
30 $font_bar_legend = $this->font_size < 1 ? $this->font_size : $this->font_size-1;
32 $total_bars = count($this->values); // count of all bars
33 $gap = ($plot_sizes[0] - $total_bars*$this->bar_width ) / ($total_bars+1); // gap between bars
36 $i=0;
37 foreach ($this->values AS $key => $value)
39 $x1 = $this->margin_left + $gap + $i*($gap+$this->bar_width);
40 $x2 = $x1 + $this->bar_width;
41 $y2 = $this->height - $this->margin_bottom - $zeroline;
42 $y1 = array();
43 foreach ($value as $v)
44 $y1[] = $y2 - $v*$ratio;
46 if ($this->vertical_bar_lines)
47 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);
49 $sum = array_sum($value); // sum of all values in a stack bar
50 $bar_border = $y2 - $sum*$ratio; // border of the sum in pixels from the top of the graph
52 $j = 0;
53 $base = 0;
54 foreach ($y1 as $v)
56 $y1i = $v-$base;
57 $y2i = $y2-$base;
59 $to_draw = array($y1i, $y2i);
60 $borders = array($bar_border, $y2);
61 sort($to_draw);
62 sort($borders);
63 if ($to_draw[0] > $borders[1] OR $to_draw[1] < $borders[0])
64 continue;
65 if ($to_draw[1] > $borders[1])
66 $to_draw[1] = $borders[1];
67 if ($to_draw[0] < $borders[0])
68 $to_draw[0] = $borders[0];
70 imagefilledrectangle($this->image, $x1, $to_draw[0], $x2, $to_draw[1], $this->get_color('bar_color', $j)); // draw bar
72 if ($this->get_color('bar_border_color'))
73 { // draw bar border
74 imageline($this->image, $x1, $to_draw[0], $x1, $to_draw[1], $this->get_color('bar_border_color', $j)); // left
75 if ($to_draw[1] > $y2)
76 imageline($this->image, $x1, $to_draw[1], $x2, $to_draw[1], $this->get_color('bar_border_color', $j)); // bottom
77 else
78 imageline($this->image, $x1, $to_draw[0], $x2, $to_draw[0], $this->get_color('bar_border_color', $j)); // top
79 imageline($this->image, $x2, $to_draw[0], $x2, $to_draw[1], $this->get_color('bar_border_color', $j)); // right
82 $base += $y2-$v + 1;
83 $j++;
86 if ($sum != 0)
87 imagefilledrectangle($this->image, $x2, $bar_border+($sum > 0 ? 1 : -1), $x2+1, $y2, $this->get_color('shade_color')); // draw shade of bar
89 $this->add_occupied($x1, $bar_border, $x2, $y2);
91 $box_points = imagettfbbox($this->font_size, 0, $this->font, $sum);
92 $bar_middle = $x1 + $this->bar_width/2;
93 $bar_legend_hwidth = ($box_points[4]-$box_points[6]) / 2;
94 $fheight = $box_points[3]-$box_points[5];
96 // bar legend
97 if ($this->bar_legend)
99 if ($sum >= 0)
101 utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, $bar_border-$fheight-5, $bar_middle+$bar_legend_hwidth, $bar_border, ALIGN_CENTER, VALIGN_MIDDLE, 0, $sum, $this->get_color('font_color3'));
102 $this->add_occupied($bar_middle-$bar_legend_hwidth, $bar_border-$fheight-5, $bar_middle+$bar_legend_hwidth, $bar_border);
104 else
106 utilities::imagestringbox($this->image, $this->font, $font_bar_legend, $bar_middle-$bar_legend_hwidth, $bar_border+5, $bar_middle+$bar_legend_hwidth, $bar_border+$fheight+5, ALIGN_CENTER, VALIGN_MIDDLE, 0, $sum, $this->get_color('font_color3'));
107 $this->add_occupied($bar_middle-$bar_legend_hwidth, $bar_border+5, $bar_middle+$bar_legend_hwidth, $bar_border+$fheight+5);
112 $box_points = imagettfbbox($this->font_size, 0, $this->font, $key);
113 $y_legend_hwidth = ($box_points[4]-$box_points[6]) / 2;
115 // x axis legend
116 utilities::imagestringbox($this->image, $this->font, $this->font_size, $bar_middle-$y_legend_hwidth, $this->margin_top+$plot_sizes[1]+5, $bar_middle+$y_legend_hwidth, $this->margin_top+$plot_sizes[1]+5+$fheight, ALIGN_CENTER, VALIGN_MIDDLE, 0, $key, $this->get_color('font_color'));
118 $i++;