Class: RailsCharts::BaseChart

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_charts/base_chart.rb

Constant Summary collapse

CHART_JS_PATTERN =
/"RAILS_CHART_JS:((?!RAILS_CHART_JS:).*?):RAILS_CHART_JS_END"/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ BaseChart

Returns a new instance of BaseChart.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rails_charts/base_chart.rb', line 12

def initialize(data, options = {})
  @data          = data
  @options       = options
  @other_options = options.delete(:options).presence || {}
  @defaults      = RailsCharts.defaults[self.class].presence || {}

  @chart_id      = "rails_charts_#{Digest::SHA1.hexdigest([Time.now, rand].join)}"
  @container_id  = options.delete(:id).presence || @chart_id

  @width         = options.delete(:width).presence || RailsCharts.options[:width]
  @height        = options.delete(:height).presence || RailsCharts.options[:height]
  @theme         = options.delete(:theme).presence || RailsCharts.options[:theme]
  @locale        = options.delete(:locale).presence || RailsCharts.options[:locale]
  @renderer      = options.delete(:renderer).presence || RailsCharts.options[:renderer] || "canvas"
  @klass         = options.delete(:class).presence || RailsCharts.options[:class]
  @style         = options.delete(:style).presence || RailsCharts.options[:style]

  @debug         = options.delete(:debug)

  @vertical      = options.delete(:vertical).presence
end

Instance Attribute Details

#chart_idObject (readonly)

Returns the value of attribute chart_id.



7
8
9
# File 'lib/rails_charts/base_chart.rb', line 7

def chart_id
  @chart_id
end

#container_idObject (readonly)

Returns the value of attribute container_id.



7
8
9
# File 'lib/rails_charts/base_chart.rb', line 7

def container_id
  @container_id
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/rails_charts/base_chart.rb', line 7

def data
  @data
end

#debugObject (readonly)

Returns the value of attribute debug.



9
10
11
# File 'lib/rails_charts/base_chart.rb', line 9

def debug
  @debug
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



7
8
9
# File 'lib/rails_charts/base_chart.rb', line 7

def defaults
  @defaults
end

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def height
  @height
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def klass
  @klass
end

#localeObject (readonly)

Returns the value of attribute locale.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def locale
  @locale
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rails_charts/base_chart.rb', line 7

def options
  @options
end

#other_optionsObject (readonly)

Returns the value of attribute other_options.



9
10
11
# File 'lib/rails_charts/base_chart.rb', line 9

def other_options
  @other_options
end

#rendererObject (readonly)

Returns the value of attribute renderer.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def renderer
  @renderer
end

#styleObject (readonly)

Returns the value of attribute style.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def style
  @style
end

#themeObject (readonly)

Returns the value of attribute theme.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def theme
  @theme
end

#verticalObject (readonly)

Returns the value of attribute vertical.



10
11
12
# File 'lib/rails_charts/base_chart.rb', line 10

def vertical
  @vertical
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/rails_charts/base_chart.rb', line 8

def width
  @width
end

Instance Method Details

#axisesObject



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rails_charts/base_chart.rb', line 102

def axises
  if self.vertical
    {
      xAxis: y_axis,
      yAxis: x_axis,
    }
  else
    {
      xAxis: x_axis,
      yAxis: y_axis,
    }
  end
end

#build_optionsObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rails_charts/base_chart.rb', line 90

def build_options
  hash = {}

  hash[:series] = Array.wrap(generate_series_options)

  hash = hash.complex_merge(axises)
  hash = hash.complex_merge(defaults)
  hash = hash.complex_merge(other_options)

  hash
end

#js_codeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rails_charts/base_chart.rb', line 34

def js_code
  style_css = []
  style_css << "width: #{width}" if width
  style_css << "height: #{height}" if height
  style_css << style

  %Q{
    <div id="#{container_id}" class="#{klass}" style="#{style_css.compact.join('; ')}">
      <script>
        if (!window.RailsCharts) {
          window.RailsCharts = {}
          window.RailsCharts.charts = {}
        }

        function init_#{chart_id}(e) {
          if (document.documentElement.hasAttribute("data-turbolinks-preview")) return;
          if (document.documentElement.hasAttribute("data-turbo-preview")) return;

          <!-- #{self.class} -->
          var chartDom = document.getElementById('#{container_id}');

          if (!chartDom) { return }

          var lib = ("echarts" in window) ? window.echarts : echarts;
          var chart = lib.init(chartDom, #{theme.to_json}, { "locale": #{locale.to_json}, "renderer": #{renderer.to_json} });
          var option = #{option};
          option && chart.setOption(option);

          window.RailsCharts.charts["#{container_id}"] = chart;
        }

        function destroy_#{chart_id}(e) {
          var chart = window.RailsCharts.charts["#{container_id}"];
          if (chart) {
            chart.dispose()
          }
          delete window.RailsCharts.charts["#{container_id}"];
        }

        window.addEventListener('load', init_#{chart_id});
        window.addEventListener('turbo:load', init_#{chart_id});
        window.addEventListener('turbolinks:load', init_#{chart_id});

        document.addEventListener("turbolinks:before-render", destroy_#{chart_id});
        document.addEventListener("turbo:before-render", destroy_#{chart_id});
      </script>
    </div>
  }
end

#optionObject



84
85
86
87
88
# File 'lib/rails_charts/base_chart.rb', line 84

def option
  str = build_options.to_json
  str.gsub!(CHART_JS_PATTERN) { Base64.decode64 $1 }
  str
end

#x_axisObject



116
117
118
# File 'lib/rails_charts/base_chart.rb', line 116

def x_axis
  []
end

#y_axisObject



120
121
122
# File 'lib/rails_charts/base_chart.rb', line 120

def y_axis
  []
end