class ChartJS::Chart

Public Class Methods

new(&block) click to toggle source
# File lib/chart_js/chart/chart.rb, line 12
def initialize(&block)
  @type      = nil
  @data      = nil
  @opts      = nil
  @file      = false
  @stream    = false
  @chart_obj = 'chart' + SecureRandom.uuid.gsub("-","")
  build(&block) if block_given?
end

Public Instance Methods

build(&block) click to toggle source
# File lib/chart_js/chart/chart.rb, line 22
def build(&block)
  instance_eval(&block)
  if @file
    f = SaveFile.new(@file, html: to_html)
    f.save!
  end
  self
end
cdn(version: "2.9.3", min: true) click to toggle source
# File lib/chart_js/chart/chart.rb, line 69
def cdn(version: "2.9.3", min: true)
  if min
    "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.min.js\"></script>"
  else
    "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/#{version}/Chart.js\"></script>"
  end
end
chart_obj(value = nil) click to toggle source
# File lib/chart_js/chart/chart.rb, line 81
def chart_obj(value = nil)
  return @chart_obj if value.nil?
  @chart_obj = value
end
data(&block) click to toggle source
# File lib/chart_js/chart/chart.rb, line 59
def data(&block)
  return @data unless block_given?
  @data = Data.new.build(&block)
end
event_stream(path, chart: chart_obj, &block) click to toggle source
# File lib/chart_js/chart/chart.rb, line 92
def event_stream(path, chart: chart_obj, &block)
  @stream = EventStream.new(path, chart)
  @stream.build(&block) if block_given?
end
file(path, &block) click to toggle source
# File lib/chart_js/chart/chart.rb, line 120
def file(path, &block)
  @file = path
  @file_block = block
end
opts(&block) click to toggle source
# File lib/chart_js/chart/chart.rb, line 64
def opts(&block)
  return @opts unless block_given?
  @opts = Opts.new.build(&block)
end
random_color() click to toggle source
# File lib/chart_js/chart/chart.rb, line 77
def random_color
  "##{SecureRandom.hex(3)}"
end
random_id(force: false) click to toggle source
# File lib/chart_js/chart/chart.rb, line 86
def random_id(force: false)
  return @id unless @id.nil? or force
  @id = SecureRandom.uuid
end
script(config: to_json, id: random_id, chart: chart_obj) click to toggle source
# File lib/chart_js/chart/chart.rb, line 97
def script(config: to_json, id: random_id, chart: chart_obj)
  "<script>
      var config = #{config}
      var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
      var #{chart} = new Chart(ctx, config);
  </script>"
end
to_h() click to toggle source
# File lib/chart_js/chart/chart.rb, line 31
def to_h
  case type
  when 'pie'
    { type: @type, data: @data.to_h(:pie), options: @opts.to_h }
  when 'doughnut'
    { type: @type, data: @data.to_h(:pie), options: @opts.to_h }
  when 'line'
    data = @data.to_h(false)
    data['datasets'].each do |set|
      set['fill'] = false unless set['fill']
    end
    #data['fill'] = false unless data['fill']
    { type: @type, data: data, options: @opts.to_h }
  else
    { type: @type, data: @data.to_h(false), options: @opts.to_h }
  end
end
to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true, chart: chart_obj, stream: true) click to toggle source
# File lib/chart_js/chart/chart.rb, line 105
def to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true, chart: chart_obj, stream: true)
  str = []
  if cdn
    str << "<head>#{cdn(version: version)}</head>"
  end
  str << "<body>" if body
  str << "<div id=\"canvas-holder\" style=\"width:#{width} heigth:#{heigth}\">"
  str << "<canvas id=\"#{id}\"/>"
  str << "</div>"
  str << "</body>" if body
  str << script(id: id, chart: chart) if script
  str << @stream.to_html if @stream && stream
  str.join("\n")
end
to_json(type = :pretty) click to toggle source
# File lib/chart_js/chart/chart.rb, line 49
def to_json(type = :pretty)
  return JSON.pretty_generate(to_h) if type == :pretty
  to_h.to_json
end
type(value = nil) click to toggle source
# File lib/chart_js/chart/chart.rb, line 54
def type(value = nil)
  return @type if value.nil?
  @type = value
end