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