| Class | Gruff::Line |
| In: |
lib/gruff/line.rb
|
| Parent: | Gruff::Base |
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 baseline_value, baseline_color, hide_dots, and hide_lines.
| baseline_color | [RW] | Color of the baseline |
| baseline_value | [RW] | Draw a dashed line at the given value |
| 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. |
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 35
35: def initialize(*args)
36: raise ArgumentError, "Wrong number of arguments" if args.length > 2
37: if args.empty? or ((not Numeric === args.first) && (not String === args.first)) then
38: super()
39: else
40: super args.shift
41: end
42:
43: @hide_dots = @hide_lines = false
44: @baseline_color = 'red'
45: @baseline_value = nil
46: end
# File lib/gruff/line.rb, line 48
48: def draw
49: super
50:
51: return unless @has_data
52:
53: # Check to see if more than one datapoint was given. NaN can result otherwise.
54: @x_increment = (@column_count > 1) ? (@graph_width / (@column_count - 1).to_f) : @graph_width
55:
56: if (defined?(@norm_baseline)) then
57: level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
58: @d = @d.push
59: @d.stroke_color @baseline_color
60: @d.fill_opacity 0.0
61: @d.stroke_dasharray(10, 20)
62: @d.stroke_width 5
63: @d.line(@graph_left, level, @graph_left + @graph_width, level)
64: @d = @d.pop
65: end
66:
67: @norm_data.each do |data_row|
68: prev_x = prev_y = nil
69:
70: data_row[1].each_with_index do |data_point, index|
71: new_x = @graph_left + (@x_increment * index)
72: next if data_point.nil?
73:
74: draw_label(new_x, index)
75:
76: new_y = @graph_top + (@graph_height - data_point * @graph_height)
77:
78: # Reset each time to avoid thin-line errors
79: @d = @d.stroke data_row[DATA_COLOR_INDEX]
80: @d = @d.fill data_row[DATA_COLOR_INDEX]
81: @d = @d.stroke_opacity 1.0
82: @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)
83:
84: if !@hide_lines and !prev_x.nil? and !prev_y.nil? then
85: @d = @d.line(prev_x, prev_y, new_x, new_y)
86: end
87: circle_radius = clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 2.5), 5.0)
88: @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y) unless @hide_dots
89:
90: prev_x = new_x
91: prev_y = new_y
92: end
93:
94: end
95:
96: @d.draw(@base_image)
97: end