Class: RailsDataExplorer::Chart::ParallelCoordinates

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

Overview

Responsibilities:

* Render a parallel coordinates chart for multivariate analysis of
  a mix of quantitative, temporal, and categorical data series.

Collaborators:

* DataSet

TODO: add :color chart_role (test first if it makes sense, e.g., for ‘pay’)

Instance Attribute Summary

Attributes inherited from RailsDataExplorer::Chart

#output_buffer

Instance Method Summary collapse

Methods inherited from RailsDataExplorer::Chart

#dom_id

Constructor Details

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

Returns a new instance of ParallelCoordinates.



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

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

Instance Method Details

#compute_chart_attrsObject



61
62
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
# File 'lib/rails_data_explorer/chart/parallel_coordinates.rb', line 61

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

  dimension_names = dimension_data_series.map(&:name)
  number_of_values = dimension_data_series.first.values.length
  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
  dimension_types = dimension_data_series.inject({}) { |m,ds|
    m[ds.name] = case ds.data_type
    when RailsDataExplorer::DataType::Categorical
      'string'
    when RailsDataExplorer::DataType::Quantitative::Temporal
      'date'
    when RailsDataExplorer::DataType::Quantitative::Integer,
         RailsDataExplorer::DataType::Quantitative::Decimal
      'number'
    else
      raise "Unhandled data_type: #{ ds.data_type.inspect }"
    end
    m
  }
  {
    dimensions: dimension_names,
    values: dimension_values,
    types: dimension_types,
    alpha: 1 / ([Math.log([number_of_values, 2].max), 10].min) # from 1.0 to 0.1
  }
end

#renderObject



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
# File 'lib/rails_data_explorer/chart/parallel_coordinates.rb', line 21

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

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

          parcoords.data(#{ ca[:values].to_json })
                   .render()
                   .createAxes() // has to come before other methods that rely on axes (e.g., brushable)
                   // .shadows() // they don't redraw after reordering, so I'm turning them off for now.
                   .reorderable()
                   .brushable()
                   ;

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

#render?Boolean

Render ParallelCoordinates only when there is at least one data series with DataType Quantitative. If it’s all Categorical, then ParallelSet is much better suited.

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/rails_data_explorer/chart/parallel_coordinates.rb', line 55

def render?
  @data_set.data_series.any? { |ds|
    ds.data_type.is_a?(RailsDataExplorer::DataType::Quantitative)
  }
end