Sha256: e4f0c65f78aae56592ec3fde3358d9c453eb89b83dd5913f0c6012f5f79b833b

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'spout/models/tables/default'
require 'spout/helpers/array_statistics'

module Spout
  module Models
    module Tables
      class NumericVsChoices < Spout::Models::Tables::Default
        def title
          "#{@chart_variable.display_name} vs #{@variable.display_name}"
        end

        def headers
          [[''] + Spout::Helpers::ArrayStatistics.calculations.collect(&:first) + ['Total']]
        end

        def footers
          return [] unless @totals
          total_values = Spout::Helpers::ArrayStatistics.calculations.collect do |_calculation_label, calculation_method, calculation_type, calculation_format|
            total_count = @filtered_subjects.collect(&@variable.id.to_sym).send(calculation_method)
            { text: Spout::Helpers::TableFormatting.format_number(total_count, calculation_type, calculation_format), style: 'font-weight:bold' }
          end

          [
            [{ text: 'Total', style: 'font-weight:bold' }] + total_values + [{ text: Spout::Helpers::TableFormatting.format_number(@filtered_subjects.count, :count), style: 'font-weight:bold' }]
          ]
        end

        def rows
          filtered_domain_options(@chart_variable).collect do |option|
            row_subjects = @filtered_subjects.select { |s| s.send(@chart_variable.id) == option.value }

            row_cells = Spout::Helpers::ArrayStatistics.calculations.collect do |_calculation_label, calculation_method, calculation_type, calculation_format|
              count = row_subjects.collect(&@variable.id.to_sym).send(calculation_method)
              (count == 0 && calculation_method == :count) ? { text: '-', class: 'text-muted' } : Spout::Helpers::TableFormatting.format_number(count, calculation_type, calculation_format)
            end

            [option.display_name] + row_cells + [{ text: Spout::Helpers::TableFormatting.format_number(row_subjects.count, :count), style: 'font-weight:bold' }]
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spout-0.12.1 lib/spout/models/tables/numeric_vs_choices.rb
spout-0.12.0 lib/spout/models/tables/numeric_vs_choices.rb
spout-0.12.0.rc2 lib/spout/models/tables/numeric_vs_choices.rb
spout-0.12.0.rc lib/spout/models/tables/numeric_vs_choices.rb
spout-0.12.0.beta2 lib/spout/models/tables/numeric_vs_choices.rb
spout-0.12.0.beta1 lib/spout/models/tables/numeric_vs_choices.rb
spout-0.11.1 lib/spout/models/tables/numeric_vs_choices.rb