require "json" require "securerandom" require_relative "data.rb" require_relative "opts.rb" require_relative "file.rb" module ChartJS class LineChart def initialize(&block) @type = "line" @data = nil @opts = nil @file = false build(&block) end def build(&block) instance_eval(&block) if @file f = SaveFile.new(@file, html: to_html) f.save! end self end def to_h { type: @type, data: @data.to_h, options: @opts.to_h } end def to_json(type = :pretty) return JSON.pretty_generate(to_h) if type == :pretty to_h.to_json end def data(&block) return @data unless block_given? @data = Data.new.build(&block) end def opts(&block) return @opts unless block_given? @opts = Opts.new.build(&block) end def cdn(version: "2.6.0", min: true) if min "" else "" end end def random_color "##{SecureRandom.hex(3)}" end def random_id SecureRandom.uuid end def script(config: to_json, id: random_id) "" end def to_html(width: "60%", heigth: width, head: true, cdn: head, version: "2.6.0", body: true, id: random_id, script: true) str = [] if cdn str << "#{cdn(version: version)}" end str << "" if body str << "
" str << "" str << "
" str << "" if body str << script(id: id) if script str.join("\n") end def file(path, &block) @file = path @file_block = block end end end