class ApexCharts::Renderer

Attributes

options[R]

Public Class Methods

new(options) click to toggle source
# File lib/apex_charts/renderer.rb, line 16
def initialize(options)
  @options = options
end
render(options) click to toggle source
# File lib/apex_charts/renderer.rb, line 9
def render(options)
  new(options).render
end

Public Instance Methods

attributes() click to toggle source
# File lib/apex_charts/renderer.rb, line 67
def attributes
  @attributes ||= options.delete(:div) { {} }
end
css_class() click to toggle source
# File lib/apex_charts/renderer.rb, line 83
def css_class
  attributes.delete(:class)
end
defer(js) click to toggle source
# File lib/apex_charts/renderer.rb, line 35
    def defer(js)
      if defer?
        <<~DEFERRED
          (function() {
            var createChart = function() {
              #{indent(js)}
            };
            if (window.addEventListener) {
              window.addEventListener("load", createChart, true);
            } else if (window.attachEvent) {
              window.attachEvent("onload", createChart);
            } else {
              createChart();
            }
          })();
        DEFERRED
      else
        js
      end
    end
defer?() click to toggle source
# File lib/apex_charts/renderer.rb, line 63
def defer?
  @defer ||= options.delete(:defer)
end
element_id() click to toggle source
# File lib/apex_charts/renderer.rb, line 71
def element_id
  @element_id ||= attributes.delete(:id)
end
height() click to toggle source
# File lib/apex_charts/renderer.rb, line 87
def height
  "#{options[:chart][:height].to_i}px"
end
id_number() click to toggle source
# File lib/apex_charts/renderer.rb, line 75
def id_number
  @id_number ||= element_id&.[](/\d+/)
end
indent(content, times=2) click to toggle source
# File lib/apex_charts/renderer.rb, line 107
def indent(content, times=2)
  content.lines.map.with_index do |line, index|
    (index == 0 ? '' : '  ' * times) + line
  end.join
end
render() click to toggle source
# 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 = <<~JS
        var #{variable} = new ApexCharts(document.querySelector("##{element_id}"), #{substitute_function_object(options.to_json)});
        #{variable}.render();
      JS

      html += <<~HTML
        <div id="#{element_id}" class="#{css_class}" style="#{style}"></div>
        #{script(defer(chart_rendering))}
      HTML
    end
script(js) click to toggle source
# File lib/apex_charts/renderer.rb, line 99
    def script(js)
      <<~SCRIPT
        <script type="text/javascript" apexcharts-rb="#{RELEASE}">
        #{js}
        </script>
      SCRIPT
    end
style() click to toggle source
# File lib/apex_charts/renderer.rb, line 91
def style
  "height: #{height}; #{attributes.delete(:style)}"
end
substitute_function_object(json) click to toggle source
# File lib/apex_charts/renderer.rb, line 56
def substitute_function_object(json)
  json.gsub(%r[{"function":{"args":"(?<args>.*?)","body":"(?<body>.*?)"}}]) do
    body = "\"#{$~&.[](:body)}\"".undump
    "function(#{$~&.[](:args)}){#{body}}"
  end
end
variable() click to toggle source
# File lib/apex_charts/renderer.rb, line 79
def variable
  @variable ||= attributes.delete(:var) { "chart#{id_number}" }
end
window_apex() click to toggle source
# File lib/apex_charts/renderer.rb, line 95
def window_apex
  script("window.Apex = #{ApexCharts.config.default_options.to_json}")
end