class ChartCandy::Builder::Donut

Public Class Methods

new(id, options={}) click to toggle source
Calls superclass method ChartCandy::Builder::Base::new
# File lib/chart-candy/builder/donut.rb, line 3
def initialize(id, options={})
  super

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

Public Instance Methods

add_hole_item(name, value) click to toggle source
# File lib/chart-candy/builder/donut.rb, line 9
def add_hole_item(name, value)
  @chart[:hole] = [t('hole.title')].flatten if hole.empty?
  hole << t("hole.#{name}", value: value)
end
add_slice(name, value, options={}) click to toggle source
# File lib/chart-candy/builder/donut.rb, line 14
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
close_chart() click to toggle source
# File lib/chart-candy/builder/donut.rb, line 30
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
format_money(value) click to toggle source
# File lib/chart-candy/builder/donut.rb, line 40
def format_money(value)
  if value > 99
    "#{value.round} $"
  else
    sprintf("%0.02f", (value.to_f).round(0)).gsub('.', ',') + ' $'
  end
end
hole() click to toggle source
# File lib/chart-candy/builder/donut.rb, line 48
def hole
  @chart[:hole]
end
money?() click to toggle source
# File lib/chart-candy/builder/donut.rb, line 52
def money?
  @chart[:unit] == :money
end
show_label() click to toggle source
# File lib/chart-candy/builder/donut.rb, line 60
def show_label
  @chart[:show_label]
end
show_label=(active) click to toggle source
# File lib/chart-candy/builder/donut.rb, line 56
def show_label=(active)
  @chart[:show_label] = active
end
unit() click to toggle source
# File lib/chart-candy/builder/donut.rb, line 68
def unit
  @chart[:unit]
end
unit=(unit_sym) click to toggle source
# File lib/chart-candy/builder/donut.rb, line 64
def unit=(unit_sym)
  @chart[:unit] = unit_sym.to_sym if [:number, :money].include? unit_sym.to_sym
end

Private Instance Methods

fill_percents() click to toggle source
# File lib/chart-candy/builder/donut.rb, line 74
def fill_percents
  @chart[:slices].each { |s| s[:percent] = (s[:value].to_f * 100 / @chart[:total][:value]).round(2) }
end