Method: RailsDataExplorer::Chart::Scatterplot#compute_chart_attrs

Defined in:
lib/rails-data-explorer/chart/scatterplot.rb

#compute_chart_attrsObject



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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rails-data-explorer/chart/scatterplot.rb', line 10

def compute_chart_attrs
  x_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::Scatterplot] & [:x, :any]).any?
  }
  y_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::Scatterplot] & [:y, :any]).any?
  }
  color_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::Scatterplot] & [:color, :any]).any?
  }
  size_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::Scatterplot] & [:size, :any]).any?
  }

  x_ds = x_candidates.first
  y_ds = (y_candidates - [x_ds]).first
  color_ds = (color_candidates - [x_ds, y_ds]).first
  size_ds = (size_candidates - [x_ds, y_ds, color_ds]).first
  return false  if x_ds.nil? || y_ds.nil?

  ca = case @data_set.dimensions_count
  when 0,1
    raise(ArgumentError.new("At least two data series required for scatterplot, only #{ @data_set.dimensions_count } given"))
  when 2
    key = ''
    values_hash = x_ds.values.length.times.map { |idx|
      r = { x: x_ds.values[idx], y: y_ds.values[idx] }
      r[:color] = color_ds.values[idx]  if color_ds
      r
    }
    {
      values: values_hash,
      x_axis_label: x_ds.name,
      x_axis_tick_format: x_ds.axis_tick_format,
      y_axis_label: y_ds.name,
      y_axis_tick_format: y_ds.axis_tick_format,
    }
  when 3
    visual_attr_ds = color_ds || size_ds
    raise "No visual_attr_ds given"  if visual_attr_ds.nil?
    data_series_hash = visual_attr_ds.values.uniq.inject({}) { |m,visual_attr|
      m[visual_attr] = []
      m
    }
    x_ds.values.length.times.each { |idx|
      data_series_hash[visual_attr_ds.values[idx]] << { x: x_ds.values[idx], y: y_ds.values[idx] }
    }
    {
      values: data_series_hash,
      x_axis_label: x_ds.name,
      x_axis_tick_format: x_ds.axis_tick_format,
      y_axis_label: y_ds.name,
      y_axis_tick_format: y_ds.axis_tick_format,
    }
  else
  end
  ca
end