Class: Gruff::Scatter

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

Overview

Here's how to set up an XY Scatter Chart

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('test/output/scatter.png')

Constant Summary

Constants inherited from Base

Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DATA_VALUES_X_INDEX, Base::DEBUG, Base::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN, Base::THOUSAND_SEPARATOR

Instance Attribute Summary collapse

Attributes inherited from Base

#additional_line_values, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_formatting, #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, #show_labels_for_bar_values, #sort, #sorted_drawing, #stacked, #title, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

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

Methods included from Deprecated

#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height

Constructor Details

#initialize(*args) ⇒ Scatter

Gruff::Scatter takes the same parameters as the Gruff::Line graph

Example

g = Gruff::Scatter.new



43
44
45
46
47
48
49
50
# File 'lib/gruff/scatter.rb', line 43

def initialize(*args)
  super(*args)
  
  @maximum_x_value = @minimum_x_value = nil
  @baseline_x_color = @baseline_y_color = 'red'
  @baseline_x_value = @baseline_y_value = nil
  @marker_x_count = nil
end

Instance Attribute Details

#marker_x_countObject

The number of vertical lines shown for reference



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

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.



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

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.



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

def minimum_x_value
  @minimum_x_value
end

Instance Method Details

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

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.

NOTE: If you want to use a preset theme, you must set it before calling data().

Parameters

name

String or Symbol containing the name of the dataset.

x_data_points

An Array of of x-axis data points.

y_data_points

An Array of of y-axis data points.

color

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

Exceptions

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

x_data_points is empty

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

y_data_points is empty

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

x_data_points.length != y_data_points.length

Error means that the x and y axis point arrays do not match in length

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')

Raises:

  • (ArgumentError)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/gruff/scatter.rb', line 144

def data(name, x_data_points=[], y_data_points=[], color=nil)
  
  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 y axis data
  super(name, y_data_points, color)
  
  #append the x data to the last entry that was just added in the @data member
  last_elem = @data.length()-1
  @data[last_elem] << x_data_points
  
  if @maximum_x_value.nil? && @minimum_x_value.nil?
    @maximum_x_value = @minimum_x_value = x_data_points.first
  end
  
  @maximum_x_value = x_data_points.max > @maximum_x_value ?
                      x_data_points.max : @maximum_x_value
  @minimum_x_value = x_data_points.min < @minimum_x_value ?
                      x_data_points.min : @minimum_x_value
end

#drawObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gruff/scatter.rb', line 63

def draw
  super
  return unless @has_data

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

  #~ if (defined?(@norm_y_baseline)) then
    #~ level = @graph_top + (@graph_height - @norm_baseline * @graph_height)
    #~ @d = @d.push
    #~ @d.stroke_color @baseline_color
    #~ @d.fill_opacity 0.0
    #~ @d.stroke_dasharray(10, 20)
    #~ @d.stroke_width 5
    #~ @d.line(@graph_left, level, @graph_left + @graph_width, level)
    #~ @d = @d.pop
  #~ end

  #~ if (defined?(@norm_x_baseline)) then
    
  #~ end

  @norm_data.each do |data_row|      
    data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
      x_value = data_row[DATA_VALUES_X_INDEX][index]
      next if data_point.nil? || x_value.nil? 

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

      # Reset each time to avoid thin-line errors
      @d = @d.stroke data_row[DATA_COLOR_INDEX]
      @d = @d.fill data_row[DATA_COLOR_INDEX]
      @d = @d.stroke_opacity 1.0
      @d = @d.stroke_width clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 4), 5.0)

      circle_radius = clip_value_if_greater_than(@columns / (@norm_data.first[1].size * 2.5), 5.0)
      @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
    end
  end

  @d.draw(@base_image)
end

#setup_drawingObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/gruff/scatter.rb', line 52

def setup_drawing
  # TODO Need to get x-axis labels working. Current behavior will be to not allow.
  @labels = {}

  super

  # Translate our values so that we can use the base methods for drawing
  # the standard chart stuff
  @column_count = @x_spread
end