| Class | Gruff::StackedBar |
| In: |
lib/gruff/stacked_bar.rb
|
| Parent: | Gruff::Base |
Draws a bar graph, but multiple sets are stacked on top of each other.
# File lib/gruff/stacked_bar.rb, line 9
9: def draw
10: get_maximum_by_stack
11: super
12: return unless @has_data
13:
14: # Setup spacing.
15: #
16: # Columns sit stacked.
17: @bar_spacing ||= 0.9
18: @bar_width = @graph_width / @column_count.to_f
19: padding = (@bar_width * (1 - @bar_spacing)) / 2
20:
21: @d = @d.stroke_opacity 0.0
22:
23: height = Array.new(@column_count, 0)
24:
25: @norm_data.each_with_index do |data_row, row_index|
26: data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
27: @d = @d.fill data_row[DATA_COLOR_INDEX]
28:
29: # Calculate center based on bar_width and current row
30: label_center = @graph_left + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
31: draw_label(label_center, point_index)
32:
33: next if (data_point == 0)
34: # Use incremented x and scaled y
35: left_x = @graph_left + (@bar_width * point_index) + padding
36: left_y = @graph_top + (@graph_height -
37: data_point * @graph_height -
38: height[point_index]) + 1
39: right_x = left_x + @bar_width * @bar_spacing
40: right_y = @graph_top + @graph_height - height[point_index] - 1
41:
42: # update the total height of the current stacked bar
43: height[point_index] += (data_point * @graph_height )
44:
45: @d = @d.rectangle(left_x, left_y, right_x, right_y)
46:
47: end
48:
49: end
50:
51: @d.draw(@base_image)
52: end