Sha256: bd36a9d5be25f6afe24dc02bf8f8bcaf64a0c2fb8454f35f701616945750947f

Contents?: true

Size: 1.66 KB

Versions: 10

Compression:

Stored size: 1.66 KB

Contents

class ChartCandy::Builder::Donut < ChartCandy::Builder::Base

  def initialize(id, options={})
    super

    @chart.merge! hole: [], label: t('label'), nature: 'donut', slices: [], show_label: true, unit: :number, value: t('value')
  end

  def add_hole_item(name, value)
    @chart[:hole] = [t('hole.title')].flatten if hole.empty?
    hole << t("hole.#{name}", value: value)
  end

  def add_slice(name, value, options={})
    options.reverse_merge! txt_vars: {}

    return if value.to_i <= 0

    value = value.round(2) if money?
    valuef = money? ? format_money(value) : value

    options[:txt_vars][:value] = valuef

    label_str = t("slices.#{name}.label", options[:txt_vars])
    tooltip = t("slices.#{name}.tooltip", options[:txt_vars])

    @chart[:slices] << { label: label_str, percent: 0, tooltip: tooltip, value: value, valuef: valuef }
  end

  def close_chart
    total = @chart[:slices].sum{ |s| s[:value] }

    total = total.round(2) if money?

    @chart[:total] = { label: 'Total', value: total }

    fill_percents
  end

  def format_money(value)
    if value > 99
      "#{value.round} $"
    else
      sprintf("%0.02f", (value.to_f).round(0)).gsub('.', ',') + ' $'
    end
  end

  def hole
    @chart[:hole]
  end

  def money?
    @chart[:unit] == :money
  end

  def show_label=(active)
    @chart[:show_label] = active
  end

  def show_label
    @chart[:show_label]
  end

  def unit=(unit_sym)
    @chart[:unit] = unit_sym.to_sym if [:number, :money].include? unit_sym.to_sym
  end

  def unit
    @chart[:unit]
  end

  private

  def fill_percents
    @chart[:slices].each { |s| s[:percent] = (s[:value].to_f * 100 / @chart[:total][:value]).round(2) }
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
chart-candy-0.1.7 lib/chart-candy/builder/donut.rb
chart-candy-0.1.6 lib/chart-candy/builder/donut.rb
chart-candy-0.1.5 lib/chart-candy/builder/donut.rb
chart-candy-0.1.4 lib/chart-candy/builder/donut.rb
chart-candy-0.1.3 lib/chart-candy/builder/donut.rb
chart-candy-0.1.2 lib/chart-candy/builder/donut.rb
chart-candy-0.1.1 lib/chart-candy/builder/donut.rb
chart-candy-0.1.0 lib/chart-candy/builder/donut.rb
chart-candy-0.0.12 lib/chart-candy/builder/donut.rb
chart-candy-0.0.11 lib/chart-candy/builder/donut.rb