class RailsDataExplorer class Chart class HistogramCategorical < Chart def initialize(_data_set, options = {}) @data_set = _data_set @options = {}.merge(options) end 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 } { 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('r')", } end def render return '' unless render? ca = compute_chart_attrs return '' unless ca render_vega(ca) end def render_vega(ca) %(