module Chartjs::ChartHelper

Constants

CHART_TYPES

Private Instance Methods

chart_id() click to toggle source
# File lib/chartjs/chart_helper.rb, line 105
def chart_id
  @chart_id ||= 0
  @chart_id += 1
end
chart_styling(width, height) click to toggle source
# File lib/chartjs/chart_helper.rb, line 80
    def chart_styling(width, height)
      width = ERB::Util.html_escape(width)
      height = ERB::Util.html_escape(height)

      <<-CSS.squish
        style="
          #{'width: ' + width + ';'}
          #{'height: ' + height + ';'}
          text-align: center;
          color: #999;
          font-size: 14px;
          font-family: Verdana;
          #{'line-height: ' + height + ';'}
        "
      CSS
    end
chart_template(id, klass, width, height, data) click to toggle source
# File lib/chartjs/chart_helper.rb, line 72
    def chart_template(id, klass, width, height, data)
      <<-DIV.squish
        <div id="#{ERB::Util.html_escape(id)}" class="#{ERB::Util.html_escape(klass)}" #{data_attributes(data)} #{chart_styling(width, height)}>
          Loading...
        </div>
      DIV
    end
configure_html_options(html_options) click to toggle source
# File lib/chartjs/chart_helper.rb, line 65
def configure_html_options(html_options)
  html_options[:id]     ||= "chart-#{chart_id}"
  html_options[:width]  ||= '100%'
  html_options[:height] ||= '300px'
  html_options
end
data_attributes(data) click to toggle source
# File lib/chartjs/chart_helper.rb, line 97
def data_attributes(data)
  return unless data

  data.map do |key, value|
    "data-#{ERB::Util.html_escape(key)}='#{ERB::Util.html_escape(value)}'"
  end.join(' ')
end
draw_chart(type, url, dataset_properties: {}, options: {}, **html_options) click to toggle source
# File lib/chartjs/chart_helper.rb, line 21
    def draw_chart(type, url, dataset_properties: {}, options: {}, **html_options)
      html_options = configure_html_options(html_options)

      template = chart_template(
        html_options[:id],
        html_options[:class],
        html_options[:width],
        html_options[:height],
        html_options[:data]
      )

      script = <<-JS.squish
        <script type="text/javascript">
          (function() {
            var initChart = function() {
              new SimpleChart(
                '#{type}',
                '#{html_options[:id]}',
                '#{url}',
                '#{html_options[:width]}',
                '#{html_options[:height]}',
                '#{dataset_properties.to_json}',
                '#{options.to_json}'
              ).createChart();
            };

            if (document.addEventListener) {
              if (typeof(Turbolinks) != 'undefined' && Turbolinks.supported) {
                document.addEventListener("turbolinks:load", initChart, true);
                document.addEventListener("turbolinks:before-cache", function() {
                  document.removeEventListener("turbolinks:load", initChart, true);
                });
              } else {
                document.addEventListener("load", initChart, true);
              }
            }
          })();
        </script>
      JS

      html = template + script
      html.respond_to?(:html_safe) ? html.html_safe : html
    end