Class: ApexCharts::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/apex_charts/renderer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Renderer



34
35
36
# File 'lib/apex_charts/renderer.rb', line 34

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/apex_charts/renderer.rb', line 32

def options
  @options
end

Class Method Details

.render_default(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/apex_charts/renderer.rb', line 8

def render_default(options)
  renderer = new options

  html = if renderer.id_number == '1' && !ApexCharts.config.default_options.empty?
           renderer.window_apex
         else
           ''
         end
  html + "    <div id=\"\#{renderer.element_id}\" class=\"\#{renderer.css_class}\" style=\"\#{renderer.style}\"></div>\n    <script type=\"text/javascript\">\n      var \#{renderer.variable} = new ApexCharts(document.querySelector(\"#\#{renderer.element_id}\"), \#{substitute_function_object(renderer.options.to_json)});\n      \#{renderer.variable}.render();\n    </script>\n  HTML\nend\n"

.substitute_function_object(json) ⇒ Object



25
26
27
28
29
# File 'lib/apex_charts/renderer.rb', line 25

def substitute_function_object(json)
  json.gsub(%r[{"function":{"args":"(?<args>.*?)","body":"(?<body>.*?)"}}]) do
    "function(#{$~&.[](:args)}){#{$~&.[](:body)}}"
  end
end

Instance Method Details

#attributesObject



38
39
40
# File 'lib/apex_charts/renderer.rb', line 38

def attributes
  @attributes ||= options.delete(:div) { {} }
end

#css_classObject



54
55
56
# File 'lib/apex_charts/renderer.rb', line 54

def css_class
  attributes.delete(:class)
end

#element_idObject



42
43
44
# File 'lib/apex_charts/renderer.rb', line 42

def element_id
  @element_id ||= attributes.delete(:id)
end

#heightObject



58
59
60
# File 'lib/apex_charts/renderer.rb', line 58

def height
  "#{options[:chart][:height].to_i}px"
end

#id_numberObject



46
47
48
# File 'lib/apex_charts/renderer.rb', line 46

def id_number
  @id_number ||= element_id&.[](/\d+/)
end

#styleObject



62
63
64
# File 'lib/apex_charts/renderer.rb', line 62

def style
  "height: #{height}; #{attributes.delete(:style)}"
end

#variableObject



50
51
52
# File 'lib/apex_charts/renderer.rb', line 50

def variable
  @variable ||= attributes.delete(:var) { "chart#{id_number}" }
end

#window_apexObject



66
67
68
69
70
71
72
# File 'lib/apex_charts/renderer.rb', line 66

def window_apex
  "    <script type=\"text/javascript\">\n      window.Apex = \#{ApexCharts.config.default_options.to_json}\n    </script>\n  HTML\nend\n"