class ChartJS::LineChart

Public Class Methods

new(&block) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 11
def initialize(&block)
  @type = "line"
  @data = nil
  @opts = nil
  @file = false
  build(&block)
end

Public Instance Methods

build(&block) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 19
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/line_chart/line_chart.rb, line 47
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
data(&block) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 37
def data(&block)
  return @data unless block_given?
  @data = Data.new.build(&block)
end
file(path, &block) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 90
def file(path, &block)
  @file = path
  @file_block = block
end
opts(&block) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 42
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/line_chart/line_chart.rb, line 55
def random_color
  "##{SecureRandom.hex(3)}"
end
random_id() click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 59
def random_id
  SecureRandom.uuid
end
script(config: to_json, id: random_id, chart_name: id) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 63
def script(config: to_json, id: random_id, chart_name: id)
  "<script>
      var config = #{config};
      var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
      var #{chart_name.gsub("-","_")} = new Chart(ctx, config);
      // var #{chart_name.gsub("-","_")};
      // window.onload = function() {
      //  var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
      //  var #{chart_name.gsub("-","_")} = new Chart(ctx, config);
      //};
  </script>"
end
to_h() click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 28
def to_h
  { type: @type, data: @data.to_h, options: @opts.to_h }
end
to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true, chart_name: "line_chart") click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 76
def to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true, chart_name: "line_chart")
  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_name: chart_name) if script
  str.join("\n")
end
to_json(type = :pretty) click to toggle source
# File lib/chart_js/chart/line_chart/line_chart.rb, line 32
def to_json(type = :pretty)
  return JSON.pretty_generate(to_h) if type == :pretty
  to_h.to_json
end