Class: ChartCandy::Builder::Base

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

Direct Known Subclasses

Counter, Donut, Line

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/chart-candy/builder/base.rb', line 2

def initialize(id, options={})
  options.reverse_merge! from: nil, to: nil, step: nil

  @from = options[:from] ? Time.parse(options[:from]) : nil
  @to = options[:to] ? Time.parse(options[:to]) : Time.now

  @chart = { id: id }
  @chart[:step] = options[:step] if options[:step]
  @chart[:title] = t('title')
  @chart[:period] = ChartCandy::Builder.period(@from, @to, step: @chart[:step]) if @from
end

Instance Method Details

#close_chartObject



14
15
16
# File 'lib/chart-candy/builder/base.rb', line 14

def close_chart
  # Hooks before closing a chart
end

#filenameObject



18
19
20
21
22
23
24
# File 'lib/chart-candy/builder/base.rb', line 18

def filename
  name = [title.parameterize]
  name << @from.strftime('%Y%m%d') if @from
  name << @to.strftime('%Y%m%d') if @to

  return name.compact.join('-')
end

#idObject



26
27
28
# File 'lib/chart-candy/builder/base.rb', line 26

def id
  @chart[:id]
end

#l(date, options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/chart-candy/builder/base.rb', line 30

def l(date, options={})
  options.reverse_merge!(format: :date_long)

  return ChartCandy.localize(date, options)
end

#periodObject



36
37
38
# File 'lib/chart-candy/builder/base.rb', line 36

def period
  @chart[:period]
end

#set_period_from_data(data) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/chart-candy/builder/base.rb', line 40

def set_period_from_data(data)
  @from = data.first
  @to = data.last

  @chart[:step] = ChartCandy::Builder.get_step_from_interval(data[1] - data[0]) if not @chart[:step]

  @chart[:period] = ChartCandy::Builder.period @from, @to, step: @chart[:step]
end

#t(path, vars = {}) ⇒ Object



49
50
51
52
53
# File 'lib/chart-candy/builder/base.rb', line 49

def t(path, vars={})
  vars.reverse_merge! :default => ''

  ChartCandy.translate("#{id.gsub('-', '_')}.#{path}", vars)
end

#titleObject



55
56
57
# File 'lib/chart-candy/builder/base.rb', line 55

def title
  @chart[:title]
end

#to_jsonObject



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

def to_json
  close_chart

  return @chart.to_json
end

#to_xlsObject



65
66
67
68
69
# File 'lib/chart-candy/builder/base.rb', line 65

def to_xls
  close_chart

  return ChartCandy::Builder::XlsBuilder.chart_to_xls @chart
end