Class: ChartJS::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/chart_js/chart/dataset.rb,
lib/chart_js/chart/bar_chart/dataset.rb,
lib/chart_js/chart/line_chart/dataset.rb,
lib/chart_js/chart/radar_chart/dataset.rb

Instance Method Summary collapse

Constructor Details

#initialize(label, &block) ⇒ Dataset

Returns a new instance of Dataset.



10
11
12
13
14
15
# File 'lib/chart_js/chart/dataset.rb', line 10

def initialize(label, &block)
  @container = Hash.new
  #fill(false)
  label(label)
  build(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/chart_js/chart/dataset.rb', line 22

def method_missing(m, *args, &block)
  m = m.to_s
  if m.to_s.include?('_')
    parts = m.split('_').collect(&:capitalize)
    parts[0] = parts[0].downcase
    m = parts.join
  end
  @container[m] = args.first
end

Instance Method Details

#axis_id(value, axis) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/chart_js/chart/dataset.rb', line 44

def axis_id(value, axis)
  case axis
  when :x
    @container['xAxisID'] = value
  when :y
    @container['yAxisID'] = value
  end
end

#background(&block) ⇒ Object



75
76
77
# File 'lib/chart_js/chart/dataset.rb', line 75

def background(&block)
  @container = Background.new(@container).build(&block)
end

#border(&block) ⇒ Object



79
80
81
# File 'lib/chart_js/chart/dataset.rb', line 79

def border(&block)
  @container = Border.new(@container).build(&block)
end

#build(&block) ⇒ Object



17
18
19
20
# File 'lib/chart_js/chart/dataset.rb', line 17

def build(&block)
  instance_eval(&block)
  @container
end

#color(value = :random, type = :both) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chart_js/chart/dataset.rb', line 53

def color(value = :random, type = :both)
  if value == :random
    c = "##{SecureRandom.hex(3)}"
    color c, :border     if type == :both || type == :border
    color c, :background if type == :both || type == :background
    return
  end
  case type
  when :border
    @container['borderColor'] = value
  when :background
    @container['backgroundColor'] = value
  when :both
    color value, :border
    color value, :background
  end
end

#data(value) ⇒ Object



32
33
34
# File 'lib/chart_js/chart/dataset.rb', line 32

def data(value)
  @container['data'] = value
end

#fill(value = true) ⇒ Object



40
41
42
# File 'lib/chart_js/chart/dataset.rb', line 40

def fill(value = true)
  @container['fill'] = value
end

#label(value) ⇒ Object



36
37
38
# File 'lib/chart_js/chart/dataset.rb', line 36

def label(value)
  @container['label'] = value
end

#line(&block) ⇒ Object



87
88
89
# File 'lib/chart_js/chart/dataset.rb', line 87

def line(&block)
  @container = Line.new(@container).build(&block)
end

#point(&block) ⇒ Object



83
84
85
# File 'lib/chart_js/chart/dataset.rb', line 83

def point(&block)
  @container = Point.new(@container).build(&block)
end

#span_gaps(value = true) ⇒ Object



71
72
73
# File 'lib/chart_js/chart/dataset.rb', line 71

def span_gaps(value = true)
  @container['spanGaps'] = value
end

#to_hObject



91
92
93
# File 'lib/chart_js/chart/dataset.rb', line 91

def to_h
  @container
end