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

Benjamin Mako Hill || Want to submit a patch?