Class: RailsDataExplorer::Chart::PieChart

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

Overview

Responsibilities:

* Render a pie chart for univariate analysis of a categorical data series.

Collaborators:

* DataSet

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

Returns a new instance of PieChart.



14
15
16
17
# File 'lib/rails_data_explorer/chart/pie_chart.rb', line 14

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

Instance Method Details

#compute_chart_attrsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_data_explorer/chart/pie_chart.rb', line 19

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

  val_mod = { name: :limit_distinct_values }
  total_count = x_ds.values(val_mod).length
  # compute histogram
  h = x_ds.values(val_mod).inject(Hash.new(0)) { |m,e| m[e] += 1; m }
  {
    values: h.map { |k,v|
      { key: k, value: (v / total_count.to_f) }
    }.sort(
      &x_ds.label_sorter(
        :key,
        lambda { |a,b| b[:value] <=> a[:value] }
      )
    )
  }
end

#renderObject



39
40
41
42
43
44
# File 'lib/rails_data_explorer/chart/pie_chart.rb', line 39

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

#render_nvd3(ca) ⇒ Object



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

def render_nvd3(ca)
  %(
    <div class="rde-chart rde-pie-chart">
      <h3 class="rde-chart-title">Pie Chart</h3>
      <div id="#{ dom_id }", style="height: 400px; width: 400px;">
        <svg></svg>
      </div>
      <script type="text/javascript">
        (function() {
          var data = #{ ca[:values].to_json };

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

            chart.valueFormat(d3.format('.1%'))
                 .donut(true)
              ;
            chart.tooltipContent(
              function(key, y, e, graph) {
                return '<p>' + key + '</p>' + '<p>' +  y + '</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



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rails_data_explorer/chart/pie_chart.rb', line 46

def render_vega(ca)
  %(
    <div class="rde-chart rde-pie-chart">
      <h3 class="rde-chart-title">Pie Chart</h3>
      <div id="#{ dom_id }"></div>
      <script type="text/javascript">
        (function() {
          var spec = {
            "width": 300,
            "height": 300,
            "padding": {"top": 10, "left": 10, "bottom": 10, "right": 100},
            "data": [
              {
                "name": "table",
                "values": #{ ca[:values].to_json }
              }
            ],
            "scales": [
              {
                "name": "color",
                "domain": {"data": "table", "field": "data.key"},
                "range": "category10",
                "type": "ordinal"
              },
            ],
            "axes": [],
            "marks": [
              {
                "type": "arc",
                "from": {
                  "data": "table",
                  "transform": [{"type": "pie", "value": "data.value"}]
                },
                "properties": {
                  "enter": {
                    "x": {"group": "width", "mult": 0.5},
                    "y": {"group": "height", "mult": 0.5},
                    "endAngle": {"field": "endAngle"},
                    "innerRadius": {"value": 100},
                    "outerRadius": {"value": 150},
                    "startAngle": {"field": "startAngle"},
                    "stroke": {"value": "white"},
                    "fill": {"field": "data.key", "scale": "color"}
                  }
                }
              }
            ],
            "legends": [
              {
                "fill": "color",
              }
            ],
          };

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

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