Class: ApexCharts::Renderer
- Inherits:
-
Object
- Object
- ApexCharts::Renderer
- Defined in:
- lib/apex_charts/renderer.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #css_class ⇒ Object
- #defer(js) ⇒ Object
- #defer? ⇒ Boolean
- #element_id ⇒ Object
- #height ⇒ Object
- #id_number ⇒ Object
- #indent(content, times = 2) ⇒ Object
-
#initialize(options) ⇒ Renderer
constructor
A new instance of Renderer.
- #module? ⇒ Boolean
- #render ⇒ Object
- #script(js) ⇒ Object
- #style ⇒ Object
- #substitute_function_object(json) ⇒ Object
- #variable ⇒ Object
- #window_apex ⇒ Object
Constructor Details
#initialize(options) ⇒ Renderer
Returns a new instance of Renderer.
16 17 18 |
# File 'lib/apex_charts/renderer.rb', line 16 def initialize() = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/apex_charts/renderer.rb', line 14 def end |
Class Method Details
.render(options) ⇒ Object
9 10 11 |
# File 'lib/apex_charts/renderer.rb', line 9 def render() new().render end |
Instance Method Details
#attributes ⇒ Object
71 72 73 |
# File 'lib/apex_charts/renderer.rb', line 71 def attributes @attributes ||= .delete(:div) { {} } end |
#css_class ⇒ Object
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
63 64 65 |
# File 'lib/apex_charts/renderer.rb', line 63 def defer? @defer ||= .delete(:defer) end |
#element_id ⇒ Object
75 76 77 |
# File 'lib/apex_charts/renderer.rb', line 75 def element_id @element_id ||= attributes.delete(:id) end |
#height ⇒ Object
91 92 93 |
# File 'lib/apex_charts/renderer.rb', line 91 def height "#{options[:chart][:height].to_i}px" end |
#id_number ⇒ Object
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
67 68 69 |
# File 'lib/apex_charts/renderer.rb', line 67 def module? @module ||= .delete(:module) end |
#render ⇒ Object
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..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" |
#style ⇒ Object
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 |
#variable ⇒ Object
83 84 85 |
# File 'lib/apex_charts/renderer.rb', line 83 def variable @variable ||= attributes.delete(:var) { "chart#{id_number}" } end |
#window_apex ⇒ Object
99 100 101 |
# File 'lib/apex_charts/renderer.rb', line 99 def window_apex script("window.Apex = #{ApexCharts.config.default_options.to_json}") end |