Parent

Gruff::Line

Here’s how to make a Line graph:

  g = Gruff::Line.new
  g.title = "A Line Graph"
  g.data 'Fries', [20, 23, 19, 8]
  g.data 'Hamburgers', [50, 19, 99, 29]
  g.write("test/output/line.png")

There are also other options described below, such as #, #, #, and #.

Attributes

baseline_value[RW]

Draw a dashed line at the given value

baseline_color[RW]

Color of the baseline

line_width[RW]

Dimensions of lines and dots; calculated based on dataset size if left unspecified

dot_radius[RW]
hide_dots[RW]

Hide parts of the graph to fit more datapoints, or for a different appearance.

hide_lines[RW]

Hide parts of the graph to fit more datapoints, or for a different appearance.

Public Class Methods

new(*args) click to toggle source

Call with target pixel width of graph (800, 400, 300), and/or ‘false’ to omit lines (points only).

 g = Gruff::Line.new(400) # 400px wide with lines

 g = Gruff::Line.new(400, false) # 400px wide, no lines (for backwards compatibility)

 g = Gruff::Line.new(false) # Defaults to 800px wide, no lines (for backwards compatibility)

The preferred way is to call hide_dots or hide_lines instead.

    # File lib/gruff/line.rb, line 39
39:   def initialize(*args)
40:     raise ArgumentError, "Wrong number of arguments" if args.length > 2
41:     if args.empty? or ((not Numeric === args.first) && (not String === args.first)) then
42:       super()
43:     else
44:       super args.shift
45:     end
46: 
47:     @hide_dots = @hide_lines = false
48:     @baseline_color = 'red'
49:     @baseline_value = nil
50:   end

Public Instance Methods

contains_one_point_only?(data_row) click to toggle source
     # File lib/gruff/line.rb, line 118
118:   def contains_one_point_only?(data_row)
119:     # Spin through data to determine if there is just one_value present.
120:     one_point = false
121:     data_row[DATA_VALUES_INDEX].each do |data_point|
122:       if !data_point.nil?
123:         if one_point
124:           # more than one point, bail
125:           return false
126:         else
127:           # there is at least one data point
128:           return true
129:         end
130:       end
131:     end
132:     return one_point
133:   end
draw() click to toggle source
     # File lib/gruff/line.rb, line 52
 52:   def draw
 53:     super
 54: 
 55:     return unless @has_data
 56: 
 57:     # Check to see if more than one datapoint was given. NaN can result otherwise.
 58:     @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
 59: 
 60:     if (defined?(@norm_baseline)) then
 61:       level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
 62:       @d = @d.push
 63:       @d.stroke_color @baseline_color
 64:       @d.fill_opacity 0.0
 65:       @d.stroke_dasharray(10, 20)
 66:       @d.stroke_width 5
 67:       @d.line(@graph_left, level, @graph_left + @graph_width, level)
 68:       @d = @d.pop
 69:     end
 70: 
 71:     @norm_data.each do |data_row|
 72:       prev_x = prev_y = nil
 73: 
 74:       @one_point = contains_one_point_only?(data_row)
 75: 
 76:       data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
 77:         new_x = @graph_left + (@x_increment * index)
 78:         next if data_point.nil?
 79: 
 80:         draw_label(new_x, index)
 81: 
 82:         new_y = @graph_top + (@graph_height - data_point * @graph_height)
 83: 
 84:         # Reset each time to avoid thin-line errors
 85:         @d = @d.stroke data_row[DATA_COLOR_INDEX]
 86:         @d = @d.fill data_row[DATA_COLOR_INDEX]
 87:         @d = @d.stroke_opacity 1.0
 88:         @d = @d.stroke_width line_width ||
 89:           clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 4), 5.0)
 90: 
 91: 
 92:         circle_radius = dot_radius ||
 93:           clip_value_if_greater_than(@columns / (@norm_data.first[DATA_VALUES_INDEX].size * 2.5), 5.0)
 94: 
 95:         if !@hide_lines and !prev_x.nil? and !prev_y.nil? then
 96:           @d = @d.line(prev_x, prev_y, new_x, new_y)
 97:         elsif @one_point
 98:           # Show a circle if there's just one_point
 99:           @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
100:         end
101:         @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) unless @hide_dots
102: 
103:         prev_x = new_x
104:         prev_y = new_y
105:       end
106: 
107:     end
108: 
109:     @d.draw(@base_image)
110:   end
normalize() click to toggle source
     # File lib/gruff/line.rb, line 112
112:   def normalize
113:     @maximum_value = [@maximum_value.to_f, @baseline_value.to_f].max
114:     super
115:     @norm_baseline = (@baseline_value.to_f / @maximum_value.to_f) if @baseline_value
116:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.