Class | Gruff::Dot |
In: |
lib/gruff/dot.rb
|
Parent: | Gruff::Base |
Graph with dots and labels along a vertical access see: ‘Creating More Effective Graphs’ by Robbins
# File lib/gruff/dot.rb, line 9 9: def draw 10: @has_left_labels = true 11: super 12: 13: return unless @has_data 14: 15: # Setup spacing. 16: # 17: spacing_factor = 1.0 18: 19: @items_width = @graph_height / @column_count.to_f 20: @item_width = @items_width * spacing_factor / @norm_data.size 21: @d = @d.stroke_opacity 0.0 22: height = Array.new(@column_count, 0) 23: length = Array.new(@column_count, @graph_left) 24: padding = (@items_width * (1 - spacing_factor)) / 2 25: 26: @norm_data.each_with_index do |data_row, row_index| 27: data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index| 28: 29: x_pos = @graph_left + (data_point * @graph_width) - (@item_width.to_f/6.0).round 30: y_pos = @graph_top + (@items_width * point_index) + padding + (@item_width.to_f/2.0).round 31: 32: if row_index == 0 33: @d = @d.stroke(@marker_color) 34: @d = @d.stroke_width 1.0 35: @d = @d.opacity 0.1 36: @d = @d.line(@graph_left, y_pos, @graph_left + @graph_width, y_pos) 37: end 38: 39: @d = @d.fill data_row[DATA_COLOR_INDEX] 40: @d = @d.stroke('transparent') 41: @d = @d.circle(x_pos, y_pos, x_pos + (@item_width.to_f/3.0).round, y_pos) 42: 43: # Calculate center based on item_width and current row 44: label_center = @graph_top + (@items_width * point_index + @items_width / 2) + padding 45: draw_label(label_center, point_index) 46: end 47: 48: end 49: 50: @d.draw(@base_image) 51: end
Draw on the Y axis instead of the X
# File lib/gruff/dot.rb, line 96 96: def draw_label(y_offset, index) 97: if !@labels[index].nil? && @labels_seen[index].nil? 98: @d.fill = @font_color 99: @d.font = @font if @font 100: @d.stroke = 'transparent' 101: @d.font_weight = NormalWeight 102: @d.pointsize = scale_fontsize(@marker_font_size) 103: @d.gravity = EastGravity 104: @d = @d.annotate_scaled(@base_image, 105: 1, 1, 106: -@graph_left + LABEL_MARGIN * 2.0, y_offset, 107: @labels[index], @scale) 108: @labels_seen[index] = 1 109: end 110: end
Instead of base class version, draws vertical background lines and label
# File lib/gruff/dot.rb, line 56 56: def draw_line_markers 57: 58: return if @hide_line_markers 59: 60: @d = @d.stroke_antialias false 61: 62: # Draw horizontal line markers and annotate with numbers 63: @d = @d.stroke(@marker_color) 64: @d = @d.stroke_width 1 65: number_of_lines = 5 66: 67: # TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc. 68: increment = significant(@maximum_value.to_f / number_of_lines) 69: (0..number_of_lines).each do |index| 70: 71: line_diff = (@graph_right - @graph_left) / number_of_lines 72: x = @graph_right - (line_diff * index) - 1 73: @d = @d.line(x, @graph_bottom, x, @graph_bottom + 0.5 * LABEL_MARGIN) 74: diff = index - number_of_lines 75: marker_label = diff.abs * increment 76: 77: unless @hide_line_numbers 78: @d.fill = @font_color 79: @d.font = @font if @font 80: @d.stroke = 'transparent' 81: @d.pointsize = scale_fontsize(@marker_font_size) 82: @d.gravity = CenterGravity 83: # TODO Center text over line 84: @d = @d.annotate_scaled( @base_image, 85: 0, 0, # Width of box to draw text in 86: x, @graph_bottom + (LABEL_MARGIN * 2.0), # Coordinates of text 87: marker_label.to_s, @scale) 88: end # unless 89: @d = @d.stroke_antialias true 90: end 91: end