Class: Gruff::Line

Inherits:
Base
  • Object
show all
Defined in:
lib/gruff/line.rb

Overview

Here’s how to make a Gruff::Line.

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("line.png")

There are also other options described below, such as #baseline_value, #baseline_color, #hide_dots=, and #hide_lines=.

Constant Summary

Constants inherited from Base

Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN, Base::TITLE_MARGIN

Instance Attribute Summary collapse

Attributes inherited from Base

#bottom_margin, #colors, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_margin, #label_max_size, #label_truncation_style, #left_margin, #legend_at_bottom, #legend_box_size, #legend_margin, #marker_color, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #sorted_drawing, #title_margin, #top_margin, #x_axis_increment, #x_axis_label_format, #y_axis_increment, #y_axis_label_format

Instance Method Summary collapse

Methods inherited from Base

#add_color, #bold_title=, #draw, #font=, #font_color=, #label_rotation=, #label_stagger_height=, #labels=, #legend_font_size=, #margins=, #marker_font_size=, #no_data_font_size=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #title=, #title_font=, #title_font_size=, #to_blob, #to_image, #transparent_background=, #write

Constructor Details

#initialize(*args) ⇒ Line

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.

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
# File 'lib/gruff/line.rb', line 55

def initialize(*args)
  raise ArgumentError, 'Wrong number of arguments' if args.length > 2

  if args.empty? || (!args.first.is_a?(Numeric) && !args.first.is_a?(String))
    super()
  else
    super(args.shift)
  end
end

Instance Attribute Details

#dot_radius=(value) ⇒ Object (writeonly)

: Float | Integer



28
29
30
# File 'lib/gruff/line.rb', line 28

def dot_radius=(value)
  @dot_radius = value
end

#dot_style=(value) ⇒ Object (writeonly)

default is ‘circle’, other options include square and diamond.



31
32
33
# File 'lib/gruff/line.rb', line 31

def dot_style=(value)
  @dot_style = value
end

#hide_dots=(value) ⇒ Object (writeonly)

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



34
35
36
# File 'lib/gruff/line.rb', line 34

def hide_dots=(value)
  @hide_dots = value
end

#hide_lines=(value) ⇒ Object (writeonly)

: bool



35
36
37
# File 'lib/gruff/line.rb', line 35

def hide_lines=(value)
  @hide_lines = value
end

#line_width=(value) ⇒ Object (writeonly)

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



27
28
29
# File 'lib/gruff/line.rb', line 27

def line_width=(value)
  @line_width = value
end

#marker_x_count=(value) ⇒ Object (writeonly)

The number of vertical lines shown.



44
45
46
# File 'lib/gruff/line.rb', line 44

def marker_x_count=(value)
  @marker_x_count = value
end

#maximum_x_value=(value) ⇒ Object (writeonly)

accessors for support of xy data.



41
42
43
# File 'lib/gruff/line.rb', line 41

def maximum_x_value=(value)
  @maximum_x_value = value
end

#minimum_x_value=(value) ⇒ Object (writeonly)

accessors for support of xy data.



38
39
40
# File 'lib/gruff/line.rb', line 38

def minimum_x_value=(value)
  @minimum_x_value = value
end

#reference_line_default_color=(value) ⇒ Object (writeonly)

: String



20
21
22
# File 'lib/gruff/line.rb', line 20

def reference_line_default_color=(value)
  @reference_line_default_color = value
end

#reference_line_default_width=(value) ⇒ Object (writeonly)

: Float | Integer



21
22
23
# File 'lib/gruff/line.rb', line 21

def reference_line_default_width=(value)
  @reference_line_default_width = value
end

#reference_linesObject

Allow for reference lines ( which are like baseline … just allowing for more & on both axes ).



19
20
21
# File 'lib/gruff/line.rb', line 19

def reference_lines
  @reference_lines
end

#show_vertical_markers=(value) ⇒ Object (writeonly)

Allow for vertical marker lines.



24
25
26
# File 'lib/gruff/line.rb', line 24

def show_vertical_markers=(value)
  @show_vertical_markers = value
end

Instance Method Details

#baseline_colorObject



83
84
85
86
87
# File 'lib/gruff/line.rb', line 83

def baseline_color
  if @reference_lines.key?(:baseline)
    @reference_lines[:baseline][:color]
  end
end

