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

Returns a new instance of Renderer.



16
17
18
# File 'lib/apex_charts/renderer.rb', line 16

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/apex_charts/renderer.rb', line 14

def options
  @options
end

Class Method Details

.render(options) ⇒ Object



9
10
11
# File 'lib/apex_charts/renderer.rb', line 9

def render(options)
  new(options).render
end

Instance Method Details

#attributesObject



71
72
73
# File 'lib/apex_charts/renderer.rb', line 71

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

#css_classObject



87
88
89
# File 'lib/apex_charts/renderer.rb', line 87

def css_class
  attributes.delete(:class)
end

#defer(js) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/apex_charts/renderer.rb', line 35

def defer(js)
  if defer?
    "      (function() {\n        var createChart = function() {\n          \#{indent(js)}\n        };\n        if (window.addEventListener) {\n          window.addEventListener(\"load\", createChart, true);\n        } else if (window.attachEvent) {\n          window.attachEvent(\"onload\", createChart);\n        } else {\n          createChart();\n        }\n      })();\n    DEFERRED\n  else\n    js\n  end\nend\n"

#defer?Boolean

Returns:

  • (Boolean)


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

def defer?
  @defer ||= options.delete(:defer)
end

#element_idObject



75
76
77
# File 'lib/apex_charts/renderer.rb', line 75

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

#heightObject



91
92
93
# File 'lib/apex_charts/renderer.rb', line 91

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

#id_numberObject



79
80
81
# File 'lib/apex_charts/renderer.rb', line 79

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

#indent(content, times = 2) ⇒ Object



112
113
114
115
116
# File 'lib/apex_charts/renderer.rb', line 112

def indent(content, times=2)
  content.lines.map.with_index do |line, index|
    (index.zero? ? '' : '  ' * times) + line
  end.join
end

#module?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/apex_charts/renderer.rb', line 67

def module?
  @module ||= options.delete(:module)
end

#renderObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/apex_charts/renderer.rb', line 20

def render
  html = ''
  html = window_apex if id_number == '1' && !ApexCharts.config.default_options.empty?

  chart_rendering = "    var \#{variable} = new ApexCharts(document.querySelector(\"#\#{element_id}\"), \#{substitute_function_object(options.to_json)});\n    \#{variable}.render();\n  JS\n\n  html += <<~HTML\n    <div id=\"\#{element_id}\" class=\"\#{css_class}\" style=\"\#{style}\"></div>\n    \#{script(defer(chart_rendering))}\n  HTML\nend\n"

#script(js) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/apex_charts/renderer.rb', line 103

def script(js)
  type = module? ? 'module' : 'text/javascript'
  "    <script type=\"\#{type}\" apexcharts-rb=\"\#{RELEASE}\" >\n    \#{js}\n    </script>\n  SCRIPT\nend\n"

#styleObject



95
96
97
# File 'lib/apex_charts/renderer.rb', line 95

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

#substitute_function_object(json) ⇒ Object



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

def substitute_function_object(json)
  json.gsub(/{"function":{"args":"(?<args>.*?)","body":"(?<body>.*?)"}}/) do
    body = "\"#{$LAST_MATCH_INFO&.[](:body)}\"".undump
    "function(#{$LAST_MATCH_INFO&.[](:args)}){#{body}}"
  end
end

#variableObject



83
84
85
# File 'lib/apex_charts/renderer.rb', line 83

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

#window_apexObject



99
100
101
# File 'lib/apex_charts/renderer.rb', line 99

def window_apex
  script("window.Apex = #{ApexCharts.config.default_options.to_json}")
end