Class: RailsDataExplorer::Chart::Scatterplot

Inherits:
RailsDataExplorer::Chart show all
Defined in:
lib/rails-data-explorer/chart/scatterplot.rb

Instance Attribute Summary

Attributes inherited from RailsDataExplorer::Chart

#output_buffer

Instance Method Summary collapse

Methods inherited from RailsDataExplorer::Chart

#dom_id, #render?

Constructor Details

#initialize(_data_set, options = {}) ⇒ Scatterplot

Returns a new instance of Scatterplot.



5
6
7
8
# File 'lib/rails-data-explorer/chart/scatterplot.rb', line 5

def initialize(_data_set, options = {})
  @data_set = _data_set
  @options = {}.merge(options)
end

Instance Method Details

#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
# 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

  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: [ { key: key, 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.map { |k,v| { key: k, values: v } },
      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

#renderObject



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
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails-data-explorer/chart/scatterplot.rb', line 68

def render
  return ''  unless render?
  chart_attrs = compute_chart_attrs
  %(
    <div class="rde-chart rde-scatterplot">
      <h3 class="rde-chart-title">Scatterplot</h3>
      <div id="#{ dom_id }", style="height: 400px;">
        <svg></svg>
      </div>
      <script type="text/javascript">
        (function() {
          var data = #{ chart_attrs[:values].to_json };

          nv.addGraph(function() {
            var chart = nv.models.scatterChart()
                          .showDistX(true)
                          .showDistY(true)
                          .useVoronoi(true)
                          .color(d3.scale.category10().range())
                          .transitionDuration(300)
                          ;

            chart.xAxis.tickFormat(#{ chart_attrs[:x_axis_tick_format] })
                       .axisLabel('#{ chart_attrs[:x_axis_label] }')
                       ;

            chart.yAxis.tickFormat(#{ chart_attrs[:y_axis_tick_format] })
                       .axisLabel('#{ chart_attrs[:y_axis_label] }')
                       ;

            chart.tooltipContent(function(key) {
                return key;
            });

            d3.select('##{ dom_id } svg')
                .datum(data)
                .call(chart);

            nv.utils.windowResize(chart.update);

            chart.dispatch.on('stateChange', function(e) { ('New State:', JSON.stringify(e)); });

            return chart;
          });
        })();
      </script>
    </div>
  )
end