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: spacing_factor = 0.9 17: 18: @bars_width = @graph_height / @column_count.to_f 19: @bar_width = @bars_width * spacing_factor / @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: 24: @norm_data.each_with_index do |data_row, row_index| 25: @d = @d.fill data_row[DATA_COLOR_INDEX] 26: 27: data_row[1].each_with_index do |data_point, point_index| 28: 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 - data_point * @graph_width - height[point_index]) 33: temp2 = @graph_left + @graph_width - height[point_index] 34: difference = temp2 - temp1 35: 36: left_x = length[point_index] - 1 37: left_y = @graph_top + (@bars_width * point_index) + (@bar_width * row_index) 38: right_x = left_x + difference 39: right_y = left_y + @bar_width 40: 41: height[point_index] += (data_point * @graph_width) 42: 43: @d = @d.rectangle(left_x, left_y, right_x, right_y) 44: 45: # Calculate center based on bar_width and current row 46: label_center = @graph_top + (@bars_width * point_index + @bars_width / 2) 47: draw_label(label_center, point_index) 48: end 49: 50: end 51: 52: @d.draw(@base_image) 53: end
Draw on the Y axis instead of the X
# File lib/gruff/side_bar.rb, line 98 98: def draw_label(y_offset, index) 99: if !@labels[index].nil? && @labels_seen[index].nil? 100: @d.fill = @font_color 101: @d.font = @font if @font 102: @d.stroke = 'transparent' 103: @d.font_weight = NormalWeight 104: @d.pointsize = scale_fontsize(@marker_font_size) 105: @d.gravity = EastGravity 106: @d = @d.annotate_scaled(@base_image, 107: 1, 1, 108: -@graph_left + LABEL_MARGIN * 2.0, y_offset, 109: @labels[index], @scale) 110: @labels_seen[index] = 1 111: end 112: end
Instead of base class version, draws vertical background lines and label
# File lib/gruff/side_bar.rb, line 58 58: def draw_line_markers 59: 60: return if @hide_line_markers 61: 62: @d = @d.stroke_antialias false 63: 64: # Draw horizontal line markers and annotate with numbers 65: @d = @d.stroke(@marker_color) 66: @d = @d.stroke_width 1 67: number_of_lines = 5 68: 69: # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc. 70: increment = significant(@maximum_value.to_f / number_of_lines) 71: (0..number_of_lines).each do |index| 72: 73: line_diff = (@graph_right - @graph_left) / number_of_lines 74: x = @graph_right - (line_diff * index) - 1 75: @d = @d.line(x, @graph_bottom, x, @graph_top) 76: diff = index - number_of_lines 77: marker_label = diff.abs * increment 78: 79: unless @hide_line_numbers 80: @d.fill = @font_color 81: @d.font = @font if @font 82: @d.stroke = 'transparent' 83: @d.pointsize = scale_fontsize(@marker_font_size) 84: @d.gravity = CenterGravity 85: # TODO Center text over line 86: @d = @d.annotate_scaled( @base_image, 87: 0, 0, # Width of box to draw text in 88: x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text 89: marker_label.to_s, @scale) 90: end # unless 91: @d = @d.stroke_antialias true 92: end 93: end