apexcharts 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +248 -52
  3. data/lib/apexcharts.rb +2 -1
  4. data/lib/apexcharts/charts.rb +11 -18
  5. data/lib/apexcharts/charts/base.rb +19 -18
  6. data/lib/apexcharts/charts/bubble.rb +2 -8
  7. data/lib/apexcharts/charts/cartesian.rb +30 -29
  8. data/lib/apexcharts/charts/donut.rb +0 -1
  9. data/lib/apexcharts/charts/features/annotations.rb +16 -17
  10. data/lib/apexcharts/charts/features/mixable.rb +1 -2
  11. data/lib/apexcharts/charts/heatmap.rb +2 -4
  12. data/lib/apexcharts/charts/mixed.rb +42 -32
  13. data/lib/apexcharts/charts/pie.rb +0 -1
  14. data/lib/apexcharts/charts/polar.rb +2 -9
  15. data/lib/apexcharts/charts/radar.rb +2 -4
  16. data/lib/apexcharts/charts/radial_bar.rb +0 -1
  17. data/lib/apexcharts/charts/syncing.rb +39 -25
  18. data/lib/apexcharts/colors.rb +4 -6
  19. data/lib/apexcharts/config.rb +25 -0
  20. data/lib/apexcharts/config/default_options.rb +12 -0
  21. data/lib/apexcharts/helper.rb +151 -87
  22. data/lib/apexcharts/options/annotations.rb +5 -5
  23. data/lib/apexcharts/options/axis.rb +12 -12
  24. data/lib/apexcharts/options/chart.rb +21 -21
  25. data/lib/apexcharts/options/data_labels.rb +9 -7
  26. data/lib/apexcharts/options/div_attributes.rb +4 -5
  27. data/lib/apexcharts/options/fill.rb +7 -7
  28. data/lib/apexcharts/options/grid.rb +10 -10
  29. data/lib/apexcharts/options/legend.rb +22 -19
  30. data/lib/apexcharts/options/markers.rb +13 -13
  31. data/lib/apexcharts/options/no_data.rb +7 -7
  32. data/lib/apexcharts/options/plot_options.rb +7 -7
  33. data/lib/apexcharts/options/root.rb +30 -30
  34. data/lib/apexcharts/options/states.rb +4 -4
  35. data/lib/apexcharts/options/stroke.rb +7 -7
  36. data/lib/apexcharts/options/subtitle.rb +3 -1
  37. data/lib/apexcharts/options/theme.rb +3 -3
  38. data/lib/apexcharts/options/title.rb +8 -8
  39. data/lib/apexcharts/options/tooltip.rb +17 -17
  40. data/lib/apexcharts/options/x_axis.rb +5 -5
  41. data/lib/apexcharts/options/y_axis.rb +8 -8
  42. data/lib/apexcharts/options_builder.rb +49 -60
  43. data/lib/apexcharts/prefix_with_apex.rb +5 -0
  44. data/lib/apexcharts/prefixer.rb +19 -0
  45. data/lib/apexcharts/renderer.rb +64 -17
  46. data/lib/apexcharts/series.rb +5 -3
  47. data/lib/apexcharts/series/bubble.rb +33 -22
  48. data/lib/apexcharts/series/cartesian.rb +63 -49
  49. data/lib/apexcharts/series/polar.rb +31 -17
  50. data/lib/apexcharts/support/rails.rb +1 -1
  51. data/lib/apexcharts/support/sinatra.rb +9 -0
  52. data/lib/apexcharts/theme.rb +5 -6
  53. data/lib/apexcharts/utils.rb +1 -0
  54. data/lib/apexcharts/utils/copy.rb +9 -0
  55. data/lib/apexcharts/utils/date_time.rb +50 -44
  56. data/lib/apexcharts/utils/hash.rb +2 -3
  57. data/lib/apexcharts/version.rb +1 -1
  58. data/vendor/assets/javascripts/apexcharts.js +2 -2
  59. metadata +27 -7
@@ -1,34 +1,35 @@
1
1
  module ApexCharts
2
2
  class BaseChart
3
- attr_reader :options, :series
3
+ include Utils::Hash
4
4
 
5
- def initialize data, options={}
6
- @series = sanitize_data(data)
7
- @options = Utils::Hash.deep_merge(
8
- build_options(x_sample, options),
9
- Utils::Hash.camelize_keys(
10
- {**@series, chart: {type: chart_type}}.compact
11
- )
12
- )
5
+ attr_reader :options, :series, :sample
6
+
7
+ def initialize(data, options={})
8
+ @series = build_series(data)
9
+ @options = build_options(options)
13
10
  end
