Class: ChartJS::RadarChart

Inherits:
Object
  • Object
show all
Defined in:
lib/chart_js/chart/radar_chart/radar_chart.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ RadarChart

Returns a new instance of RadarChart.



11
12
13
14
15
16
17
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 11

def initialize(&block)
  @type = "radar"
  @data = nil
  @opts = nil
  @file = false
  build(&block)
end

Instance Method Details

#build(&block) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/chart_js/chart/radar_chart/radar_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) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/chart_js/chart/radar_chart/radar_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) ⇒ Object



37
38
39
40
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 37

def data(&block)
  return @data unless block_given?
  @data = Data.new.build(&block)
end

#file(path, &block) ⇒ Object



87
88
89
90
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 87

def file(path, &block)
  @file = path
  @file_block = block
end

#opts(&block) ⇒ Object



42
43
44
45
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 42

def opts(&block)
  return @opts unless block_given?
  @opts = Opts.new.build(&block)
end

#random_colorObject



55
56
57
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 55

def random_color
  "##{SecureRandom.hex(3)}"
end

#random_idObject



59
60
61
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 59

def random_id
  SecureRandom.uuid
end

#script(config: to_json, id: random_id) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 63

def script(config: to_json, id: random_id)
  "<script>
      var config = #{config}
      window.onload = function() {
        var ctx = document.getElementById(\"#{id}\").getContext(\"2d\");
        new Chart(ctx, config);
      };
  </script>"
end

#to_hObject



28
29
30
# File 'lib/chart_js/chart/radar_chart/radar_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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 73

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 << "<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) if script
  str.join("\n")
end

#to_json(type = :pretty) ⇒ Object



32
33
34
35
# File 'lib/chart_js/chart/radar_chart/radar_chart.rb', line 32

def to_json(type = :pretty)
  return JSON.pretty_generate(to_h) if type == :pretty
  to_h.to_json
end