rails_charts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +487 -0
  4. data/Rakefile +3 -0
  5. data/lib/rails_charts/area_chart.rb +31 -0
  6. data/lib/rails_charts/bar_chart.rb +25 -0
  7. data/lib/rails_charts/base_chart.rb +123 -0
  8. data/lib/rails_charts/calendar_chart.rb +36 -0
  9. data/lib/rails_charts/candlestick_chart.rb +25 -0
  10. data/lib/rails_charts/custom_chart.rb +14 -0
  11. data/lib/rails_charts/donut_chart.rb +36 -0
  12. data/lib/rails_charts/engine.rb +33 -0
  13. data/lib/rails_charts/funnel_chart.rb +30 -0
  14. data/lib/rails_charts/gauge_chart.rb +30 -0
  15. data/lib/rails_charts/helpers.rb +83 -0
  16. data/lib/rails_charts/javascript.rb +17 -0
  17. data/lib/rails_charts/line_chart.rb +78 -0
  18. data/lib/rails_charts/parallel_chart.rb +30 -0
  19. data/lib/rails_charts/pie_chart.rb +55 -0
  20. data/lib/rails_charts/radar_chart.rb +38 -0
  21. data/lib/rails_charts/ruby_ext.rb +28 -0
  22. data/lib/rails_charts/sankey_chart.rb +31 -0
  23. data/lib/rails_charts/scatter_chart.rb +34 -0
  24. data/lib/rails_charts/stacked_bar_chart.rb +13 -0
  25. data/lib/rails_charts/version.rb +3 -0
  26. data/lib/rails_charts.rb +43 -0
  27. data/vendor/assets/rails_charts/echarts.min.js +45 -0
  28. data/vendor/assets/rails_charts/extension/bmap.js +413 -0
  29. data/vendor/assets/rails_charts/extension/bmap.js.map +1 -0
  30. data/vendor/assets/rails_charts/extension/bmap.min.js +22 -0
  31. data/vendor/assets/rails_charts/extension/dataTool.js +436 -0
  32. data/vendor/assets/rails_charts/extension/dataTool.js.map +1 -0
  33. data/vendor/assets/rails_charts/extension/dataTool.min.js +22 -0
  34. data/vendor/assets/rails_charts/i18n/langCS-obj.js +172 -0
  35. data/vendor/assets/rails_charts/i18n/langCS.js +168 -0
  36. data/vendor/assets/rails_charts/i18n/langDE-obj.js +172 -0
  37. data/vendor/assets/rails_charts/i18n/langDE.js +168 -0
  38. data/vendor/assets/rails_charts/i18n/langEN-obj.js +173 -0
  39. data/vendor/assets/rails_charts/i18n/langEN.js +169 -0
  40. data/vendor/assets/rails_charts/i18n/langES-obj.js +111 -0
  41. data/vendor/assets/rails_charts/i18n/langES.js +107 -0
  42. data/vendor/assets/rails_charts/i18n/langFI-obj.js +111 -0
  43. data/vendor/assets/rails_charts/i18n/langFI.js +107 -0
  44. data/vendor/assets/rails_charts/i18n/langFR-obj.js +173 -0
  45. data/vendor/assets/rails_charts/i18n/langFR.js +169 -0
  46. data/vendor/assets/rails_charts/i18n/langIT-obj.js +173 -0
  47. data/vendor/assets/rails_charts/i18n/langIT.js +169 -0
  48. data/vendor/assets/rails_charts/i18n/langJA-obj.js +173 -0
  49. data/vendor/assets/rails_charts/i18n/langJA.js +169 -0
  50. data/vendor/assets/rails_charts/i18n/langKO-obj.js +173 -0
  51. data/vendor/assets/rails_charts/i18n/langKO.js +169 -0
  52. data/vendor/assets/rails_charts/i18n/langPL-obj.js +173 -0
  53. data/vendor/assets/rails_charts/i18n/langPL.js +169 -0
  54. data/vendor/assets/rails_charts/i18n/langPT-br-obj.js +174 -0
  55. data/vendor/assets/rails_charts/i18n/langPT-br.js +170 -0
  56. data/vendor/assets/rails_charts/i18n/langRO-obj.js +173 -0
  57. data/vendor/assets/rails_charts/i18n/langRO.js +169 -0
  58. data/vendor/assets/rails_charts/i18n/langRU-obj.js +174 -0
  59. data/vendor/assets/rails_charts/i18n/langRU.js +170 -0
  60. data/vendor/assets/rails_charts/i18n/langSI-obj.js +172 -0
  61. data/vendor/assets/rails_charts/i18n/langSI.js +168 -0
  62. data/vendor/assets/rails_charts/i18n/langTH-obj.js +111 -0
  63. data/vendor/assets/rails_charts/i18n/langTH.js +107 -0
  64. data/vendor/assets/rails_charts/i18n/langZH-obj.js +168 -0
  65. data/vendor/assets/rails_charts/i18n/langZH.js +164 -0
  66. data/vendor/assets/rails_charts/theme/azul.js +163 -0
  67. data/vendor/assets/rails_charts/theme/bee-inspired.js +178 -0
  68. data/vendor/assets/rails_charts/theme/blue.js +178 -0
  69. data/vendor/assets/rails_charts/theme/caravan.js +178 -0
  70. data/vendor/assets/rails_charts/theme/carp.js +163 -0
  71. data/vendor/assets/rails_charts/theme/cool.js +180 -0
  72. data/vendor/assets/rails_charts/theme/dark-blue.js +164 -0
  73. data/vendor/assets/rails_charts/theme/dark-bold.js +164 -0
  74. data/vendor/assets/rails_charts/theme/dark-digerati.js +164 -0
  75. data/vendor/assets/rails_charts/theme/dark-fresh-cut.js +164 -0
  76. data/vendor/assets/rails_charts/theme/dark-mushroom.js +164 -0
  77. data/vendor/assets/rails_charts/theme/dark.js +224 -0
  78. data/vendor/assets/rails_charts/theme/eduardo.js +178 -0
  79. data/vendor/assets/rails_charts/theme/forest.js +163 -0
  80. data/vendor/assets/rails_charts/theme/fresh-cut.js +163 -0
  81. data/vendor/assets/rails_charts/theme/fruit.js +178 -0
  82. data/vendor/assets/rails_charts/theme/gray.js +220 -0
  83. data/vendor/assets/rails_charts/theme/green.js +222 -0
  84. data/vendor/assets/rails_charts/theme/helianthus.js +263 -0
  85. data/vendor/assets/rails_charts/theme/infographic.js +236 -0
  86. data/vendor/assets/rails_charts/theme/inspired.js +163 -0
  87. data/vendor/assets/rails_charts/theme/jazz.js +163 -0
  88. data/vendor/assets/rails_charts/theme/london.js +163 -0
  89. data/vendor/assets/rails_charts/theme/macarons.js +240 -0
  90. data/vendor/assets/rails_charts/theme/macarons2.js +249 -0
  91. data/vendor/assets/rails_charts/theme/mint.js +155 -0
  92. data/vendor/assets/rails_charts/theme/red-velvet.js +163 -0
  93. data/vendor/assets/rails_charts/theme/red.js +225 -0
  94. data/vendor/assets/rails_charts/theme/roma.js +119 -0
  95. data/vendor/assets/rails_charts/theme/royal.js +163 -0
  96. data/vendor/assets/rails_charts/theme/sakura.js +140 -0
  97. data/vendor/assets/rails_charts/theme/shine.js +177 -0
  98. data/vendor/assets/rails_charts/theme/tech-blue.js +180 -0
  99. data/vendor/assets/rails_charts/theme/vintage.js +63 -0
  100. metadata +268 -0
