Class: RailsDataExplorer::Chart::BoxPlot

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

Overview

Responsibilities:

* Render a box plot for univariate analysis of a quantitative data series.

Collaborators:

* DataSet

Resources:

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

Returns a new instance of BoxPlot.



17
18
19
20
# File 'lib/rails_data_explorer/chart/box_plot.rb', line 17

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

Instance Method Details

#compute_chart_attrsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rails_data_explorer/chart/box_plot.rb', line 22

def compute_chart_attrs
  x_ds = @data_set.data_series.first
  return false  if x_ds.nil?

  {
    values: [x_ds.values],
    min: x_ds.min_val,
    max: x_ds.max_val,
    base_width: 120,
    base_height: 960,
    axis_tick_format: x_ds.axis_tick_format,
  }
end

#renderObject



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails_data_explorer/chart/box_plot.rb', line 36

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

  %(
    <div id="#{ dom_id }" class="rde-chart rde-box-plot">
      <svg class="box" style="height: #{ ca[:base_width] }px; width: 100%;"></svg>

      <script type="text/javascript">
        (function() {
          var base_width = #{ ca[:base_width] },
              base_height = #{ ca[:base_height] },
              margin = { top: 10, right: 50, bottom: 95, left: 50 },
              width = base_width - margin.left - margin.right,
              height = base_height - margin.top - margin.bottom;

          var min = #{ ca[:min] },
              max = #{ ca[:max] };

          var chart = d3.box()
                        .whiskers(iqr(1.5))
                        .width(width)
                        .height(height)
                        .tickFormat(#{ ca[:axis_tick_format] });

          var data = #{ ca[:values].to_json };

          chart.domain([min, max]);

          var svg = d3.select("##{ dom_id }").selectAll("svg")
                      .data(data)
                    .append("g")
                      .attr("transform", "rotate(90) translate(" + (width + margin.left) + " -" + (height + margin.bottom) + ")")
                      .call(chart);

          // Function to compute the interquartile range.
          function iqr(k) {
            return function(d, i) {
              var q1 = d.quartiles[0],
                  q3 = d.quartiles[2],
                  iqr = (q3 - q1) * k,
                  i = -1,
                  j = d.length;
              while (d[++i] < q1 - iqr);
              while (d[--j] > q3 + iqr);
              return [i, j];
            };
          }
        })();
      </script>
    </div>
  )
end