Class: Gruff::Scatter

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

Overview

Here’s how to set up a Gruff::Scatter.

g = Gruff::Scatter.new(800)
g.data :apples, [1,2,3,4], [4,3,2,1]
g.data 'oranges', [5,7,8], [4,1,7]
g.write('scatter.png')

Constant Summary

Constants inherited from Base

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

Instance Attribute Summary collapse

Attributes inherited from Base

#bold_title, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_max_size, #label_stagger_height, #label_truncation_style, #labels, #left_margin, #legend_at_bottom, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #sorted_drawing, #title, #title_font, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_increment, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #initialize, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #write

Constructor Details

This class inherits a constructor from Gruff::Base

Instance Attribute Details

#circle_radiusObject

Attributes to allow customising the size of the points.



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

def circle_radius
  @circle_radius
end

#disable_significant_rounding_x_axisObject

Allow disabling the significant rounding when labeling the X axis. This is useful when working with a small range of high values (for example, a date range of months, while seconds as units).



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

def disable_significant_rounding_x_axis
  @disable_significant_rounding_x_axis
end

#enable_vertical_line_markersObject

Allow enabling vertical lines. When you have a lot of data, they can work great.



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

def enable_vertical_line_markers
  @enable_vertical_line_markers
end

#marker_x_countObject

The number of vertical lines shown for reference.



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

def marker_x_count
  @marker_x_count
end

#maximum_x_valueObject

Maximum X Value. The value will get overwritten by the max in the datasets.



16
17
18
# File 'lib/gruff/scatter.rb', line 16

def maximum_x_value
  @maximum_x_value
end

#minimum_x_valueObject

Minimum X Value. The value will get overwritten by the min in the datasets.



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

def minimum_x_value
  @minimum_x_value
end

#stroke_widthObject

Returns the value of attribute stroke_width.



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

def stroke_width
  @stroke_width
end

#use_vertical_x_labelsObject

Returns the value of attribute use_vertical_x_labels.



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

def use_vertical_x_labels
  @use_vertical_x_labels
end

#x_axis_label_formatObject

Returns the value of attribute x_axis_label_format.



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

def x_axis_label_format
  @x_axis_label_format
end

#x_label_marginObject

Allow using vertical labels in the X axis (and setting the label margin).



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

def x_label_margin
  @x_label_margin
end

#y_axis_label_formatObject

Allow passing lambdas to format labels.



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

def y_axis_label_format
  @y_axis_label_format
end

Instance Method Details

#data(name, x_data_points = [], y_data_points = [], color = nil) ⇒ Object Also known as: dataxy

Note:

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

The first parameter is the name of the dataset. The next two are the x and y axis data points contain in their own array in that respective order. The final parameter is the color.

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:

g = Gruff::Scatter.new
g.data(:apples, [1,2,3], [3,2,1])
g.data('oranges', [1,1,1], [2,3,4])
g.data('bitter_melon', [3,5,6], [6,7,8], '#000000')

Parameters:

  • name (String, Symbol)

    containing the name of the dataset.

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

    An Array of of x-axis data points.

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

    An Array of of y-axis data points.

  • color (String) (defaults to: nil)

    The hex string for the color of the dataset. Defaults to nil.

Raises:

  • (ArgumentError)

    Data points contain nil values. This error will get raised if either the x or y axis data points array contains a nil value. The graph will not make an assumption as how to graph nil.

  • (ArgumentError)

    x_data_points is empty. This error is raised when the array for the x-axis points are empty

  • (ArgumentError)

    y_data_points is empty. This error is raised when the array for the y-axis points are empty.

  • (ArgumentError)

    x_data_points.length != y_data_points.length. Error means that the x and y axis point arrays do not match in length.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gruff/scatter.rb', line 123

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

  raise ArgumentError, 'Data Points contain nil Value!' if x_data_points.include?(nil) || y_data_points.include?(nil)
  raise ArgumentError, 'x_data_points is empty!' if x_data_points.empty?
  raise ArgumentError, 'y_data_points is empty!' if y_data_points.empty?
  raise ArgumentError, 'x_data_points.length != y_data_points.length!' if x_data_points.length != y_data_points.length

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

#drawObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gruff/scatter.rb', line 64

def draw
  super
  return unless data_given?

  # Check to see if more than one datapoint was given. NaN can result otherwise.
  @x_increment = (@x_spread > 1) ? (@graph_width / (@x_spread - 1).to_f) : @graph_width

  store.norm_data.each do |data_row|
    data_row.coordinates.each do |x_value, y_value|
      next if y_value.nil? || x_value.nil?

      new_x = get_x_coord(x_value, @graph_width, @graph_left)
      new_y = @graph_top + (@graph_height - y_value * @graph_height)

      # Reset each time to avoid thin-line errors
      stroke_width  = @stroke_width  || clip_value_if_greater_than(@columns / (store.norm_data.first[1].size * 4), 5.0)
      circle_radius = @circle_radius || clip_value_if_greater_than(@columns / (store.norm_data.first[1].size * 2.5), 5.0)
      Gruff::Renderer::Circle.new(color: data_row.color, width: stroke_width).render(new_x, new_y, new_x - circle_radius, new_y)
    end
  end

  Gruff::Renderer.finish
end