Class: Rubyplot::Bubble

Inherits:
Scatter show all
Defined in:
lib/rubyplot/scripting/bubble/data.rb,
lib/rubyplot/scripting/bubble/draw.rb,
lib/rubyplot/scripting/bubble/init.rb

Constant Summary

Constants inherited from Artist

Artist::DATA_COLOR_INDEX, Artist::DATA_LABEL_INDEX, Artist::DATA_VALUES_INDEX, Artist::DATA_VALUES_X_INDEX, Artist::DEFAULT_MARGIN, Artist::DEFAULT_TARGET_WIDTH, Artist::LABEL_MARGIN, Artist::LEGEND_MARGIN, Artist::THOUSAND_SEPARATOR

Instance Attribute Summary

Attributes inherited from Scatter

#circle_radius, #disable_significant_rounding_x_axis, #enable_vertical_line_markers, #marker_x_count, #maximum_x_value, #minimum_x_value, #stroke_width, #use_vertical_x_labels, #x_axis_label_format, #x_label_margin, #y_axis_label_format

Attributes inherited from Artist

#base_image, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_numbers, #labels, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_shadow_color, #maximum_value, #minimum_value, #title, #title_margin, #x_axis_label, #y_axis_label

Instance Method Summary collapse

Methods inherited from Scatter

#calculate_spread, #normalize, #setup_drawing

Methods inherited from Artist

#artist_draw, #calculate_caps_height, #calculate_spread, #calculate_width, #center, #clip_value_if_greater_than, #construct_colors_array, #draw_axis_labels, #draw_label, #draw_legend, #draw_line_markers!, #draw_title, #get_colors_array, #initialize_variables, #normalize, #render_gradiated_background, #reset_themes, #scale, #scale_fontsize, #set_colors_array, #setup_drawing, #setup_graph_measurements, #significant, #sort_norm_data, #sum, #theme=, #write

Constructor Details

#initializeBubble

Rubyplot::Bubble takes the same parameters as the Rubyplot::Bubble graph

Example

g = Rubyplot::Bubble.new


7
8
9
10
11
12
# File 'lib/rubyplot/scripting/bubble/init.rb', line 7

def initialize(*)
  super
  @all_colors_array = Magick.colors
  @plot_colors = []
  @z_data = []
end

Instance Method Details

#data(name, x_data_points = [], y_data_points = [], z_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.

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 = Rubyplot::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')


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubyplot/scripting/bubble/data.rb', line 70

def data(name, x_data_points = [], y_data_points = [], z_data_points = [], _color = nil)
  # Call the existing data routine for the y axis data
  data_y(name, y_data_points, z_data_points)
  # 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 @geometry.maximum_x_value.nil? && @geometry.minimum_x_value.nil?
    @geometry.maximum_x_value = @geometry.minimum_x_value = x_data_points.first
  end
  @z_data << z_data_points
  x_z_array_sum = [x_data_points, z_data_points].transpose.map { |x| x.reduce(:+) }
  x_z_array_diff = [x_data_points, z_data_points].transpose.map { |x| x.reduce(:-) }

  @geometry.maximum_x_value = x_z_array_sum.max > @geometry.maximum_x_value ?
                      x_z_array_sum.max : @geometry.maximum_x_value
  @geometry.minimum_x_value = x_z_array_sum.min < @geometry.minimum_x_value ?
                      x_z_array_sum.min : @geometry.minimum_x_value

  @geometry.maximum_x_value = x_z_array_diff.max > @geometry.maximum_x_value ?
                      x_z_array_diff.max : @geometry.maximum_x_value
  @geometry.minimum_x_value = x_z_array_diff.min < @geometry.minimum_x_value ?
                      x_z_array_diff.min : @geometry.minimum_x_value
end

#data_y(name, data_points = [], z_data_points) ⇒ Object

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.

Example:

data("Arafat", [95, 45, 78, 89, 88, 76])

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubyplot/scripting/bubble/data.rb', line 10

def data_y(name, data_points = [], z_data_points)
  data_points = Array(data_points) # make sure it's an array
  # TODO: Adding an empty color array which can be developed later
  # to make graphs super customizable with regards to coloring of
  # individual data points.
  color = []
  @data << [name, data_points, color]
  # Set column count if this is larger than previous column counts
  @geometry.column_count = data_points.length > @geometry.column_count ? data_points.length : @geometry.column_count

  y_z_array_sum = [data_points, z_data_points].transpose.map { |x| x.reduce(:+) }
  y_z_array_diff = [data_points, z_data_points].transpose.map { |x| x.reduce(:-) }
  if @geometry.maximum_value.nil? && @geometry.maximum_value.nil?
    @geometry.maximum_value = @geometry.minimum_value = data_points.first
  end
  @geometry.maximum_value = y_z_array_sum.max > @geometry.maximum_value ?
                      y_z_array_sum.max : @geometry.maximum_value
  @geometry.minimum_value = y_z_array_sum.min < @geometry.minimum_value ?
                      y_z_array_sum.min : @geometry.minimum_value
  @geometry.maximum_value = y_z_array_diff.max > @geometry.maximum_value ?
                      y_z_array_diff.max : @geometry.maximum_value
  @geometry.minimum_value = y_z_array_diff.min < @geometry.minimum_value ?
                      y_z_array_diff.min : @geometry.minimum_value
  @geometry.has_data = true
end

#drawObject


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubyplot/scripting/bubble/draw.rb', line 2

def draw
  super

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

  @geometry.norm_data.each_with_index do |data_row, data_row_index|
    data_row[DATA_VALUES_INDEX].each_with_index do |data_point, index|
      @d = @d.fill @plot_colors[data_row_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_opacity 1.0
      @d.fill_opacity(0.3)
      @d.fill_color(@plot_colors[data_row_index])
      @d = @d.stroke_width @stroke_width || clip_value_if_greater_than(@columns / (@geometry.norm_data.first[1].size * 4), 5.0)

      circle_radius = 2 * @z_data[data_row_index][index]
      @d = @d.circle(new_x, new_y, new_x - circle_radius, new_y)
    end
  end
  @d.draw(@base_image)
end