Class: ChartJS::EventStreams::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/chart_js/chart/event_streams/push.rb

Instance Method Summary collapse

Constructor Details

#initialize(chart:, dataset:, &block) ⇒ Push

Returns a new instance of Push.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/chart_js/chart/event_streams/push.rb', line 5

def initialize(chart:, dataset:, &block)
  @str = ""
  @chart = chart
  dataset(dataset)
  @str << "source.onmessage = function(e) { \n"
  @str << "json = JSON.parse(e.data);\n"
  base(json: 'data', dataset: dataset, chart: chart)
  @str << "if(json.label){ #{chart}.data.labels.push(json.label); }\n"
  @str << "#{chart}.update();\n"
  build(&block) if block_given?
  @str << "\n};"
end

Instance Method Details

#base(json:, chart:, dataset:, raw: false) ⇒ Object



70
71
72
# File 'lib/chart_js/chart/event_streams/push.rb', line 70

def base(json:, chart:, dataset:, raw: false)
  @str << "if(json.#{json}){ #{chart}.data.datasets[#{dataset}].#{json}.push(json.#{json}); }\n"
end

#build(&block) ⇒ Object



18
19
20
21
# File 'lib/chart_js/chart/event_streams/push.rb', line 18

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

#color(type, chart: @chart, dataset: @dataset) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chart_js/chart/event_streams/push.rb', line 50

def color(type, chart: @chart, dataset: @dataset)
  case type
  when :background
    base(json: 'backgroundColor',           dataset: dataset, chart: chart)
  when :border
    base(json: 'borderColor',               dataset: dataset, chart: chart)
  when :point
    base(json: 'pointBackgroundColor',      dataset: dataset, chart: chart)
    base(json: 'pointBorderColor',          dataset: dataset, chart: chart)
    base(json: 'pointHoverBackgroundColor', dataset: dataset, chart: chart)
    base(json: 'pointHoverBorderColor',     dataset: dataset, chart: chart)
    base(json: 'pointHoverBorderColor',     dataset: dataset, chart: chart)
    base(json: 'pointHoverBorderColor',     dataset: dataset, chart: chart)
  else
    color :background,  chart: chart, dataset: dataset
    color :border,      chart: chart, dataset: dataset
    color :point,       chart: chart, dataset: dataset
  end
end

#dataset(value = nil) ⇒ Object



23
24
25
26
# File 'lib/chart_js/chart/event_streams/push.rb', line 23

def dataset(value = nil)
  return @dataset || 0 if value.nil?
  @dataset = value 
end

#point(type, chart: @chart, dataset: @dataset) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chart_js/chart/event_streams/push.rb', line 28

def point(type, chart: @chart, dataset: @dataset)
  case type
  when :hit_radius
    base(json: 'pointHitRadius',        dataset: dataset, chart: chart)
  when :style
    base(json: 'point_style',           dataset: dataset, chart: chart)
  when :border
    color :border,        chart: chart, dataset: dataset
    base(json: 'pointBorder',           dataset: dataset, chart: chart)
    base(json: 'pointBorderWidth',      dataset: dataset, chart: chart)
  when :radius
    base(json: 'pointRadius',           dataset: dataset, chart: chart)
  when :hover_radius
    base(json: 'pointHoverRadius',      dataset: dataset, chart: chart)
  else
    point :hit_radius,    chart: chart, dataset: dataset
    point :style,         chart: chart, dataset: dataset
    point :border,        chart: chart, dataset: dataset
    point :hover_radius,  chart: chart, dataset: dataset
  end
end

#to_sObject



74
75
76
# File 'lib/chart_js/chart/event_streams/push.rb', line 74

def to_s
  @str
end