Class: RailsCharts::BaseChart
- Inherits:
-
Object
- Object
- RailsCharts::BaseChart
- Defined in:
- lib/rails_charts/base_chart.rb
Direct Known Subclasses
CalendarChart, CustomChart, FunnelChart, GaugeChart, LineChart, ParallelChart, RadarChart, SankeyChart
Constant Summary collapse
- CHART_JS_PATTERN =
/"RAILS_CHART_JS:((?!RAILS_CHART_JS:).*?):RAILS_CHART_JS_END"/
Instance Attribute Summary collapse
-
#chart_id ⇒ Object
readonly
Returns the value of attribute chart_id.
-
#container_id ⇒ Object
readonly
Returns the value of attribute container_id.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#other_options ⇒ Object
readonly
Returns the value of attribute other_options.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
-
#vertical ⇒ Object
readonly
Returns the value of attribute vertical.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #axises ⇒ Object
- #build_options ⇒ Object
-
#initialize(data, options = {}) ⇒ BaseChart
constructor
A new instance of BaseChart.
- #js_code ⇒ Object
- #option ⇒ Object
- #x_axis ⇒ Object
- #y_axis ⇒ Object
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, = {}) @data = data @options = @other_options = .delete(:options).presence || {} @defaults = RailsCharts.defaults[self.class].presence || {} @chart_id = "rails_charts_#{Digest::SHA1.hexdigest([Time.now, rand].join)}" @container_id = .delete(:id).presence || @chart_id @width = .delete(:width).presence || RailsCharts.[:width] @height = .delete(:height).presence || RailsCharts.[:height] @theme = .delete(:theme).presence || RailsCharts.[:theme] @locale = .delete(:locale).presence || RailsCharts.[:locale] @renderer = .delete(:renderer).presence || RailsCharts.[:renderer] || "canvas" @klass = .delete(:class).presence || RailsCharts.[:class] @style = .delete(:style).presence || RailsCharts.[:style] @debug = .delete(:debug) @vertical = .delete(:vertical).presence end |
Instance Attribute Details
#chart_id ⇒ Object (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_id ⇒ Object (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 |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/rails_charts/base_chart.rb', line 7 def data @data end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
9 10 11 |
# File 'lib/rails_charts/base_chart.rb', line 9 def debug @debug end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
7 8 9 |
# File 'lib/rails_charts/base_chart.rb', line 7 def defaults @defaults end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/rails_charts/base_chart.rb', line 8 def height @height end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/rails_charts/base_chart.rb', line 8 def klass @klass end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
8 9 10 |
# File 'lib/rails_charts/base_chart.rb', line 8 def locale @locale end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/rails_charts/base_chart.rb', line 7 def @options end |
#other_options ⇒ Object (readonly)
Returns the value of attribute other_options.
9 10 11 |
# File 'lib/rails_charts/base_chart.rb', line 9 def @other_options end |
#renderer ⇒ Object (readonly)
Returns the value of attribute renderer.
8 9 10 |
# File 'lib/rails_charts/base_chart.rb', line 8 def renderer @renderer end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
8 9 10 |
# File 'lib/rails_charts/base_chart.rb', line 8 def style @style end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
8 9 10 |
# File 'lib/rails_charts/base_chart.rb', line 8 def theme @theme end |
#vertical ⇒ Object (readonly)
Returns the value of attribute vertical.
10 11 12 |
# File 'lib/rails_charts/base_chart.rb', line 10 def vertical @vertical end |
#width ⇒ Object (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
#axises ⇒ Object
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_options ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rails_charts/base_chart.rb', line 90 def hash = {} hash[:series] = Array.wrap() hash = hash.complex_merge(axises) hash = hash.complex_merge(defaults) hash = hash.complex_merge() hash end |
#js_code ⇒ Object
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 |
#option ⇒ Object
84 85 86 87 88 |
# File 'lib/rails_charts/base_chart.rb', line 84 def option str = .to_json str.gsub!(CHART_JS_PATTERN) { Base64.decode64 $1 } str end |
#x_axis ⇒ Object
116 117 118 |
# File 'lib/rails_charts/base_chart.rb', line 116 def x_axis [] end |
#y_axis ⇒ Object
120 121 122 |
# File 'lib/rails_charts/base_chart.rb', line 120 def y_axis [] end |