1 require File.dirname(__FILE__) + '/base'
6 # Graph with horizontal bars instead of vertical.
8 # TODO SideStackedBar should probably inherit from this
9 # to consolidate the line marker drawing.
10 class Gruff::SideBar < Gruff::Base
12 # Instead of base class version, draws vertical background lines and label
15 return if @hide_line_markers
17 @d = @d.stroke_antialias false
19 # Draw horizontal line markers and annotate with numbers
20 @d = @d.stroke(@marker_color)
21 @d = @d.stroke_width 1
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|
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
34 unless @hide_line_numbers
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,
42 x - (@marker_font_size/1.5), @graph_bottom + 40,
43 marker_label.to_s, @scale)
45 @d = @d.stroke_antialias true
50 # Draw on the Y axis instead of the X
52 def draw_label(y_offset, index)
53 if !@labels[index].nil? && @labels_seen[index].nil?
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,
62 -@graph_left + LABEL_MARGIN * 2.0, y_offset,
63 @labels[index], @scale)
64 @labels_seen[index] = 1
69 @has_left_labels = true
72 return unless @has_data
76 # Columns sit stacked.
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)
84 @norm_data.each_with_index do |data_row, row_index|
85 @d = @d.fill data_row[DATA_COLOR_INDEX]
87 data_row[1].each_with_index do |data_point, point_index|
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
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)
105 @d = @d.rectangle(left_x, left_y, right_x, right_y)
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)