@@ -0,0 +1,36 @@
1
+ module RailsCharts
2
+ class CalendarChart < BaseChart
3
+ attr_reader :visual_map_options
4
+
5
+ def initialize(data, options = {})
6
+ super(data, options)
7
+ end
8
+
9
+ def type
10
+ 'heatmap'
11
+ end
12
+
13
+ def build_options
14
+ super.except(:xAxis, :yAxis)
15
+ end
16
+
17
+ def generate_series_options
18
+ {
19
+ type: type,
20
+ coordinateSystem: 'calendar',
21
+ data: data[:data]
22
+ }
23
+ end
24
+
25
+ def defaults
26
+ {
27
+ tooltip: {
28
+ item: {
29
+ trigger: 'item',
30
+ },
31
+ },
32
+ }
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ module RailsCharts
2
+ class CandlestickChart < LineChart
3
+
4
+ def type
5
+ 'candlestick'
6
+ end
7
+
8
+ def defaults
9
+ {
10
+ tooltip: {
11
+ trigger: 'axis',
12
+ axisPointer: {
13
+ type: 'cross'
14
+ }
15
+ },
16
+ toolbox: {
17
+ feature: {
18
+ saveAsImage: {}
19
+ },
20
+ }
21
+ }
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ module RailsCharts
2
+ class CustomChart < BaseChart
3
+
4
+ def initialize(data, options = {})
5
+ super([], options)
6
+ @options = options
7
+ end
8
+
9
+ def build_options
10
+ options
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ module RailsCharts
2
+ class DonutChart < PieChart
3
+
4
+ def initialize(data, options = {})
5
+ super(data, options)
6
+ end
7
+
8
+ def generate_series_options
9
+ {
10
+ data: data.map{|k, v| {name: k, value: v} },
11
+ type: type
12
+ }
13
+ end
14
+
15
+ def defaults
16
+ {
17
+ tooltip: {
18
+ item: {
19
+ trigger: 'item',
20
+ },
21
+ },
22
+ series: {
23
+ radius: ['40%', '70%']
24
+ },
25
+ emphasis: {
26
+ itemStyle: {
27
+ shadowBlur: 10,
28
+ shadowOffsetX: 0,
29
+ shadowColor: 'rgba(0, 0, 0, 0.5)'
30
+ }
31
+ }
32
+ }
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ module RailsCharts
2
+ class Engine < ::Rails::Engine
3
+
4
+ initializer "rails_charts.importmap", before: "importmap" do |app|
5
+ if defined?(Importmap)
6
+ app.config.assets.paths << File.expand_path('../..', __dir__) + "/vendor/assets"
7
+ app.config.assets.precompile << ".js"
8
+
9
+ app.config.assets.precompile += [
10
+ 'vendor/assets/**/*',
11
+ 'vendor/assets/rails_charts/**/*'
12
+ ]
13
+
14
+ app.config.importmap.cache_sweepers << Engine.root.join("vendor/assets/rails_charts")
15
+ app.config.importmap.cache_sweepers << Engine.root.join("vendor/assets")
16
+ end
17
+ end
18
+
19
+ initializer 'rails_charts.assets_precompile' do |app|
20
+ app.config.assets.paths << File.expand_path('../..', __dir__) + "/vendor/assets"
21
+ app.config.assets.precompile += [
22
+ 'vendor/assets/**/*',
23
+ ]
24
+ end
25
+
26
+ initializer 'rails_charts.helpers' do
27
+ ActiveSupport.on_load :action_view do
28
+ include RailsCharts::Helpers
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ module RailsCharts
2
+ class FunnelChart < BaseChart
3
+
4
+ def type
5
+ 'funnel'
6
+ end
7
+
8
+ def build_options
9
+ super.except(:xAxis, :yAxis)
10
+ end
11
+
12
+ def generate_series_options
13
+ {
14
+ data: data[:data],
15
+ type: type,
16
+ }
17
+ end
18
+
19
+ def defaults
20
+ {
21
+ tooltip: {
22
+ item: {
23
+ trigger: 'item',
24
+ },
25
+ },
26
+ }
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module RailsCharts
2
+ class GaugeChart < BaseChart
3
+
4
+ def type
5
+ 'gauge'
6
+ end
7
+
8
+ def build_options
9
+ super.except(:xAxis, :yAxis)
10
+ end
11
+
12
+ def generate_series_options
13
+ {
14
+ data: Array.wrap(data[:data]),
15
+ type: type,
16
+ }
17
+ end
18
+
19
+ def defaults
20
+ {
21
+ tooltip: {
22
+ item: {
23
+ trigger: 'item',
24
+ },
25
+ },
26
+ }
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,83 @@
1
+ require "json"
2
+ require "erb"
3
+
4
+ module RailsCharts
5
+ module Helpers
6
+
7
+ def line_chart(data, options = {})
8
+ generate_rails_chart LineChart, data, options
9
+ end
10
+
11
+ def bar_chart(data, options = {})
12
+ generate_rails_chart BarChart, data, options
13
+ end
14
+
15
+ def stacked_bar_chart(data, options = {})
16
+ generate_rails_chart StackedBarChart, data, options
17
+ end
18
+
19
+ def pie_chart(data, options = {})
20
+ generate_rails_chart PieChart, data, options
21
+ end
22
+
23
+ def scatter_chart(data, options = {})
24
+ generate_rails_chart ScatterChart, data, options
25
+ end
26
+
27
+ def parallel_chart(data, options = {})
28
+ generate_rails_chart ParallelChart, data, options
29
+ end
30
+
31
+ def donut_chart(data, options = {})
32
+ generate_rails_chart DonutChart, data, options
33
+ end
34
+
35
+ def sankey_chart(data, options = {})
36
+ generate_rails_chart SankeyChart, data, options
37
+ end
38
+
39
+ def area_chart(data, options = {})
40
+ generate_rails_chart AreaChart, data, options
41
+ end
42
+
43
+ def radar_chart(data, options = {})
44
+ generate_rails_chart RadarChart, data, options
45
+ end
46
+
47
+ def calendar_chart(data, options = {})
48
+ generate_rails_chart CalendarChart, data, options
49
+ end
50
+
51
+ def funnel_chart(data, options = {})
52
+ generate_rails_chart FunnelChart, data, options
53
+ end
54
+
55
+ def gauge_chart(data, options = {})
56
+ generate_rails_chart GaugeChart, data, options
57
+ end
58
+
59
+ def candlestick_chart(data, options = {})
60
+ generate_rails_chart CandlestickChart, data, options
61
+ end
62
+
63
+ def custom_chart(options = {})
64
+ generate_rails_chart CustomChart, [], options
65
+ end
66
+
67
+ def generate_rails_chart(klass, data, options = {})
68
+ klass = klass.new(data, options)
69
+ if options[:code]
70
+ tag.textarea(row: 20, cols: 80, style: 'width: 100%; height: 300px') do
71
+ klass.js_code
72
+ end
73
+ else
74
+ # tag.pre(debug(data.inspect)) +
75
+ # tag.textarea(row: 20, cols: 80, style: 'width: 100%; height: 300px') do
76
+ # klass.js_code
77
+ # end +
78
+ raw(klass.js_code)
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,17 @@
1
+ module RailsCharts
2
+ class Javascript
3
+ attr_reader :code, :record_id
4
+
5
+ def initialize(code)
6
+ @code = code
7
+ @record_id = Digest::SHA1.hexdigest([Time.now, rand].join)
8
+
9
+ Thread.current[:rails_charts_js_code] ||= {}
10
+ Thread.current[:rails_charts_js_code][record_id] = code
11
+ end
12
+
13
+ def as_json(attrs)
14
+ record_id
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,78 @@
1
+ module RailsCharts
2
+ class LineChart < BaseChart
3
+
4
+ def initialize(data, options = {})
5
+ super(Array.wrap(data.to_a), options)
6
+ end
7
+
8
+ def type
9
+ 'line'
10
+ end
11
+
12
+ def x
13
+ case data[0]
14
+ when Array
15
+ data.map{|e| e[0]}
16
+ when Hash
17
+ data.map{|e| e[:data].keys}.flatten
18
+ else
19
+ nil
20
+ end&.uniq
21
+ end
22
+
23
+ def y
24
+ case data[0]
25
+ when Array
26
+ data.map{|e| e[1]}
27
+ when Hash
28
+ data.map{|e| e[:name]}
29
+ end
30
+ end
31
+
32
+ def generate_series_options
33
+ # binding.pry if debug
34
+ case data[0]
35
+ when Array
36
+ {
37
+ data: is_a?(AreaChart) || is_a?(ScatterChart) ? data : data.map(&:last),
38
+ type: type,
39
+ }
40
+ when Hash
41
+ data.map do |e|
42
+ {
43
+ data: e[:data].is_a?(Hash) ? e[:data].values : e[:data],
44
+ type: type,
45
+ name: e[:name]
46
+ }
47
+ end
48
+ end
49
+ end
50
+
51
+ def x_axis
52
+ {
53
+ type: 'category',
54
+ data: x
55
+ }
56
+ end
57
+
58
+ def y_axis
59
+ {
60
+ type: 'value',
61
+ }
62
+ end
63
+
64
+ def defaults
65
+ {
66
+ tooltip: {
67
+ trigger: 'axis'
68
+ },
69
+ toolbox: {
70
+ feature: {
71
+ saveAsImage: {}
72
+ },
73
+ }
74
+ }
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,30 @@
1
+ module RailsCharts
2
+ class ParallelChart < BaseChart
3
+
4
+ def type
5
+ 'parallel'
6
+ end
7
+
8
+ def build_options
9
+ super.except(:xAxis, :yAxis)
10
+ end
11
+
12
+ def generate_series_options
13
+ {
14
+ data: data,
15
+ type: type
16
+ }
17
+ end
18
+
19
+ def defaults
20
+ {
21
+ series: {
22
+ lineStyle: {
23
+ width: 2
24
+ }
25
+ }
26
+ }
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ module RailsCharts
2
+ class PieChart < LineChart
3
+
4
+ def type
5
+ 'pie'
6
+ end
7
+
8
+ def build_options
9
+ super.except(:xAxis, :yAxis)
10
+ end
11
+
12
+ def generate_series_options
13
+ if data[0].is_a?(Hash)
14
+ {
15
+ data: data,
16
+ type: type
17
+ }
18
+ else
19
+ {
20
+ data: data.map{|k, v| {name: k, value: v} },
21
+ type: type
22
+ }
23
+ end
24
+ end
25
+
26
+ def x_axis
27
+ []
28
+ end
29
+
30
+ def y_axis
31
+ []
32
+ end
33
+
34
+ def defaults
35
+ {
36
+ tooltip: {
37
+ item: {
38
+ trigger: 'item',
39
+ },
40
+ },
41
+ series: {
42
+ radius: '70%',
43
+ },
44
+ emphasis: {
45
+ itemStyle: {
46
+ shadowBlur: 10,
47
+ shadowOffsetX: 0,
48
+ shadowColor: 'rgba(0, 0, 0, 0.5)'
49
+ }
50
+ }
51
+ }
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ module RailsCharts
2
+ class RadarChart < BaseChart
3
+
4
+ def type
5
+ 'radar'
6
+ end
7
+
8
+ def build_options
9
+ super.except(:xAxis, :yAxis).merge(radar)
10
+ end
11
+
12
+ def radar
13
+ {
14
+ radar: {
15
+ indicator: data[:indicators].map{|(k, v)| {name: k, value: v} }
16
+ }
17
+ }
18
+ end
19
+
20
+ def generate_series_options
21
+ {
22
+ type: type,
23
+ data: data[:data]
24
+ }
25
+ end
26
+
27
+ def defaults
28
+ {
29
+ tooltip: {
30
+ item: {
31
+ trigger: 'item',
32
+ },
33
+ },
34
+ }
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,28 @@
1
+ module RailsCharts
2
+
3
+ module RubyExt
4
+
5
+ refine ::Array do
6
+ def deep_merge(other)
7
+ map{|e| e.merge!(other) }
8
+ end
9
+ end
10
+
11
+ refine ::Hash do
12
+ def complex_merge(other)
13
+ self.each do |k, v|
14
+ next unless default = other[k]
15
+ if v.is_a?(Array) || v.is_a?(Hash)
16
+ self[k] = v.deep_merge(default)
17
+ else
18
+ self[k] = default
19
+ end
20
+ end
21
+ self.deep_merge!(other.except(*self.keys))
22
+ self
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,31 @@
1
+ module RailsCharts
2
+ class SankeyChart < BaseChart
3
+
4
+ def type
5
+ 'sankey'
6
+ end
7
+
8
+ def build_options
9
+ super.except(:xAxis, :yAxis)
10
+ end
11
+
12
+ def generate_series_options
13
+ {
14
+ data: data[:data],
15
+ links: data[:links],
16
+ type: type
17
+ }
18
+ end
19
+
20
+ def defaults
21
+ {
22
+ series: {
23
+ emphasis: {
24
+ focus: 'adjacency'
25
+ }
26
+ }
27
+ }
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ module RailsCharts
2
+ class ScatterChart < LineChart
3
+
4
+ def type
5
+ 'scatter'
6
+ end
7
+
8
+ # def generate_series_options
9
+ # {
10
+ # data: data,
11
+ # type: type,
12
+ # }
13
+ # end
14
+
15
+ def defaults
16
+ {
17
+ xAxis: {},
18
+ yAxis: {},
19
+ tooltip: {
20
+ formatter: '{c}'
21
+ },
22
+ }
23
+ end
24
+
25
+ def x_axis
26
+ {}
27
+ end
28
+
29
+ def y_axis
30
+ {}
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ module RailsCharts
2
+ class StackedBarChart < BarChart
3
+
4
+ def defaults
5
+ super.deep_merge({
6
+ series: {
7
+ stack: {}
8
+ },
9
+ })
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module RailsCharts
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ require "rails_charts/version"
2
+ require "rails_charts/ruby_ext"
3
+ require "rails_charts/javascript"
4
+ require "rails_charts/engine"
5
+ require "rails_charts/base_chart"
6
+ require "rails_charts/line_chart"
7
+ require "rails_charts/bar_chart"
8
+ require "rails_charts/stacked_bar_chart"
9
+ require "rails_charts/pie_chart"
10
+ require "rails_charts/donut_chart"
11
+ require "rails_charts/area_chart"
12
+ require "rails_charts/radar_chart"
13
+ require "rails_charts/calendar_chart"
14
+ require "rails_charts/funnel_chart"
15
+ require "rails_charts/gauge_chart"
16
+ require "rails_charts/scatter_chart"
17
+ require "rails_charts/parallel_chart"
18
+ require "rails_charts/sankey_chart"
19
+ require "rails_charts/candlestick_chart"
20
+ require "rails_charts/custom_chart"
21
+ require "rails_charts/helpers"
22
+
23
+ module RailsCharts
24
+ using RubyExt
25
+
26
+ class << self
27
+ attr_accessor :options, :defaults
28
+ end
29
+
30
+ self.options = {
31
+ theme: nil,
32
+ width: "100%",
33
+ height: "450px",
34
+ css_klass: "rails_charts",
35
+ style: nil
36
+ }
37
+
38
+ self.defaults = {
39
+ # RailsCharts::DonutChart => {
40
+ # ...
41
+ # }
42
+ }
43
+ end