#baseline_color=(new_value) ⇒ Object



90
91
92
93
# File 'lib/gruff/line.rb', line 90

def baseline_color=(new_value)
  @reference_lines[:baseline] ||= {}
  @reference_lines[:baseline][:color] = new_value
end

#baseline_valueObject

Get the value if somebody has defined it.



68
69
70
71
72
# File 'lib/gruff/line.rb', line 68

def baseline_value
  if @reference_lines.key?(:baseline)
    @reference_lines[:baseline][:value]
  end
end

#baseline_value=(new_value) ⇒ Object

Set a value for a baseline reference line..



77
78
79
80
# File 'lib/gruff/line.rb', line 77

def baseline_value=(new_value)
  @reference_lines[:baseline] ||= {}
  @reference_lines[:baseline][:value] = new_value
end

#data(name, data_points = [], color = nil) ⇒ Object

Note:

If you want to use a preset theme, you must set it before calling #data.

Input the data in the graph.

Parameters are an array where the first element is the name of the dataset and the value is an array of values to plot.

Can be called multiple times with different datasets for a multi-valued graph.

If the color argument is nil, the next color from the default theme will be used.

Examples:

data("Bart S.", [95, 45, 78, 89, 88, 76], '#ffcc00')

Parameters:

  • name (String, Symbol)

    The name of the dataset.

  • data_points (Array) (defaults to: [])

    The array of dataset.

  • color (String) (defaults to: nil)

    The color for drawing graph of dataset.



118
119
120
# File 'lib/gruff/line.rb', line 118

def data(name, data_points = [], color = nil)
  store.add(name, nil, data_points, color)
end

#dataxy(name, x_data_points = [], y_data_points = [], color = nil) ⇒ Object #dataxy(name, xy_data_points = [], color = nil) ⇒ Object

Note:
  • if (x_data_points.length != y_data_points.length) an error is returned.

  • if the color argument is nil, the next color from the default theme will be used.

  • if you want to use a preset theme, you must set it before calling #dataxy.

This method allows one to plot a dataset with both X and Y data.

Examples:

g = Gruff::Line.new
g.title = "X/Y Dataset"
g.dataxy("Apples", [1,3,4,5,6,10], [1, 2, 3, 4, 4, 3])
g.dataxy("Bapples", [1,3,4,5,7,9], [1, 1, 2, 2, 3, 3])
g.dataxy("Capples", [[1,1],[2,3],[3,4],[4,5],[5,7],[6,9]])

# you can still use the old data method too if you want:
g.data("Capples", [1, 1, 2, 2, 3, 3])

# labels will be drawn at the x locations of the keys passed in.
In this example the labels are drawn at x positions 2, 4, and 6:
g.labels = {0 => '2003', 2 => '2004', 4 => '2005', 6 => '2006'}
# The 0 => '2003' label will be ignored since it is outside the chart range.

Overloads:

  • #dataxy(name, x_data_points = [], y_data_points = [], color = nil) ⇒ Object

    Parameters:

    • name (String)

      the title of the dataset.

    • x_data_points (Array) (defaults to: [])

      an array containing the x data points for the graph.

    • y_data_points (Array) (defaults to: [])

      an array containing the y data points for the graph.

    • color (String) (defaults to: nil)

      hex number indicating the line color as an RGB triplet.

  • #dataxy(name, xy_data_points = [], color = nil) ⇒ Object

    Parameters:

    • name (String)

      the title of the dataset.

    • xy_data_points (Array) (defaults to: [])

      an array containing both x and y data points for the graph.

    • color (String) (defaults to: nil)

      hex number indicating the line color as an RGB triplet.

Raises:

  • (ArgumentError)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gruff/line.rb', line 161

def dataxy(name, x_data_points = [], y_data_points = [], color = nil)
  # make sure it's an array
  x_data_points = Array(x_data_points)

  raise ArgumentError, 'x_data_points is nil!' if x_data_points.empty?

  if x_data_points.all? { |p| p.is_a?(Array) && p.size == 2 }
    color = y_data_points if y_data_points.is_a?(String)
    x_data_points, y_data_points = x_data_points.transpose
  else
    y_data_points = Array(y_data_points)
  end

  raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length # steep:ignore

  # call the existing data routine for the x/y data.
  store.add(name, x_data_points, y_data_points, color)
end