]> projects.mako.cc - selectricity/blob - vendor/plugins/gruff/lib/gruff/stacked_bar.rb
bf28df7af5c9f6ffc787684c1fb4415b4ff771ad
[selectricity] / vendor / plugins / gruff / lib / gruff / stacked_bar.rb
1
2 require File.dirname(__FILE__) + '/base'
3
4 class Gruff::StackedBar < Gruff::Base
5
6     # Draws a bar graph, but multiple sets are stacked on top of each other.
7     def draw
8       get_maximum_by_stack
9       super
10       return unless @has_data
11
12       # Setup spacing.
13       #
14       # Columns sit stacked.
15       spacing_factor = 0.9
16       @bar_width = @graph_width / @column_count.to_f
17     
18       @d = @d.stroke_opacity 0.0
19       
20       height = Array.new(@column_count, 0)
21     
22       @norm_data.each_with_index do |data_row, row_index|
23         @d = @d.fill data_row[DATA_COLOR_INDEX]
24       
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
33           
34           # update the total height of the current stacked bar
35           height[point_index] += (data_point * @graph_height - 2)
36           
37           @d = @d.rectangle(left_x, left_y, right_x, right_y)
38           
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)
42         end
43
44       end
45     
46       @d.draw(@base_image)    
47     end
48
49 end

Benjamin Mako Hill || Want to submit a patch?