# -*- coding: utf-8 -*- class RailsDataExplorer class Chart # Responsibilities: # * Render a pie chart for univariate analysis of a categorical data series. # # Collaborators: # * DataSet # class PieChart < 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? 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 def render return '' unless render? ca = compute_chart_attrs return '' unless ca render_vega(ca) end def render_vega(ca) %(