14
11
 
15
12
  def render
16
- ApexCharts::Renderer.render_default(options)
13
+ Renderer.render_default(options)
17
14
  end
18
15
 
19
- def chart_type
20
- end
16
+ def chart_type; end
21
17
 
22
18
  protected
23
19
 
24
- def build_options(x_sample, options)
25
- ApexCharts::OptionsBuilder.new(x_sample, options).build_options
20
+ def build_series(data)
21
+ series_object = series_type.new(data)
22
+ @sample = series_object.sample
23
+ series_object.sanitized
26
24
  end
27
25
 
28
- def x_sample
29
- series[:series][0][:data][0][:x]
26
+ def build_options(options)
27
+ deep_merge(
28
+ OptionsBuilder.new(sample, options).build_options,
29
+ camelize_keys(
30
+ {**@series, chart: {type: chart_type}}.compact
31
+ )
32
+ )
30
33
  end
31
34
  end
32
35
  end
33
-
34
-
@@ -6,14 +6,8 @@ module ApexCharts
6
6
  'bubble'
7
7
  end
8
8
 
9
- private
10
-
11
- def sanitize_data(data)
12
- ApexCharts::BubbleSeries.new(data).sanitized
13
- end
14
-
15
- def x_sample
16
- series[:series][0][:data][0][0]
9
+ def series_type
10
+ BubbleSeries
17
11
  end
18
12
  end
19
13
  end
@@ -2,67 +2,69 @@
2
2
 
3
3
  require_relative 'features/annotations'
4
4
  require_relative 'features/mixable'
5
+ require_relative '../utils/hash'
5
6
 
6
7
  module ApexCharts
7
8
  class CartesianChart < BaseChart
8
9
  include Annotations
9
10
  include Mixable
11
+ include Utils::Hash
10
12
 
11
- def initialize bindings, data, options={}, &block
12
- @bindings = bindings
13
- options = Utils::Hash.deep_merge(
14
- Utils::Hash.camelize_keys(options),
15
- Utils::Hash.camelize_keys(more_options)
16
- )
13
+ def initialize(outer_self, data, options={}, &block)
14
+ @outer_self = outer_self
15
+ options = deep_merge(
16
+ camelize_keys(options),
17
+ camelize_keys(more_options)
18
+ )
17
19
 
18
- build_instance_variables if @bindings
20
+ build_instance_variables if @outer_self
19
21
 
20
22
  instance_eval &block if block_given?
21
23
 
22
24
  options[:annotations] = @annotations if @annotations
23
- @series = sanitize_data(data)
24
- @options = Utils::Hash.deep_merge(
25
- build_options(x_sample, options),
26
- Utils::Hash.camelize_keys(
27
- {**@series, chart: {type: chart_type}}.compact
28
- )
29
- )
30
-
31
- get_selection_range if brush?
25
+ @series = build_series(data)
26
+ @options = build_options(options)
27
+
28
+ build_selection_range if brush?
29
+ end
30
+
31
+ def series_type
32
+ CartesianSeries
32
33
  end
33
34
 
34
35
  def more_options
35
36
  {}
36
37
  end
37
38
 
38
- def method_missing method, *args, &block
39
- if @bindings
40
- @bindings.send method, *args, &block
39
+ def method_missing(method, *args, &block)
40
+ if @outer_self.respond_to?(method)
41
+ @outer_self.send method, *args, &block
41
42
  else
42
43
  super
43
44
  end
44
45
  end
45
46
 
47
+ def respond_to_missing?(method, *args)
48
+ @outer_self.respond_to?(method) || super
49
+ end
50
+
46
51
  protected
47
52
 
48
53
  def build_instance_variables
49
- (@bindings.instance_variables - instance_variables).each do |i|
50
- instance_variable_set(i, @bindings.instance_variable_get(i))
54
+ (@outer_self.instance_variables - instance_variables).each do |i|
55
+ instance_variable_set(i, @outer_self.instance_variable_get(i))
51
56
  end
52
57
  end
53
58
 
54
- def sanitize_data(data)
55
- ApexCharts::CartesianSeries.new(data).sanitized
56
- end
57
-
58
59
  def brush?
59
60
  @options[:chart][:brush]&.[](:enabled) && \
60
61
  !@options[:chart][:selection]&.[](:xaxis)
61
62
  end
62
63
 
