fix security issue
[selectricity] / lib / gruff-0.2.8 / lib / gruff / bar.rb
1
2 require File.dirname(__FILE__) + '/base'
3 require File.dirname(__FILE__) + '/bar_conversion'
4
5 class Gruff::Bar < Gruff::Base
6   
7   def draw
8     # Labels will be centered over the left of the bar if
9     # there are more labels than columns. This is basically the same 
10     # as where it would be for a line graph.
11     @center_labels_over_point = (@labels.keys.length > @column_count ? true : false)
12     
13     super
14     return unless @has_data
15
16     draw_bars
17   end
18
19 protected
20
21   def draw_bars
22     # Setup spacing.
23     #
24     # Columns sit side-by-side.
25     spacing_factor = 0.9 # space between the bars
26     @bar_width = @graph_width / (@column_count * @data.length).to_f
27     
28     @d = @d.stroke_opacity 0.0
29
30     # Setup the BarConversion Object
31     conversion = Gruff::BarConversion.new()
32     conversion.graph_height = @graph_height
33     conversion.graph_top = @graph_top
34
35     # Set up the right mode [1,2,3] see BarConversion for further explanation
36     if @minimum_value >= 0 then
37       # all bars go from zero to positiv
38       conversion.mode = 1
39     else
40       # all bars go from 0 to negativ
41       if @maximum_value <= 0 then
42         conversion.mode = 2
43       else
44         # bars either go from zero to negativ or to positiv
45         conversion.mode = 3
46         conversion.spread = @spread
47         conversion.minimum_value = @minimum_value
48         conversion.zero = -@minimum_value/@spread
49       end
50     end
51
52     # iterate over all normalised data
53     @norm_data.each_with_index do |data_row, row_index|
54       count = 0
55       data_row[1].each_with_index do |data_point, point_index|
56       
57         # Use incremented x and scaled y
58         # x
59         left_x = @graph_left + (@bar_width * (row_index + point_index + ((@data.length - 1) * point_index)))
60         right_x = left_x + @bar_width * spacing_factor
61         # y
62         conv = []
63         conversion.getLeftYRightYscaled( data_point, conv )
64
65         # create new bar
66         @d = @d.fill data_row[DATA_COLOR_INDEX]
67         @d = @d.rectangle(left_x, conv[0], right_x, conv[1])
68         
69         # Calculate center based on bar_width and current row
70         label_center = @graph_left + 
71                       (@data.length * @bar_width * point_index) + 
72                       (@data.length * @bar_width / 2.0)
73         # Subtract half a bar width to center left if requested
74         draw_label(label_center - (@center_labels_over_point ? @bar_width / 2.0 : 0.0), point_index, count)
75         # custom modification to allow graph labels to alternate in height on
76         # bar graphs so they don't overlap
77         count += 1
78       end
79
80     end
81
82     # Draw the last label if requested
83     draw_label(@graph_right, @column_count) if @center_labels_over_point
84
85     @d.draw(@base_image)
86   end
87
88   def draw_axis_labels
89     unless @x_axis_label.nil?
90       # X Axis
91       # Centered vertically and horizontally by setting the
92       # height to 1.0 and the width to the width of the graph.
93       x_axis_label_y_coordinate = @graph_bottom + LABEL_MARGIN * 2.5 + @marker_caps_height
94
95       # TODO Center between graph area
96       @d.fill = @font_color
97       @d.font = @font if @font
98       @d.stroke('transparent')
99       @d.pointsize = scale_fontsize(@marker_font_size) * 1.2
100       @d.gravity = NorthGravity
101       @d = @d.annotate_scaled( @base_image, 
102                         @raw_columns, 1.0, 
103                         0.0, x_axis_label_y_coordinate, 
104                         @x_axis_label, @scale)
105       debug { @d.line 0.0, x_axis_label_y_coordinate, @raw_columns, x_axis_label_y_coordinate }
106     end
107
108     unless @y_axis_label.nil?
109       # Y Axis, rotated vertically
110       @d.rotation = 90.0
111       @d.gravity = CenterGravity
112       @d = @d.annotate_scaled( @base_image, 
113                         1.0, @raw_rows,
114                         LEFT_MARGIN + @marker_caps_height / 2.0, 0.0, 
115                         @y_axis_label, @scale)
116       @d.rotation = -90.0
117     end
118   end
119
120 end

Benjamin Mako Hill || Want to submit a patch?