2 require File.dirname(__FILE__) + '/base'
4 class Gruff::StackedBar < Gruff::Base
6 # Draws a bar graph, but multiple sets are stacked on top of each other.
10 return unless @has_data
14 # Columns sit stacked.
16 @bar_width = @graph_width / @column_count.to_f
18 @d = @d.stroke_opacity 0.0
20 height = Array.new(@column_count, 0)
22 @norm_data.each_with_index do |data_row, row_index|
23 @d = @d.fill data_row[DATA_COLOR_INDEX]
25 data_row[1].each_with_index do |data_point, point_index|
26 # Use incremented x and scaled y
27 left_x = @graph_left + (@bar_width * point_index)
28 left_y = @graph_top + (@graph_height -
29 data_point * @graph_height -
30 height[point_index]) + 1
31 right_x = left_x + @bar_width * spacing_factor
32 right_y = @graph_top + @graph_height - height[point_index] - 1
34 # update the total height of the current stacked bar
35 height[point_index] += (data_point * @graph_height - 2)
37 @d = @d.rectangle(left_x, left_y, right_x, right_y)
39 # Calculate center based on bar_width and current row
40 label_center = @graph_left + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0)
41 draw_label(label_center, point_index)