Class: RailsDataExplorer::Chart::BoxPlotGroup

Inherits:
RailsDataExplorer::Chart show all
Defined in:
lib/rails-data-explorer/chart/box_plot_group.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 = {}) ⇒ BoxPlotGroup



15
16
17
18
# File 'lib/rails-data-explorer/chart/box_plot_group.rb', line 15

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

Instance Method Details

#compute_chart_attrsObject



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
# File 'lib/rails-data-explorer/chart/box_plot_group.rb', line 20

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

  x_ds = x_candidates.first
  y_ds = (y_candidates - [x_ds]).first
  return false  if x_ds.nil? || y_ds.nil?

  min = x_ds.min_val # get global min
  max = x_ds.max_val # get global max

  values_hash = y_ds.uniq_vals.inject({}) { |m,y_val|
    m[y_val] = []
    m
  }

  y_ds.values.each_with_index { |y_val, idx|
    next  if (y_val.nil? || Float::NAN == y_val)
    values_hash[y_val] << x_ds.values[idx]
  }
  y_sorted_keys = y_ds.uniq_vals.sort(
    &y_ds.label_sorter(
      nil,
      lambda { |a,b| a <=> b }
    )
  )
  sorted_values = y_sorted_keys.map { |y_val| values_hash[y_val] }

  {
    values: sorted_values,
    category_labels: y_sorted_keys,
    min: min,
    max: max,
    base_width: 120,
    base_height: 800,
    axis_tick_format: x_ds.axis_tick_format,
    num_box_plots: y_ds.uniq_vals_count,
    axis_scale: DataSeries.new('_', [min, max]).axis_scale(:d3)
  }
end

#renderObject



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rails-data-explorer/chart/box_plot_group.rb', line 65

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

  svg_trs = ca[:category_labels].map { |cat_label|
    %(
      <tr>
        <td style="vertical-align: middle;">#{ cat_label }</td>
        <td style="vertical-align: middle; width: 100%">
          <svg class="box" style="height: #{ ca[:base_width] }px;"></svg>
        </td>
      </tr>
    )
  }.join.html_safe
  %(
    <div id="#{ dom_id }" class="rde-chart rde-box-plot-group">
      <table>#{ svg_trs }</table>

      <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]);
          chart.scale(#{ ca[:axis_scale] });

          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