class ChartJS::EventStreams::Push

Public Class Methods

new(chart:, dataset:, &block) click to toggle source
# 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

Public Instance Methods

base(json:, chart:, dataset:, raw: false) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source
# File lib/chart_js/chart/event_streams/push.rb, line 74
def to_s
  @str
end