Class: ChartJS::Data

Inherits:
Object
  • Object
show all
Includes:
Helpers::Dates
Defined in:
lib/chart_js/chart/data.rb,
lib/chart_js/chart/bar_chart/data.rb,
lib/chart_js/chart/line_chart/data.rb,
lib/chart_js/chart/radar_chart/data.rb

Constant Summary

Constants included from Helpers::Dates

Helpers::Dates::MONDAY_TO_SUNDAY, Helpers::Dates::WORK_DAYS

Instance Method Summary collapse

Methods included from Helpers::Dates

#days_of_the_week, #monday_to_sunday, #months_of_the_year, #work_days

Constructor Details

#initializeData

Returns a new instance of Data.



10
11
12
13
# File 'lib/chart_js/chart/data.rb', line 10

def initialize
  @container = Hash.new
  @datasets  = Hash.new
end

Instance Method Details

#build(&block) ⇒ Object



15
16
17
18
# File 'lib/chart_js/chart/data.rb', line 15

def build(&block)
  instance_eval(&block)
  self
end

#dataset(label, &block) ⇒ Object



25
26
27
# File 'lib/chart_js/chart/data.rb', line 25

def dataset(label, &block)
  @datasets[label] = Dataset.new(label, &block)
end

#labels(labels = nil) ⇒ Object



20
21
22
23
# File 'lib/chart_js/chart/data.rb', line 20

def labels(labels = nil)
  raise "Not an array!" unless labels.is_a? Array
  @container['labels'] = labels
end

#to_hObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chart_js/chart/data.rb', line 29

def to_h(type)
  case type
  when :pie
    @container  
    @container['labels'] = Array.new
    @container['datasets'] = Array.new
    @container['datasets'] << Hash.new
    @datasets.each do |label, data|
      @container['labels'] << label
      data.to_h.each do |key, value|
        if @container['datasets'][0][key].is_a? Array
          @container['datasets'][0][key] << value
        else
          @container['datasets'][0][key] = Array.new
          @container['datasets'][0][key] << value
        end
      end
    end
    @container  
  else
    cont = @container.dup
    cont['datasets'] = []
    @datasets.each do |_, data|
      cont['datasets'] << data.to_h
    end
    cont

  end
end