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

Benjamin Mako Hill || Want to submit a patch?