63
- def get_selection_range
64
- first_x = @series[:series].last[:data].first[:x]
65
- last_x = @series[:series].last[:data].last[:x]
64
+ def build_selection_range
65
+ last_data = @series[:series].last[:data]
66
+ first_x = last_data.first[:x]
67
+ last_x = last_data.last[:x]
66
68
  @options[:chart][:selection][:xaxis] = {
67
69
  min: handle_time(twenty_percent_before_last_x(first_x, last_x)),
68
70
  max: handle_time(last_x)
@@ -78,4 +80,3 @@ module ApexCharts
78
80
  end
79
81
  end
80
82
  end
81
-
@@ -7,4 +7,3 @@ module ApexCharts
7
7
  end
8
8
  end
9
9
  end
10
-
@@ -3,36 +3,35 @@ module ApexCharts
3
3
  def annotation(axis, value:, text:, color: nil, **options)
4
4
  @annotations ||= {}
5
5
 
6
- unless [:xaxis, :yaxis, :points].include? axis
7
- raise "unrecognized axis: #{axis}"
8
- end
6
+ raise "unrecognized axis: #{axis}" unless %i[xaxis yaxis points].include? axis
7
+
9
8
  @annotations[axis] ||= []
10
9
  @annotations[axis] << annotation_value(axis, value).merge(
11
- if axis == :points
12
- {marker: {size: 8, fillColor: 'white', strokeColor: color, radius: 2}, **options}
13
- else
14
- {borderColor: color, fillColor: color, opacity: 0.2}
15
- end
16
- ).merge(
17
- annotation_label(text, color, **options)
18
- )
10
+ if axis == :points
11
+ {marker: {size: 8, fillColor: 'white', strokeColor: color, radius: 2}, **options}
12
+ else
13
+ {borderColor: color, fillColor: color, opacity: 0.2}
14
+ end
15
+ ).merge(
16
+ annotation_label(text, color, **options)
17
+ )
19
18
  end
20
19
 
21
- def x_annotation **args
20
+ def x_annotation(**args)
22
21
  annotation :xaxis, **args
23
22
  end
24
23
 
25
- def y_annotation **args
24
+ def y_annotation(**args)
26
25
  annotation :yaxis, **args
27
26
  end
28
27
 
29
- def point_annotation **args
28
+ def point_annotation(**args)
30
29
  annotation :points, **args
31
30
  end
32
31
 
33
32
  private
34
33
 
35
- def annotation_value axis, value
34
+ def annotation_value(axis, value)
36
35
  axis = axis.to_s.delete_suffix('axis').to_sym
37
36
  case value
38
37
  when Range
@@ -48,7 +47,7 @@ module ApexCharts
48
47
  end
49
48
  else
50
49
  if axis == :points
51
- value.map!{ |x| Utils::DateTime.convert(x) }
50
+ value.map! {|x| Utils::DateTime.convert(x) }
52
51
  {x: value.first, y: value.last}
53
52
  else
54
53
  value = Utils::DateTime.convert(value)
@@ -57,7 +56,7 @@ module ApexCharts
57
56
  end
58
57
  end
59
58
 
