Added the Gruff library to the lib/ directory of the the rails folder, and the
[selectricity] / lib / gruff-0.2.8 / lib / gruff / side_stacked_bar.rb
1 ##
2 # New gruff graph type added to enable sideways stacking bar charts (basically looks like a x/y
3 # flip of a standard stacking bar chart)
4 #
5 # alun.eyre@googlemail.com 
6 #
7 require File.dirname(__FILE__) + '/base'
8
9 class Gruff::SideStackedBar < Gruff::Base
10
11     # instead of base class version, draws vertical background lines and label
12     def draw_line_markers
13
14       return if @hide_line_markers
15
16       # Draw horizontal line markers and annotate with numbers
17       @d = @d.stroke(@marker_color)
18       @d = @d.stroke_width 1
19       number_of_lines = 5
20
21       # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
22       increment = significant(@maximum_value.to_f / number_of_lines)
23       (0..number_of_lines).each do |index|
24
25         line_diff = (@graph_right - @graph_left) / number_of_lines
26         x = @graph_right - (line_diff * index) - 1
27         @d = @d.line(x, @graph_bottom, x, @graph_top)
28
29         diff = index - number_of_lines
30         marker_label = diff.abs * increment
31
32         @d.fill = @marker_color
33         @d.font = @font if @font
34         @d.stroke = 'transparent'
35         @d.pointsize = scale_fontsize(@marker_font_size)
36 #        @d.gravity = NorthGravity
37         @d = @d.annotate_scaled( @base_image, 
38                           100, 20,
39                           x - (@marker_font_size/1.5), @graph_bottom + 40, 
40                           marker_label.to_s, @scale)
41
42       end
43     end
44
45     # instead of base class version, modified to enable us to draw on the Y axis instead of X
46     def draw_label(y_offset, index)
47       if !@labels[index].nil? && @labels_seen[index].nil?
48         @d.fill = @marker_color
49         @d.font = @font if @font
50         @d.stroke = 'transparent'
51         @d.font_weight = NormalWeight
52         @d.pointsize = scale_fontsize(@marker_font_size)
53         @d.gravity = CenterGravity
54         @d = @d.annotate_scaled(@base_image,
55                                 1, 1,
56                                 @graph_left / 2, y_offset,
57                                 @labels[index], @scale)
58         @labels_seen[index] = 1
59       end
60     end
61
62     def draw
63       @has_left_labels = true
64       get_maximum_by_stack
65       super
66
67       return unless @has_data
68
69       # Setup spacing.
70       #
71       # Columns sit stacked.
72       spacing_factor = 0.9
73
74       @bar_width = @graph_height / @column_count.to_f
75       @d = @d.stroke_opacity 0.0
76       height = Array.new(@column_count, 0)
77       length = Array.new(@column_count, @graph_left)
78
79       @norm_data.each_with_index do |data_row, row_index|
80         @d = @d.fill data_row[DATA_COLOR_INDEX]
81
82         data_row[1].each_with_index do |data_point, point_index|
83
84           ## using the original calcs from the stacked bar chart to get the difference between
85           ## part of the bart chart we wish to stack.
86           temp1 = @graph_left + (@graph_width -
87                                       data_point * @graph_width - 
88                                       height[point_index]) + 1
89           temp2 = @graph_left + @graph_width - height[point_index] - 1
90           difference = temp2 - temp1
91
92           left_x = length[point_index] #+ 1
93                 left_y = @graph_top + (@bar_width * point_index)
94           right_x = left_x + difference
95                 right_y = left_y + @bar_width * spacing_factor
96           length[point_index] += difference
97           height[point_index] += (data_point * @graph_width - 2)
98
99           @d = @d.rectangle(left_x, left_y, right_x, right_y)
100
101           # Calculate center based on bar_width and current row
102           label_center = @graph_top + (@bar_width * point_index) + (@bar_width * spacing_factor / 2.0)
103           draw_label(label_center, point_index)
104         end
105
106       end
107
108       @d.draw(@base_image)    
109     end
110
111     protected
112
113     def larger_than_max?(data_point, index=0)
114       max(data_point, index) > @maximum_value
115     end
116
117     def max(data_point, index)
118       @data.inject(0) {|sum, item| sum + item[1][index]}
119     end
120
121 end

Benjamin Mako Hill || Want to submit a patch?