Class: ChartCandy::Builder::Donut

Inherits:
Base
  • Object
show all
Defined in:
lib/chart-candy/builder/donut.rb

Instance Method Summary collapse

Methods inherited from Base

#filename, #id, #l, #period, #set_period_from_data, #t, #title, #to_json, #to_xls

Constructor Details

#initialize(id, options = {}) ⇒ Donut

Returns a new instance of Donut.



3
4
5
6
7
# 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

Instance Method Details

#add_hole_item(name, value) ⇒ Object



9
10
11
12
# 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 = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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_chartObject



30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
43
44
45
46
# 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

#holeObject



48
49
50
# File 'lib/chart-candy/builder/donut.rb', line 48

def hole
  @chart[:hole]
end

#money?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/chart-candy/builder/donut.rb', line 52

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

#show_labelObject



60
61
62
# File 'lib/chart-candy/builder/donut.rb', line 60

def show_label
  @chart[:show_label]
end

#show_label=(active) ⇒ Object



56
57
58
# File 'lib/chart-candy/builder/donut.rb', line 56

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

#unitObject



68
69
70
# File 'lib/chart-candy/builder/donut.rb', line 68

def unit
  @chart[:unit]
end

#unit=(unit_sym) ⇒ Object



64
65
66
# 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