2 # New gruff graph type added to enable sideways stacking bar charts (basically looks like a x/y
3 # flip of a standard stacking bar chart)
5 # alun.eyre@googlemail.com
7 require File.dirname(__FILE__) + '/base'
9 class Gruff::SideStackedBar < Gruff::Base
11 # instead of base class version, draws vertical background lines and label
14 return if @hide_line_markers
16 # Draw horizontal line markers and annotate with numbers
17 @d = @d.stroke(@marker_color)
18 @d = @d.stroke_width 1
21 # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
22 increment = significant(@maximum_value.to_f / number_of_lines)
23 (0..number_of_lines).each do |index|
25 line_diff = (@graph_right - @graph_left) / number_of_lines
26 x = @graph_right - (line_diff * index) - 1
27 @d = @d.line(x, @graph_bottom, x, @graph_top)
29 diff = index - number_of_lines
30 marker_label = diff.abs * increment
32 @d.fill = @marker_color
33 @d.font = @font if @font
34 @d.stroke = 'transparent'
35 @d.pointsize = scale_fontsize(@marker_font_size)
36 # @d.gravity = NorthGravity
37 @d = @d.annotate_scaled( @base_image,
39 x - (@marker_font_size/1.5), @graph_bottom + 40,
40 marker_label.to_s, @scale)
45 # instead of base class version, modified to enable us to draw on the Y axis instead of X
46 def draw_label(y_offset, index)
47 if !@labels[index].nil? && @labels_seen[index].nil?
48 @d.fill = @marker_color
49 @d.font = @font if @font
50 @d.stroke = 'transparent'
51 @d.font_weight = NormalWeight
52 @d.pointsize = scale_fontsize(@marker_font_size)
53 @d.gravity = CenterGravity
54 @d = @d.annotate_scaled(@base_image,
56 @graph_left / 2, y_offset,
57 @labels[index], @scale)
58 @labels_seen[index] = 1
63 @has_left_labels = true
67 return unless @has_data
71 # Columns sit stacked.
74 @bar_width = @graph_height / @column_count.to_f
75 @d = @d.stroke_opacity 0.0
76 height = Array.new(@column_count, 0)
77 length = Array.new(@column_count, @graph_left)
79 @norm_data.each_with_index do |data_row, row_index|
80 @d = @d.fill data_row[DATA_COLOR_INDEX]
82 data_row[1].each_with_index do |data_point, point_index|
84 ## using the original calcs from the stacked bar chart to get the difference between
85 ## part of the bart chart we wish to stack.
86 temp1 = @graph_left + (@graph_width -
87 data_point * @graph_width -
88 height[point_index]) + 1
89 temp2 = @graph_left + @graph_width - height[point_index] - 1
90 difference = temp2 - temp1
92 left_x = length[point_index] #+ 1
93 left_y = @graph_top + (@bar_width * point_index)
94 right_x = left_x + difference
95 right_y = left_y + @bar_width * spacing_factor
96 length[point_index] += difference
97 height[point_index] += (data_point * @graph_width - 2)
99 @d = @d.rectangle(left_x, left_y, right_x, right_y)
101 # Calculate center based on bar_width and current row
102 label_center = @graph_top + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0)
103 draw_label(label_center, point_index)
113 def larger_than_max?(data_point, index=0)
114 max(data_point, index) > @maximum_value
117 def max(data_point, index)
118 @data.inject(0) {|sum, item| sum + item[1][index]}