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: spacing_factor = 0.9 18: @bar_width = @graph_width / @column_count.to_f 19: 20: @d = @d.stroke_opacity 0.0 21: 22: height = Array.new(@column_count, 0) 23: 24: @norm_data.each_with_index do |data_row, row_index| 25: data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| 26: @d = @d.fill data_row[DATA_COLOR_INDEX] 27: 28: # Calculate center based on bar_width and current row 29: label_center = @graph_left + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0) 30: draw_label(label_center, point_index) 31: 32: next if (data_point == 0) 33: # Use incremented x and scaled y 34: left_x = @graph_left + (@bar_width * point_index) 35: left_y = @graph_top + (@graph_height - 36: data_point * @graph_height - 37: height[point_index]) + 1 38: right_x = left_x + @bar_width * spacing_factor 39: right_y = @graph_top + @graph_height - height[point_index] - 1 40: 41: # update the total height of the current stacked bar 42: height[point_index] += (data_point * @graph_height ) 43: 44: @d = @d.rectangle(left_x, left_y, right_x, right_y) 45: 46: end 47: 48: end 49: 50: @d.draw(@base_image) 51: end