Class: RailsDataExplorer::Chart::HistogramCategorical
- Inherits:
- 
      RailsDataExplorer::Chart
      
        - Object
- RailsDataExplorer::Chart
- RailsDataExplorer::Chart::HistogramCategorical
 
- Defined in:
- lib/rails-data-explorer/chart/histogram_categorical.rb
Instance Attribute Summary
Attributes inherited from RailsDataExplorer::Chart
Instance Method Summary collapse
- #compute_chart_attrs ⇒ Object
- 
  
    
      #initialize(_data_set, options = {})  ⇒ HistogramCategorical 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of HistogramCategorical. 
- #render ⇒ Object
- 
  
    
      #render?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Render HistogramCategorical only if there is a fairly small number of distinct values. 
- #render_nvd3(ca) ⇒ Object
- #render_vega(ca) ⇒ Object
Methods inherited from RailsDataExplorer::Chart
Constructor Details
#initialize(_data_set, options = {}) ⇒ HistogramCategorical
Returns a new instance of HistogramCategorical.
| 5 6 7 8 | # File 'lib/rails-data-explorer/chart/histogram_categorical.rb', line 5 def initialize(_data_set, = {}) @data_set = _data_set @options = {}.merge() end | 
Instance Method Details
#compute_chart_attrs ⇒ Object
| 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # File 'lib/rails-data-explorer/chart/histogram_categorical.rb', line 10 def compute_chart_attrs x_ds = @data_set.data_series.first return false if x_ds.nil? # compute histogram h = x_ds.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 } }.sort( &x_ds.label_sorter( :x, lambda { |a,b| b[:y] <=> a[:y] } ) ), x_axis_label: x_ds.name, x_axis_tick_format: "d3.format('r')", y_axis_label: 'Frequency', y_axis_tick_format: "d3.format(',g')", #histogram_values_ds.axis_tick_format, y_scale_type: y_scale_type, y_scale_domain: [, histogram_values_ds.max_val], bar_y2_val: , } end | 
#render ⇒ Object
| 38 39 40 41 42 43 | # File 'lib/rails-data-explorer/chart/histogram_categorical.rb', line 38 def render return '' unless render? ca = compute_chart_attrs return '' unless ca render_vega(ca) end | 
#render? ⇒ Boolean
Render HistogramCategorical only if there is a fairly small number of distinct values.
| 174 175 176 | # File 'lib/rails-data-explorer/chart/histogram_categorical.rb', line 174 def render? !@data_set.data_series.first.has_many_uniq_vals? end | 
#render_nvd3(ca) ⇒ Object
| 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 | # File 'lib/rails-data-explorer/chart/histogram_categorical.rb', line 120 def render_nvd3(ca) %( <div class="rde-chart rde-histogram-categorical"> <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.discreteBarChart() ; 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>' + x + '</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
| 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 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 | # File 'lib/rails-data-explorer/chart/histogram_categorical.rb', line 45 def render_vega(ca) %( <div class="rde-chart rde-histogram-categorical"> <h3 class="rde-chart-title">Histogram</h3> <div id="#{ dom_id }"></div> <script type="text/javascript"> (function() { var spec = { "width": 800, "height": 200, "padding": {"top": 10, "left": 70, "bottom": 50, "right": 10}, "data": [ { "name": "table", "values": #{ ca[:values].to_json } } ], "scales": [ { "name": "x", "type": "ordinal", "range": "width", "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": {"scale": "x", "band": true, "offset": -1}, "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 |