Class: ChartJS::Chart
- Inherits:
-
Object
- Object
- ChartJS::Chart
- Defined in:
- lib/chart_js/chart/chart.rb
Instance Method Summary collapse
- #build(&block) ⇒ Object
- #cdn(version: "2.9.3", min: true) ⇒ Object
- #chart_obj(value = nil) ⇒ Object
- #data(&block) ⇒ Object
- #event_stream(path, chart: chart_obj, &block) ⇒ Object
- #file(path, &block) ⇒ Object
-
#initialize(&block) ⇒ Chart
constructor
A new instance of Chart.
- #opts(&block) ⇒ Object
- #random_color ⇒ Object
- #random_id(force: false) ⇒ Object
- #script(config: to_json, id: random_id, chart: chart_obj) ⇒ Object
- #to_h ⇒ Object
- #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) ⇒ Object
- #to_json(type = :pretty) ⇒ Object
- #type(value = nil) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Chart
Returns a new instance of Chart.
12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Method Details
#build(&block) ⇒ Object
22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
69 70 71 72 73 74 75 |
# 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) ⇒ Object
81 82 83 84 |
# 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) ⇒ Object
59 60 61 62 |
# 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) ⇒ Object
92 93 94 95 |
# 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) ⇒ Object
120 121 122 123 |
# File 'lib/chart_js/chart/chart.rb', line 120 def file(path, &block) @file = path @file_block = block end |
#opts(&block) ⇒ Object
64 65 66 67 |
# 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 ⇒ Object
77 78 79 |
# File 'lib/chart_js/chart/chart.rb', line 77 def random_color "##{SecureRandom.hex(3)}" end |
#random_id(force: false) ⇒ Object
86 87 88 89 |
# 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) ⇒ Object
97 98 99 100 101 102 103 |
# 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 ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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) ⇒ Object
49 50 51 52 |
# 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) ⇒ Object
54 55 56 57 |
# File 'lib/chart_js/chart/chart.rb', line 54 def type(value = nil) return @type if value.nil? @type = value end |