1 require File.dirname(__FILE__) + '/base'
4 # Graph with individual horizontal bars instead of vertical bars.
6 class Gruff::SideBar < Gruff::Base
9 @has_left_labels = true
12 return unless @has_data
16 # Columns sit stacked.
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)
24 @norm_data.each_with_index do |data_row, row_index|
25 @d = @d.fill data_row[DATA_COLOR_INDEX]
27 data_row[1].each_with_index do |data_point, point_index|
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
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
43 length[point_index] += difference
44 height[point_index] += (data_point * @graph_width - 2)
46 @d = @d.rectangle(left_x, left_y, right_x, right_y)
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)
60 # Instead of base class version, draws vertical background lines and label
63 return if @hide_line_markers
65 @d = @d.stroke_antialias false
67 # Draw horizontal line markers and annotate with numbers
68 @d = @d.stroke(@marker_color)
69 @d = @d.stroke_width 1
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|
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
82 unless @hide_line_numbers
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)
94 @d = @d.stroke_antialias true
99 # Draw on the Y axis instead of the X
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,
111 -@graph_left + LABEL_MARGIN * 2.0, y_offset,
112 @labels[index], @scale)
113 @labels_seen[index] = 1