Class | Gruff::SideStackedBar |
In: |
lib/gruff/side_stacked_bar.rb
|
Parent: | Gruff::SideBar |
New gruff graph type added to enable sideways stacking bar charts (basically looks like a x/y flip of a standard stacking bar chart)
alun.eyre@googlemail.com
# File lib/gruff/side_stacked_bar.rb, line 14 14: def draw 15: @has_left_labels = true 16: get_maximum_by_stack 17: super 18: 19: return unless @has_data 20: 21: # Setup spacing. 22: # 23: # Columns sit stacked. 24: spacing_factor = 0.9 25: 26: @bar_width = @graph_height / @column_count.to_f 27: @d = @d.stroke_opacity 0.0 28: height = Array.new(@column_count, 0) 29: length = Array.new(@column_count, @graph_left) 30: 31: @norm_data.each_with_index do |data_row, row_index| 32: @d = @d.fill data_row[DATA_COLOR_INDEX] 33: 34: data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| 35: 36: ## using the original calcs from the stacked bar chart to get the difference between 37: ## part of the bart chart we wish to stack. 38: temp1 = @graph_left + (@graph_width - 39: data_point * @graph_width - 40: height[point_index]) + 1 41: temp2 = @graph_left + @graph_width - height[point_index] - 1 42: difference = temp2 - temp1 43: 44: left_x = length[point_index] #+ 1 45: left_y = @graph_top + (@bar_width * point_index) 46: right_x = left_x + difference 47: right_y = left_y + @bar_width * spacing_factor 48: length[point_index] += difference 49: height[point_index] += (data_point * @graph_width - 2) 50: 51: @d = @d.rectangle(left_x, left_y, right_x, right_y) 52: 53: # Calculate center based on bar_width and current row 54: label_center = @graph_top + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0) 55: draw_label(label_center, point_index) 56: end 57: 58: end 59: 60: @d.draw(@base_image) 61: end
# File lib/gruff/side_stacked_bar.rb, line 65 65: def larger_than_max?(data_point, index=0) 66: max(data_point, index) > @maximum_value 67: end