Class: RailsDataExplorer::Chart::ParallelSet

Inherits:
RailsDataExplorer::Chart show all
Defined in:
lib/rails_data_explorer/chart/parallel_set.rb

Overview

Responsibilities:

* Render a parallel set chart for multivariate analysis of categorical
  data series.

Collaborators:

* DataSet

www.jasondavies.com/parallel-sets/

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 = {}) ⇒ ParallelSet

Returns a new instance of ParallelSet.



16
17
18
19
# File 'lib/rails_data_explorer/chart/parallel_set.rb', line 16

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

Instance Method Details

#compute_chart_attrsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails_data_explorer/chart/parallel_set.rb', line 21

def compute_chart_attrs
  dimension_data_series = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::ParallelSet] & [:dimension, :any]).any?
  }
  return false  if dimension_data_series.empty?

  number_of_values = dimension_data_series.first.values.length
  dimension_names = dimension_data_series.map(&:name)
  dimension_values = number_of_values.times.map do |idx|
    dimension_data_series.inject({}) { |m,ds|
      m[ds.name] = if ds.data_type.is_a?(RailsDataExplorer::DataType::Quantitative::Temporal)
        ds.values[idx].to_i * 1000
      else
        ds.values[idx]
      end
      m
    }
  end
  {
    dimensions: dimension_names,
    values: dimension_values
  }
end

#renderObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rails_data_explorer/chart/parallel_set.rb', line 45

def render
  return ''  unless render?
  ca = compute_chart_attrs
  return ''  unless ca

  %(
    <div class="rde-chart rde-parallel-set">
      <h3 class="rde-chart-title">Parallel Set</h3>
      <div id="#{ dom_id }" class="rde-chart-parallel-set" style="height: 600px; width: 100%"></div>
      <script type="text/javascript">
        (function() {
          var parset = d3.parsets()
                         .dimensions(#{ ca[:dimensions ].to_json })
                         ;

          var vis = d3.select("##{ dom_id }")
                      .append("svg")
                      .attr("width", parset.width())
                      .attr("height", parset.height())
                      ;

          vis.datum(#{ ca[:values].to_json })
             .call(parset)
             ;

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