Class: RailsDataExplorer::Chart::HistogramQuantitative
- Inherits:
-
RailsDataExplorer::Chart
- Object
- RailsDataExplorer::Chart
- RailsDataExplorer::Chart::HistogramQuantitative
- Defined in:
- lib/rails_data_explorer/chart/histogram_quantitative.rb
Overview
Responsibilities:
* Render a histogram for univariate analysis of a quantitative data series.
Collaborators:
* DataSet
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from RailsDataExplorer::Chart
Instance Method Summary collapse
- #compute_chart_attrs ⇒ Object
-
#initialize(_data_set, options = {}) ⇒ HistogramQuantitative
constructor
A new instance of HistogramQuantitative.
- #render ⇒ Object
- #render_nvd3(ca) ⇒ Object
- #render_vega(ca) ⇒ Object
Methods inherited from RailsDataExplorer::Chart
Constructor Details
#initialize(_data_set, options = {}) ⇒ HistogramQuantitative
Returns a new instance of HistogramQuantitative.
14 15 16 17 |
# File 'lib/rails_data_explorer/chart/histogram_quantitative.rb', line 14 def initialize(_data_set, = {}) @data_set = _data_set @options = {}.merge() end |
Instance Method Details
#compute_chart_attrs ⇒ Object
19 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 |
# File 'lib/rails_data_explorer/chart/histogram_quantitative.rb', line 19 def compute_chart_attrs x_ds = @data_set.data_series.first return false if x_ds.nil? # compute histogram quantizer = Utils::DataQuantizer.new(x_ds, max_number_of_bins: 100) quantized_values = quantizer.values = quantizer.number_of_bins width = 960 h = quantized_values.inject(Hash.new(0)) { |m,e| m[e] += 1; m } histogram_values_ds = DataSeries.new('_', h.values) y_scale_type = histogram_values_ds.axis_scale(:vega) = 'log' == y_scale_type ? histogram_values_ds.min_val / 10.0 : 0 { values: h.map { |k,v| { x: k, y: v } }, width: width, x_axis_label: x_ds.name, x_axis_tick_format: x_ds.axis_tick_format, x_scale_type: 'linear', x_scale_nice: true, bar_width: (width / .to_f) - 3, y_axis_label: 'Frequency', y_axis_tick_format: "d3.format('r')", y_scale_type: y_scale_type, y_scale_domain: [, histogram_values_ds.max_val], bar_y2_val: , css_class: 'rde-histogram-quantitative', } end |
#render ⇒ Object
49 50 51 52 53 54 |
# File 'lib/rails_data_explorer/chart/histogram_quantitative.rb', line 49 def render return '' unless render? ca = compute_chart_attrs return '' unless ca render_vega(ca) end |
#render_nvd3(ca) ⇒ Object
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 |
# File 'lib/rails_data_explorer/chart/histogram_quantitative.rb', line 133 def render_nvd3(ca) %( <div class="rde-chart rde-histogram-quantitative"> <h3 class="rde-chart-title">Histogram</h3> <div id="#{ dom_id }", style="height: 200px;"> <svg></svg> </div> <script type="text/javascript"> (function() { var data = [ { values: #{ ca[:values].to_json }, key: '#{ ca[:x_axis_label] }' } ]; nv.addGraph(function() { var chart = nv.models.historicalBarChart() ; 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.tooltipContent( function(key, x, y, e, graph) { return '<p>' + key + '</p>' + '<p>' + y + ' at ' + 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
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rails_data_explorer/chart/histogram_quantitative.rb', line 56 def render_vega(ca) %( <div class="rde-chart #{ ca[:css_class] }"> <h3 class="rde-chart-title">Histogram</h3> <div id="#{ dom_id }"></div> <script type="text/javascript"> (function() { var spec = { "width": #{ ca[:width] }, "height": 200, "padding": {"top": 10, "left": 70, "bottom": 50, "right": 10}, "data": [ { "name": "table", "values": #{ ca[:values].to_json }, } ], "scales": [ { "name": "x", "type": "#{ ca[:x_scale_type] }", "range": "width", "zero": false, "nice": #{ ca[:x_scale_nice] }, "domain": {"data": "table", "field": "data.x"}, }, { "name": "y", "type": "#{ ca[:y_scale_type] }", "range": "height", "domain": #{ ca[:y_scale_domain].to_json }, } ], "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] }", "titleOffset": 60, "format": #{ ca[:y_axis_tick_format] }, } ], "marks": [ { "type": "rect", "from": {"data": "table"}, "properties": { "enter": { "x": {"scale": "x", "field": "data.x"}, "width": { "value": #{ ca[:bar_width] } }, "y": {"scale": "y", "field": "data.y"}, "y2": {"scale": "y", "value": #{ ca[:bar_y2_val] }}, }, "update": { "fill": {"value": "#1F77B4"} }, } } ] }; vg.parse.spec(spec, function(chart) { var view = chart({ el:"##{ dom_id }" }).update(); }); })(); </script> </div> ) end |