60
- def annotation_label text, color, **options
59
+ def annotation_label(text, color, **options)
61
60
  {
62
61
  label: {
63
62
  borderColor: color,
@@ -3,9 +3,8 @@
3
3
  module ApexCharts
4
4
  module Mixable
5
5
  def mixed_series
6
- series[:series].each{|d| d.merge!(type: chart_type) }
6
+ series[:series].each {|d| d.merge!(type: chart_type) }
7
7
  series[:series]
8
8
  end
9
9
  end
10
10
  end
11
-
@@ -6,10 +6,8 @@ module ApexCharts
6
6
  'heatmap'
7
7
  end
8
8
 
9
- private
10
-
11
- def sanitize_data(data)
12
- ApexCharts::CartesianSeries.new(data).sanitized
9
+ def series_type
10
+ CartesianSeries
13
11
  end
14
12
  end
15
13
  end
@@ -3,9 +3,10 @@
3
3
  module ApexCharts
4
4
  class MixedCharts < BaseChart
5
5
  include Annotations
6
+ include Utils::Hash
6
7
 
7
- def initialize bindings, options={}, &block
8
- @bindings = bindings
8
+ def initialize(outer_self, options={}, &block)
9
+ @outer_self = outer_self
9
10
  @series = {series: []}
10
11
  options[:id] ||= apexcharts_id
11
12
  build_instance_variables
@@ -13,50 +14,58 @@ module ApexCharts
13
14
  instance_eval &block
14
15
 
15
16
  options[:annotations] = @annotations if @annotations
16
- @options = Utils::Hash.deep_merge(
17
- build_options(x_sample, options),
18
- Utils::Hash.camelize_keys(
19
- {chart: {type: 'area'}, **@series}
20
- )
21
- )
22
-
23
- get_selection_range if brush?
17
+ @options = build_options(options)
18
+
19
+ build_selection_range if brush?
20
+ end
21
+
22
+ def line_chart(data, options={}, &block)
23
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
24
+ @series[:series] +=
25
+ LineChart.new(outer_self, data, options, &block).mixed_series
24
26
  end
25
27
 
26
- def line_chart data, options={}, &block
27
- bindings = eval("self", block.binding) if block_given?
28
- @series[:series] += LineChart.new(bindings, data, options, &block).mixed_series
28
+ def area_chart(data, options={}, &block)
29
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
30
+ @series[:series] +=
31
+ AreaChart.new(outer_self, data, options, &block).mixed_series
29
32
  end
30
33
 
31
- def area_chart data, options={}, &block
32
- bindings = eval("self", block.binding) if block_given?
33
- @series[:series] += AreaChart.new(bindings, data, options, &block).mixed_series
34
+ def bar_chart(data, options={}, &block)
35
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
36
+ @series[:series] +=
37
+ BarChart.new(outer_self, data, options, &block).mixed_series
34
38
  end
35
39
 
36
- def bar_chart data, options={}, &block
37
- bindings = eval("self", block.binding) if block_given?
38
- @series[:series] += BarChart.new(bindings, data, options, &block).mixed_series
40
+ def column_chart(data, options={}, &block)
41
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
42
+ @series[:series] +=
43
+ ColumnChart.new(outer_self, data, options, &block).mixed_series
39
44
  end
40
45
 
41
- def column_chart data, options={}, &block
42
- bindings = eval("self", block.binding) if block_given?
43
- @series[:series] += ColumnChart.new(bindings, data, options, &block).mixed_series
46
+ def scatter_chart(data, options={}, &block)
47
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
48
+ @series[:series] +=
49
+ ScatterChart.new(outer_self, data, options, &block).mixed_series
44
50
  end
45
51
 
46
- def scatter_chart data, options={}, &block
47
- bindings = eval("self", block.binding) if block_given?
48
- @series[:series] += ScatterChart.new(bindings, data, options, &block).mixed_series
52
+ def method_missing(method, *args, &block)
53
+ if @outer_self.respond_to?(method)
54
+ @outer_self.send method, *args, &block
55
+ else
56
+ super
57
+ end
49
58
  end
50
59
 
51
- def method_missing method, *args, &block
52
- @bindings.send method, *args, &block
60
+ def respond_to_missing?(method, *args)
61
+ @outer_self.respond_to?(method) || super
53
62
  end
54
63
 
55
64
  private
56
65
 
57
66
  def build_instance_variables
58
- (@bindings.instance_variables - instance_variables).each do |i|
59
- instance_variable_set(i, @bindings.instance_variable_get(i))
67
+ (@outer_self.instance_variables - instance_variables).each do |i|
68
+ instance_variable_set(i, @outer_self.instance_variable_get(i))
60
69
  end
61
70
  end
62
71
 
@@ -65,9 +74,10 @@ module ApexCharts
65
74
  !@options[:chart][:selection]&.[](:xaxis)
66
75
  end
67
76
 
68
- def get_selection_range
69
- first_x = @series[:series].last[:data].first[:x]
70
- last_x = @series[:series].last[:data].last[:x]
77
+ def build_selection_range
78
+ last_data = @series[:series].last[:data]
79
+ first_x = last_data.first[:x]
80
+ last_x = last_data.last[:x]
71
81
  @options[:chart][:selection][:xaxis] = {
72
82
  min: handle_time(twenty_percent_before_last_x(first_x, last_x)),
73
83
  max: handle_time(last_x)
@@ -7,4 +7,3 @@ module ApexCharts
7
7
  end
8
8
  end
9
9
  end
10
-
@@ -1,14 +1,7 @@
1
1
  module ApexCharts
2
2
  class PolarChart < BaseChart
3
-
4
- protected
5
-
6
- def sanitize_data(data)
7
- ApexCharts::PolarSeries.new(data).sanitized
8
- end
9
-
10
- def x_sample
3
+ def series_type
4
+ PolarSeries
11
5
  end
12
6
  end
13
7
  end
14
-
@@ -6,10 +6,8 @@ module ApexCharts
6
6
  'radar'
7
7
  end
8
8
 
9
- private
10
-
11
- def sanitize_data(data)
12
- ApexCharts::CartesianSeries.new(data).sanitized
9
+ def series_type
10
+ CartesianSeries
13
11
  end
14
12
  end
15
13
  end
@@ -7,4 +7,3 @@ module ApexCharts
7
7
  end
8
8
  end
9
9
  end
10
-
@@ -2,9 +2,9 @@
2
2
 
3
3
  module ApexCharts
4
4
  class SyncingCharts
5
- def initialize bindings, options={}, &block
6
- @bindings = bindings
7
- @html = ""
5
+ def initialize(outer_self, options={}, &block)
6
+ @outer_self = outer_self
7
+ @html = ''
8
8
  build_instance_variables
9
9
  @options = options
10
10
  @options[:group] ||= apexcharts_group
@@ -14,40 +14,46 @@ module ApexCharts
14
14
  @options[:annotations] = @annotations if @annotations
15
15
  end
16
16
 
17
- def line_chart data, options={}, &block
17
+ def line_chart(data, options={}, &block)
18
18
  options[:id] = apexcharts_id
19
- bindings = eval("self", block.binding) if block_given?
20
- @html += LineChart.new(bindings, data, @options.merge(options), &block).render
19
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
20
+ @html +=
21
+ LineChart.new(outer_self, data, @options.merge(options), &block).render
21
22
  end
22
23
 
23
- def area_chart data, options={}, &block
24
+ def area_chart(data, options={}, &block)
24
25
  options[:id] = apexcharts_id
25
- bindings = eval("self", block.binding) if block_given?
26
- @html += AreaChart.new(bindings, data, @options.merge(options), &block).render
26
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
27
+ @html +=
28
+ AreaChart.new(outer_self, data, @options.merge(options), &block).render
27
29
  end
28
30
 
29
- def bar_chart data, options={}, &block
31
+ def bar_chart(data, options={}, &block)
30
32
  options[:id] = apexcharts_id
31
- bindings = eval("self", block.binding) if block_given?
32
- @html += BarChart.new(bindings, data, @options.merge(options), &block).render
33
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
34
+ @html +=
35
+ BarChart.new(outer_self, data, @options.merge(options), &block).render
33
36
  end
34
37
 
35
- def column_chart data, options={}, &block
38
+ def column_chart(data, options={}, &block)
36
39
  options[:id] = apexcharts_id
37
- bindings = eval("self", block.binding) if block_given?
38
- @html += ColumnChart.new(bindings, data, @options.merge(options), &block).render
40
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
41
+ @html +=
42
+ ColumnChart.new(outer_self, data, @options.merge(options), &block).render
39
43
  end
40
44
 
41
- def scatter_chart data, options={}, &block
45
+ def scatter_chart(data, options={}, &block)
42
46
  options[:id] = apexcharts_id
43
- bindings = eval("self", block.binding) if block_given?
44
- @html += ScatterChart.new(bindings, data, @options.merge(options), &block).render
47
+ outer_self = eval('self', block.binding, __FILE__, __LINE__) if block_given?
48
+ @html +=
49
+ ScatterChart.new(outer_self, data, @options.merge(options), &block).render
45
50
  end
46
51
 
47
- def mixed_charts options={}, &block
52
+ def mixed_charts(options={}, &block)
48
53
  options[:id] = apexcharts_id
49
- bindings = eval "self", block.binding
50
- @html += MixedCharts.new(bindings, @options.merge(options), &block).render
54
+ outer_self = eval('self', block.binding, __FILE__, __LINE__)
55
+ @html +=
56
+ MixedCharts.new(outer_self, @options.merge(options), &block).render
51
57
  end
52
58
  alias_method :combo_charts, :mixed_charts
53
59
 
@@ -55,15 +61,23 @@ module ApexCharts
55
61
  @html
56
62
  end
57
63
 
58
- def method_missing method, *args, &block
59
- @bindings.send method, *args, &block
64
+ def method_missing(method, *args, &block)
65
+ if @outer_self.respond_to? method, *args
66
+ @outer_self.send method, *args, &block
67
+ else
68
+ super
69
+ end
70
+ end
71
+
72
+ def respond_to_missing?(method, *args)
73
+ @outer_self.respond_to? method, *args || super
60
74
  end
61
75
 
62
76
  private
63
77
 
64
78
  def build_instance_variables
65
- (@bindings.instance_variables - instance_variables).each do |i|
66
- instance_variable_set(i, @bindings.instance_variable_get(i))
79
+ (@outer_self.instance_variables - instance_variables).each do |i|
80
+ instance_variable_set(i, @outer_self.instance_variable_get(i))
67
81
  end
68
82
  end
69
83
  end