Class: RailsDataExplorer::Chart::StackedBarChartCategorical

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

Overview

Responsibilities:

* Render a stacked bar chart for bivariate analysis of two categorical
  data series. Renders absolute frequencies of y-data series.

Collaborators:

* DataSet

Direct Known Subclasses

StackedBarChartCategoricalPercent

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

Returns a new instance of StackedBarChartCategorical.



15
16
17
18
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.rb', line 20

def compute_chart_attrs
  val_mod = { name: :limit_distinct_values }

  x_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::StackedBarChartCategoricalPercent] & [:x, :any]).any?
  }.sort { |a,b| b.uniq_vals_count(val_mod) <=> a.uniq_vals_count(val_mod) }
  y_candidates = @data_set.data_series.find_all { |ds|
    (ds.chart_roles[Chart::StackedBarChartCategoricalPercent] & [:y, :any]).any?
  }

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

  # initialize data_matrix
  data_matrix = { _sum: { _sum: 0 } }
  x_ds.uniq_vals(val_mod).each { |x_val|
    data_matrix[x_val] = {}
    data_matrix[x_val][:_sum] = 0
    y_ds.uniq_vals(val_mod).each { |y_val|
      data_matrix[x_val][y_val] = 0
      data_matrix[:_sum][y_val] = 0
    }
  }
  # populate data_matrix
  x_ds.values(val_mod).length.times { |idx|
    x_val = x_ds.values(val_mod)[idx]
    y_val = y_ds.values(val_mod)[idx]
    data_matrix[x_val][y_val] += 1
    data_matrix[:_sum][y_val] += 1
    data_matrix[x_val][:_sum] += 1
    data_matrix[:_sum][:_sum] += 1
  }
  x_sorted_keys = x_ds.uniq_vals(val_mod).sort(
    &x_ds.label_sorter(
      nil,
      lambda { |a,b| data_matrix[b][:_sum] <=> data_matrix[a][:_sum] }
    )
  )
  y_sorted_keys = y_ds.uniq_vals(val_mod).sort(
    &y_ds.label_sorter(
      nil,
      lambda { |a,b| data_matrix[:_sum][b] <=> data_matrix[:_sum][a] }
    )
  )

  values = case @data_set.dimensions_count
  when 2
    y_sorted_keys.map { |y_val|
      x_sorted_keys.map { |x_val|
        {
          x: x_val,
          y: compute_y_value(data_matrix, x_val, y_val),
          c: y_val
        }
      }
    }.flatten
  else
    raise(ArgumentError.new("Exactly two data series required for contingency table."))
  end
  {
    values: values,
    x_axis_label: x_ds.name,
    x_axis_tick_format: 'function(d) { return d }',
    y_axis_label: compute_y_axis_label(y_ds.name),
    y_axis_tick_format: "d3.format('.1%')",
  }
end

#compute_y_axis_label(y_ds_name) ⇒ Object

Parameters:

  • y_ds_name (String)

    name of the y data series



96
97
98
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.rb', line 96

def compute_y_axis_label(y_ds_name)
  "Frequency"
end

#compute_y_value(data_matrix, x_val, y_val) ⇒ Object

Override this method to change how the y value is computed. E.g., to change from absolute values to percentages.



91
92
93
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.rb', line 91

def compute_y_value(data_matrix, x_val, y_val)
  data_matrix[x_val][y_val]
end

#renderObject



100
101
102
103
104
105
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.rb', line 100

def render
  return ''  unless render?
  ca = compute_chart_attrs
  return ''  unless ca
  render_vega(ca)
end

#render_nvd3(ca) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.rb', line 210

def render_nvd3(ca)
  %(
    <div class="rde-chart rde-stacked-bar-chart-categorical-percent">
      <h3 class="rde-chart-title">Stacked Bar Chart</h3>
      <div id="#{ dom_id }", style="height: 200px;">
        <svg></svg>
      </div>
      <script type="text/javascript">
        (function() {
          var data = #{ ca[:values].to_json };

          nv.addGraph(function() {
            var chart = nv.models.multiBarChart()
              ;

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

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

            chart.multibar.stacked(true);
            chart.showControls(false);
            chart.tooltipContent(
              function(key, x, y, e, graph) {
                return '<p>' + key + '</p>' + '<p>' +  y + ' of ' + x + '</p>'
              }
            );


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

            nv.utils.windowResize(chart.update);

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

#render_vega(ca) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rails_data_explorer/chart/stacked_bar_chart_categorical.rb', line 107

def render_vega(ca)
  %(
    <div class="rde-chart rde-stacked-bar-chart-categorical-percent">
      <h3 class="rde-chart-title">Stacked Bar Chart</h3>
      <div id="#{ dom_id }"></div>
      <script type="text/javascript">
        (function() {
          var spec = {
            "width": 960,
            "height": 200,
            "padding": {"top": 10, "left": 70, "bottom": 50, "right": 100},
            "data": [
              {
                "name": "table",
                "values": #{ ca[:values].to_json }
              },
              {
                "name": "stats",
                "source": "table",
                "transform": [
                  {"type": "facet", "keys": ["data.x"]},
                  {"type": "stats", "value": "data.y"}
                ]
              }
            ],
            "scales": [
              {
                "name": "x",
                "type": "ordinal",
                "range": "width",
                "domain": {"data": "table", "field": "data.x"}
              },
              {
                "name": "y",
                "type": "linear",
                "range": "height",
                "nice": true,
                "domain": {"data": "stats", "field": "sum"}
              },
              {
                "name": "color",
                "type": "ordinal",
                "range": "category10"
              }
            ],
            "axes": [
              {
                "type": "x",
                "scale": "x",
                "title": "#{ ca[:x_axis_label] }",
                "format": #{ ca[:x_axis_tick_format] },
              },
              {
                "type": "y",
                "scale": "y",
                "title": "#{ ca[:y_axis_label] }",
                "format": #{ ca[:y_axis_tick_format] },
                "titleOffset": 60,
              }
            ],
            "marks": [
              {
                "type": "group",
                "from": {
                  "data": "table",
                  "transform": [
                    {"type": "facet", "keys": ["data.c"]},
                    {"type": "stack", "point": "data.x", "height": "data.y"}
                  ]
                },
                "marks": [
                  {
                    "type": "rect",
                    "properties": {
                      "enter": {
                        "x": {"scale": "x", "field": "data.x"},
                        "width": {"scale": "x", "band": true, "offset": -1},
                        "y": {"scale": "y", "field": "y"},
                        "y2": {"scale": "y", "field": "y2"},
                        "fill": {"scale": "color", "field": "data.c"}
                      },
                    }
                  }
                ]
              }
            ],
            "legends": [
              {
                "fill": "color",
              }
            ],
          };

          vg.parse.spec(spec, function(chart) {
            var view = chart({ el:"##{ dom_id }" }).update();
          });

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