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
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;
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
59 $to_draw = array($y1i, $y2i);
60 $borders = array($bar_border, $y2);
63 if ($to_draw[0] > $borders[1] OR $to_draw[1] < $borders[0])
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'))
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
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
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];
97 if ($this->bar_legend
)
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);
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;
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'));