# -*- coding: utf-8 -*- class RailsDataExplorer class Chart # Responsibilities: # * Render a histogram for univariate analysis of a categorical data series. # # Collaborators: # * DataSet # 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 val_mod = { name: :limit_distinct_values } h = x_ds.values(val_mod).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) bar_y2_val = '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: [bar_y2_val, histogram_values_ds.max_val], bar_y2_val: bar_y2_val, } end def render return '' unless render? ca = compute_chart_attrs return '' unless ca render_vega(ca) end def render_vega(ca) %(

Histogram

) end def render_nvd3(ca) %(

Histogram

) end end end end