apexcharts 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 440e28c754dc121a96eabbfdef5215fd5efa44939111a031dfcf008ebb1362db
4
- data.tar.gz: 373e2f066849672f92d6be10ce978fdf1077cec3ca2aa3da4dbfa2c3c6e3350e
3
+ metadata.gz: 911c4f7abf6adcf9da0230826af3f658b6281a6ed2e5a56079240c4a124e0e3d
4
+ data.tar.gz: 865dbe2ef98854e98c6a66af3c6e81195a1124dcfd85117a3d38cac5c5d39e32
5
5
  SHA512:
6
- metadata.gz: 0ab7c90e2a7957d8c805b9561421a1368d2b3f3e0bc0b82e3ce41863790fe0450aa6ce93c5f53659e8ecc010d92f660ee6faa8a2fbf217fc5ea6dea4c1a41ff8
7
- data.tar.gz: 98185d2d33c2895bd4cf1c289add3fcd51d0f6d83ccea1e334ce9f506289fab89c01ffe2556b91663c347c8f6b2c234a4bcda2236027929f648961797a3f0339
6
+ metadata.gz: 70632205846e3a684dc892bb047fac617b92e9eba36173d53ac7da298e84c401d0b73dce3d628f451533aae024fd2972b89f120d6698e0d49a3d4cd915e19246
7
+ data.tar.gz: 17524c53370e02d38af478600d18455d979821d97cf6a45d01f6a2bb0ac3c0ead50f58e0264dadb9cf5ed0dbe55a9d9c3ea27ef2dc59b38749651d3b663537e1
data/README.md CHANGED
@@ -9,11 +9,6 @@
9
9
  </p>
10
10
 
11
11
 
12
- <p align="center">Beautiful and interactive web charts for rubyist.</p>
13
-
14
-
15
- <p align="center"><img src="https://apexcharts.com/media/apexcharts-banner.png"></p>
16
-
17
12
  ## Usage
18
13
 
19
14
  ### Cartesian Charts
@@ -42,6 +37,7 @@ and I'll get the data in this format:
42
37
  ..
43
38
  }
44
39
  ```
40
+ PS: `Property` can be any model you have and `inactive` and `active` is just a normal scope.
45
41
 
46
42
  Example options used for cartesian charts:
47
43
 
@@ -55,6 +51,7 @@ Example options used for cartesian charts:
55
51
  } %>
56
52
  ```
57
53
 
54
+
58
55
  #### Line Chart
59
56
 
60
57
  ```erb
@@ -63,6 +60,14 @@ Example options used for cartesian charts:
63
60
  ![Example Line Chart](images/line_chart.png)
64
61
 
65
62
 
63
+ #### Stepline Chart
64
+
65
+ ```erb
66
+ <%= line_chart(series, {**options, theme: 'palette7', curve: 'stepline'}) %>
67
+ ```
68
+ ![Example Stepline Chart](images/stepline_chart.png)
69
+
70
+
66
71
  #### Area Chart
67
72
 
68
73
  ```erb
@@ -102,18 +107,25 @@ Given that:
102
107
  ```ruby
103
108
  @total_properties = Property.group_by_week(:created_at).count
104
109
  ```
110
+ and
111
+ ```erb
112
+ <% total_series = {
113
+ name: "Total", data: @total_properties
114
+ } %>
115
+ ```
105
116
  you can do this:
106
117
  ```erb
107
118
  <%= combo_chart({**options, theme: 'palette4', stacked: false, data_labels: false}) do %>
108
- <% line_chart({name: "Total", data: @total_properties}) %>
109
- <% area_chart({name: "Active", data: @active_properties}) %>
110
- <% column_chart({name: "Inactive", data: @inactive_properties}) %>
119
+ <% line_chart(total_series) %>
120
+ <% area_chart(series.last) %>
121
+ <% column_chart(series.first) %>
111
122
  <% end %>
112
123
  ```
113
124
  ![Example Mixed Chart](images/mixed_chart.gif)
114
125
 
115
126
 
116
127
  #### Syncing Chart
128
+
117
129
  You can synchronize charts by using `syncing_chart` or `synchronized_chart` methods. For example:
118
130
  ```erb
119
131
  <%= syncing_chart(chart: {toolbar: false}, height: 250, style: 'display: inline-block; width: 32%;') do %>
@@ -128,18 +140,32 @@ You can synchronize charts by using `syncing_chart` or `synchronized_chart` meth
128
140
  ![Example Syncing Chart](images/syncing_chart.gif)
129
141
 
130
142
 
143
+ #### Brush Chart
144
+
145
+ ```erb
146
+ <%= area_chart(total_series, {
147
+ **options, chart_id: 'the-chart', xtitle: nil, theme: 'palette2'
148
+ }) %>
149
+ <%= mixed_chart(brush_target: 'the-chart', theme: 'palette7') do %>
150
+ <% column_chart(series.first) %>
151
+ <% line_chart(series.last) %>
152
+ <% end %>
153
+ ```
154
+ ![Example Brush Chart](images/brush_chart.gif)
155
+
156
+
131
157
  #### Annotations
132
158
 
133
159
  All cartesian charts can have annotations, for example:
134
160
 
135
161
  ```erb
136
- <%= line_chart(series, options) do %>
162
+ <%= area_chart(series, {**options, theme: 'palette9'}) do %>
137
163
  <% x_annotation(value: ('2019-01-06'..'2019-02-24'), text: "Busy Time", color: 'green') %>
138
164
  <% y_annotation(value: 29, text: "Max Properties", color: 'blue') %>
139
165
  <% point_annotation(value: ['2018-10-07', 24], text: "First Peak", color: 'magenta') %>
140
166
  <% end %>
141
167
  ```
142
- ![Example Line Chart with Annotations](images/chart_with_annotations.png)
168
+ ![Example Area Chart with Annotations](images/chart_with_annotations.png)
143
169
 
144
170
 
145
171
  ### Polar Charts
@@ -213,8 +239,8 @@ require("apexcharts")
213
239
  ## Objective
214
240
  - To bring out as much apexcharts.js capabilities as possible but in ruby ways.
215
241
 
216
- ## Roadmap
217
- - Other charts (~~pie~~, ~~donut~~, radar, heatmap, etc.)
242
+ ## TODOs
243
+ - Other charts (radar, heatmap, candlestick, etc.)
218
244
  - Support other ruby frameworks (sinatra, hanami, etc.)
219
245
  - Render as Vue or React elements
220
246
 
@@ -4,7 +4,7 @@ module Apexcharts
4
4
  'bar'
5
5
  end
6
6
 
7
- def plot_options
7
+ def more_options
8
8
  {plot_options: {bar: {horizontal: true}}}
9
9
  end
10
10
  end
@@ -1,11 +1,14 @@
1
- require_relative 'capabilities/annotations'
1
+ require_relative 'features/annotations'
2
2
 
3
3
  module Apexcharts
4
4
  class CartesianChart
5
5
  include Annotations
6
6
 
7
- def initialize data, options={}, &block
8
- options = {**options, **plot_options}
7
+ def initialize bindings, data, options={}, &block
8
+ @bindings = bindings
9
+ options = {**options, **more_options}
10
+ build_instance_variables if @bindings
11
+
9
12
  instance_eval &block if block_given?
10
13
 
11
14
  options[:annotations] = @annotations if @annotations
@@ -16,12 +19,14 @@ module Apexcharts
16
19
  {**@series, chart: {type: chart_type}}.compact
17
20
  )
18
21
  )
22
+
23
+ get_selection_range if brush?
19
24
  end
20
25
 
21
26
  def chart_type
22
27
  end
23
28
 
24
- def plot_options
29
+ def more_options
25
30
  {}
26
31
  end
27
32
 
@@ -31,8 +36,8 @@ module Apexcharts
31
36
  end
32
37
 
33
38
  def render
34
- attributes = @options.delete(:div)
35
- variable = attributes.delete(:var) || "chart#{attributes[:id][/\d+/]}"
39
+ attributes = @options.delete(:div) { {} }
40
+ variable = attributes.delete(:var) { "chart#{attributes[:id]&.[](/\d+/)}" }
36
41
  element_id = attributes.delete(:id)
37
42
  css_class = attributes.delete(:class)
38
43
  height = "#{@options[:chart][:height].to_i}px"
@@ -46,14 +51,50 @@ module Apexcharts
46
51
  HTML
47
52
  end
48
53
 
54
+ def method_missing method, *args, &block
55
+ if @bindings
56
+ @bindings.send method, *args, &block
57
+ else
58
+ super
59
+ end
60
+ end
61
+
49
62
  private
50
63
 
64
+ def build_instance_variables
65
+ (@bindings.instance_variables - instance_variables).each do |i|
66
+ instance_variable_set(i, @bindings.instance_variable_get(i))
67
+ end
68
+ end
69
+
51
70
  def sanitize_data(data)
52
71
  Apexcharts::CartesianSeries.new(data).sanitized
53
72
  end
54
73
 
55
74
  def build_options(x_sample, options)
56
- Apexcharts::OptionsBuilder.new(x_sample, options).built
75
+ Apexcharts::OptionsBuilder.new(x_sample, options).build_options
76
+ end
77
+
78
+ def brush?
79
+ @options[:chart][:brush]&.[](:enabled) && \
80
+ !@options[:chart][:selection]&.[](:xaxis)
81
+ end
82
+
83
+ def get_selection_range
84
+ first_x = @series[:series].last[:data].first[:x]
85
+ last_x = @series[:series].last[:data].last[:x]
86
+ @options[:chart][:selection][:xaxis] = {
87
+ min: handle_time(twenty_percent_before_last_x(first_x, last_x)),
88
+ max: handle_time(last_x)
89
+ }
90
+ end
91
+
92
+ def twenty_percent_before_last_x(first, last)
93
+ last - (0.2 * (last - first))
94
+ end
95
+
96
+ def handle_time(input)
97
+ Utils::DateTime.convert(input)
57
98
  end
58
99
  end
59
100
  end
@@ -1,6 +1,6 @@
1
1
  module Apexcharts
2
2
  class ColumnChart < BarChart
3
- def plot_options
3
+ def more_options
4
4
  {plot_options: {bar: {horizontal: false}}}
5
5
  end
6
6
  end
@@ -1,14 +1,14 @@
1
1
  module Apexcharts
2
2
  module Annotations
3
- def annotation(position, value:, text:, color: nil, **options)
3
+ def annotation(axis, value:, text:, color: nil, **options)
4
4
  @annotations ||= {}
5
5
 
6
- unless [:xaxis, :yaxis, :points].include? position
7
- raise "unrecognized position: #{position}"
6
+ unless [:xaxis, :yaxis, :points].include? axis
7
+ raise "unrecognized axis: #{axis}"
8
8
  end
9
- @annotations[position] ||= []
10
- @annotations[position] << annotation_value(position, value).merge(
11
- if position == :points
9
+ @annotations[axis] ||= []
10
+ @annotations[axis] << annotation_value(axis, value).merge(
11
+ if axis == :points
12
12
  {marker: {size: 8, fillColor: 'white', strokeColor: color, radius: 2}, **options}
13
13
  else
14
14
  {borderColor: color, fillColor: color, opacity: 0.2}
@@ -32,27 +32,27 @@ module Apexcharts
32
32
 
33
33
  private
34
34
 
35
- def annotation_value position, value
36
- position = position.to_s.delete_suffix('axis').to_sym
35
+ def annotation_value axis, value
36
+ axis = axis.to_s.delete_suffix('axis').to_sym
37
37
  case value
38
38
  when Range
39
39
  value = Utils::DateTime.convert_range(value)
40
40
 
41
- case position
41
+ case axis
42
42
  when :x
43
43
  {x: value.first, x2: value.last}
44
44
  when :y
45
45
  {y: value.first, y2: value.last}
46
46
  else
47
- raise "position :points doesn't accept value of type Range"
47
+ raise "axis :points doesn't accept value of type Range"
48
48
  end
49
49
  else
50
- if position == :points
50
+ if axis == :points
51
51
  value.map!{ |x| Utils::DateTime.convert(x) }
52
52
  {x: value.first, y: value.last}
53
53
  else
54
54
  value = Utils::DateTime.convert(value)
55
- {position => value}
55
+ {axis => value}
56
56
  end
57
57
  end
58
58
  end
@@ -2,39 +2,48 @@ module Apexcharts
2
2
  class MixedChart
3
3
  include Annotations
4
4
 
5
- def initialize options={}, bindings, &block
5
+ def initialize bindings, options={}, &block
6
6
  @bindings = bindings
7
- @mixed_series = {series: []}
7
+ @series = {series: []}
8
+ options[:id] ||= apexcharts_id
8
9
  build_instance_variables
10
+
9
11
  instance_eval &block
10
12
 
11
13
  options[:annotations] = @annotations if @annotations
12
14
  @options = Utils::Hash.camelize_keys(
13
15
  Utils::Hash.deep_merge(
14
- build_options(@mixed_series[:series][0][:data][0][:x], options),
15
- {chart: {type: 'area'}, **@mixed_series}
16
+ build_options(@series[:series][0][:data][0][:x], options),
17
+ {chart: {type: 'area'}, **@series}
16
18
  )
17
19
  )
20
+
21
+ get_selection_range if brush?
18
22
  end
19
23
 
20
24
  def line_chart data, options={}, &block
21
- @mixed_series[:series] += LineChart.new(data, options, &block).mixed_series
25
+ bindings = eval("self", block.binding) if block_given?
26
+ @series[:series] += LineChart.new(bindings, data, options, &block).mixed_series
22
27
  end
23
28
 
24
29
  def area_chart data, options={}, &block
25
- @mixed_series[:series] += AreaChart.new(data, options, &block).mixed_series
30
+ bindings = eval("self", block.binding) if block_given?
31
+ @series[:series] += AreaChart.new(bindings, data, options, &block).mixed_series
26
32
  end
27
33
 
28
34
  def bar_chart data, options={}, &block
29
- @mixed_series[:series] += BarChart.new(data, options, &block).mixed_series
35
+ bindings = eval("self", block.binding) if block_given?
36
+ @series[:series] += BarChart.new(bindings, data, options, &block).mixed_series
30
37
  end
31
38
 
32
39
  def column_chart data, options={}, &block
33
- @mixed_series[:series] += ColumnChart.new(data, options, &block).mixed_series
40
+ bindings = eval("self", block.binding) if block_given?
41
+ @series[:series] += ColumnChart.new(bindings, data, options, &block).mixed_series
34
42
  end
35
43
 
36
44
  def scatter_chart data, options={}, &block
37
- @mixed_series[:series] += ScatterChart.new(data, options, &block).mixed_series
45
+ bindings = eval("self", block.binding) if block_given?
46
+ @series[:series] += ScatterChart.new(bindings, data, options, &block).mixed_series
38
47
  end
39
48
 
40
49
  def render
@@ -66,7 +75,29 @@ module Apexcharts
66
75
  end
67
76
 
68
77
  def build_options(x_sample, options)
69
- Apexcharts::OptionsBuilder.new(x_sample, options).built
78
+ Apexcharts::OptionsBuilder.new(x_sample, options).build_options
79
+ end
80
+
81
+ def brush?
82
+ @options[:chart][:brush]&.[](:enabled) && \
83
+ !@options[:chart][:selection]&.[](:xaxis)
84
+ end
85
+
86
+ def get_selection_range
87
+ first_x = @series[:series].last[:data].first[:x]
88
+ last_x = @series[:series].last[:data].last[:x]
89
+ @options[:chart][:selection][:xaxis] = {
90
+ min: handle_time(twenty_percent_before_last_x(first_x, last_x)),
91
+ max: handle_time(last_x)
92
+ }
93
+ end
94
+
95
+ def twenty_percent_before_last_x(first, last)
96
+ last - (0.2 * (last - first))
97
+ end
98
+
99
+ def handle_time(input)
100
+ Utils::DateTime.convert(input)
70
101
  end
71
102
  end
72
103
  end
@@ -14,8 +14,8 @@ module Apexcharts
14
14
  end
15
15
 
16
16
  def render
17
- attributes = @options.delete(:div)
18
- variable = attributes.delete(:var) || "chart#{attributes[:id][/\d+/]}"
17
+ attributes = @options.delete(:div) { {} }
18
+ variable = attributes.delete(:var) { "chart#{attributes[:id]&.[](/\d+/)}" }
19
19
  element_id = attributes.delete(:id)
20
20
  css_class = attributes.delete(:class)
21
21
  height = "#{@options[:chart][:height].to_i}px"
@@ -36,7 +36,7 @@ module Apexcharts
36
36
  end
37
37
 
38
38
  def build_options(options)
39
- Apexcharts::OptionsBuilder.new(nil, options).built
39
+ Apexcharts::OptionsBuilder.new(nil, options).build_options
40
40
  end
41
41
  end
42
42
  end
@@ -1,10 +1,12 @@
1
1
  module Apexcharts
2
2
  class SyncingChart
3
- def initialize options={}, bindings, &block
3
+ def initialize bindings, options={}, &block
4
4
  @bindings = bindings
5
5
  @html = ""
6
6
  build_instance_variables
7
7
  @options = options
8
+ @options[:group] ||= apexcharts_group
9
+
8
10
  instance_eval &block
9
11
 
10
12
  @options[:annotations] = @annotations if @annotations
@@ -12,33 +14,38 @@ module Apexcharts
12
14
 
13
15
  def line_chart data, options={}, &block
14
16
  options[:id] = apexcharts_id
15
- @html += LineChart.new(data, @options.merge(options), &block).render
17
+ bindings = eval("self", block.binding) if block_given?
18
+ @html += LineChart.new(bindings, data, @options.merge(options), &block).render
16
19
  end
17
20
 
18
21
  def area_chart data, options={}, &block
19
22
  options[:id] = apexcharts_id
20
- @html += AreaChart.new(data, @options.merge(options), &block).render
23
+ bindings = eval("self", block.binding) if block_given?
24
+ @html += AreaChart.new(bindings, data, @options.merge(options), &block).render
21
25
  end
22
26
 
23
27
  def bar_chart data, options={}, &block
24
28
  options[:id] = apexcharts_id
25
- @html += BarChart.new(data, @options.merge(options), &block).render
29
+ bindings = eval("self", block.binding) if block_given?
30
+ @html += BarChart.new(bindings, data, @options.merge(options), &block).render
26
31
  end
27
32
 
28
33
  def column_chart data, options={}, &block
29
34
  options[:id] = apexcharts_id
30
- @html += ColumnChart.new(data, @options.merge(options), &block).render
35
+ bindings = eval("self", block.binding) if block_given?
36
+ @html += ColumnChart.new(bindings, data, @options.merge(options), &block).render
31
37
  end
32
38
 
33
39
  def scatter_chart data, options={}, &block
34
40
  options[:id] = apexcharts_id
35
- @html += ScatterChart.new(data, @options.merge(options), &block).render
41
+ bindings = eval("self", block.binding) if block_given?
42
+ @html += ScatterChart.new(bindings, data, @options.merge(options), &block).render
36
43
  end
37
44
 
38
45
  def mixed_chart options={}, &block
39
46
  options[:id] = apexcharts_id
40
47
  bindings = eval "self", block.binding
41
- @html += MixedChart.new(@options.merge(options), bindings, &block).render
48
+ @html += MixedChart.new(bindings, @options.merge(options), &block).render
42
49
  end
43
50
  alias_method :combo_chart, :mixed_chart
44
51
 
@@ -6,59 +6,52 @@ require_relative 'charts'
6
6
  module Apexcharts
7
7
  module Helper
8
8
  def line_chart series, options={}, &block
9
- series, options = prepare_series_and_options(series, options)
10
- draw_chart(LineChart.new(series, options, &block))
9
+ bindings = eval("self", block.binding) if block_given?
10
+ draw_chart(LineChart.new(bindings, *prepare_series_and_options(series, options), &block))
11
11
  end
12
12
 
13
13
  def area_chart series, options={}, &block
14
- series, options = prepare_series_and_options(series, options)
15
- draw_chart(AreaChart.new(series, options, &block))
14
+ bindings = eval("self", block.binding) if block_given?
15
+ draw_chart(AreaChart.new(bindings, *prepare_series_and_options(series, options), &block))
16
16
  end
17
17
 
18
18
  def column_chart series, options={}, &block
19
- series, options = prepare_series_and_options(series, options)
20
- draw_chart(ColumnChart.new(series, options, &block))
19
+ bindings = eval("self", block.binding) if block_given?
20
+ draw_chart(ColumnChart.new(bindings, *prepare_series_and_options(series, options), &block))
21
21
  end
22
22
 
23
23
  def bar_chart series, options={}, &block
24
- series, options = prepare_series_and_options(series, options)
25
- draw_chart(BarChart.new(series, options, &block))
24
+ bindings = eval("self", block.binding) if block_given?
25
+ draw_chart(BarChart.new(bindings, *prepare_series_and_options(series, options), &block))
26
26
  end
27
27
 
28
28
  def scatter_chart series, options={}, &block
29
- series, options = prepare_series_and_options(series, options)
30
- draw_chart(ScatterChart.new(series, options, &block))
29
+ bindings = eval("self", block.binding) if block_given?
30
+ draw_chart(ScatterChart.new(bindings, *prepare_series_and_options(series, options), &block))
31
31
  end
32
32
 
33
33
  def mixed_chart options={}, &block
34
- options = deep_copy(options)
35
- options[:id] ||= apexcharts_id
36
- bindings = eval "self", block.binding
37
- draw_chart(MixedChart.new(options, bindings, &block))
34
+ bindings = eval("self", block.binding)
35
+ draw_chart(MixedChart.new(bindings, deep_copy(options), &block))
38
36
  end
39
37
  alias_method :combo_chart, :mixed_chart
40
38
 
41
39
  def syncing_chart options={}, &block
42
- options = deep_copy(options)
43
- options[:group] ||= apexcharts_group
44
- bindings = eval "self", block.binding
45
- draw_chart(SyncingChart.new(options, bindings, &block))
40
+ bindings = eval("self", block.binding)
41
+ draw_chart(SyncingChart.new(bindings, deep_copy(options), &block))
46
42
  end
47
43
  alias_method :synchronized_chart, :syncing_chart
48
44
 
49
45
  def pie_chart series, options={}
50
- series, options = prepare_series_and_options(series, options)
51
- draw_chart(PieChart.new(series, options))
46
+ draw_chart(PieChart.new(*prepare_series_and_options(series, options)))
52
47
  end
53
48
 
54
49
  def donut_chart series, options={}
55
- series, options = prepare_series_and_options(series, options)
56
- draw_chart(DonutChart.new(series, options))
50
+ draw_chart(DonutChart.new(*prepare_series_and_options(series, options)))
57
51
  end
58
52
 
59
53
  def radial_bar_chart series, options={}
60
- series, options = prepare_series_and_options(series, options)
61
- draw_chart(RadialBarChart.new(series, options))
54
+ draw_chart(RadialBarChart.new(*prepare_series_and_options(series, options)))
62
55
  end
63
56
  alias_method :circle_chart, :radial_bar_chart
64
57
 
@@ -7,6 +7,7 @@ module Apexcharts
7
7
  class RootOptions < ::SmartKv
8
8
  optional *%i[
9
9
  annotations
10
+ brush_target
10
11
  chart
11
12
  chart_id
12
13
  colors
@@ -30,8 +30,6 @@ module Apexcharts
30
30
  @xtype = Utils::DateTime.type(x_sample)
31
31
  end
32
32
  @built = {}
33
- build_options
34
- @built.compact!
35
33
  end
36
34
 
37
35
  def build_options
@@ -57,6 +55,7 @@ module Apexcharts
57
55
  build_tooltip
58
56
  build_xaxis
59
57
  build_yaxis
58
+ built.compact
60
59
  end
61
60
 
62
61
  def build_div
@@ -76,10 +75,24 @@ module Apexcharts
76
75
  end
77
76
 
78
77
  def build_chart
79
- @built[:chart] = {
78
+ @built[:chart] = if target = @options.delete(:brush_target)
79
+ {
80
+ brush: {
81
+ enabled: true,
82
+ target: target.to_s
83
+ },
84
+ selection: {
85
+ enabled: true
86
+ }
87
+ }
88
+ else
89
+ {}
90
+ end
91
+
92
+ @built[:chart].merge!({
80
93
  id: @options[:chart_id] || @options[:id],
81
94
  group: @options.delete(:group),
82
- height: @options.delete(:height) { 400 },
95
+ height: @options.delete(:height) { target ? 180 : 400 },
83
96
  width: @options.delete(:width),
84
97
  stacked: @options.delete(:stacked),
85
98
  animations: enabled(@options.delete(:animations)),
@@ -88,7 +101,7 @@ module Apexcharts
88
101
  fore_color: @options.delete(:fore_color) do
89
102
  @options.delete(:foreColor)
90
103
  end
91
- }.compact
104
+ }.compact)
92
105
 
93
106
  chart = @options.delete :chart
94
107
  if chart.is_a? Hash
@@ -103,8 +116,8 @@ module Apexcharts
103
116
  return if data_labels.nil?
104
117
  @built[:data_labels] = if [true, false].include? data_labels
105
118
  {enabled: data_labels}
106
- elsif options.is_a? Hash
107
- DataLabelsOptions.check options.compact
119
+ elsif data_labels.is_a? Hash
120
+ DataLabelsOptions.check data_labels.compact
108
121
  end
109
122
  end
110
123
 
@@ -185,14 +198,6 @@ module Apexcharts
185
198
  end
186
199
 
187
200
  def build_states
188
- def filter_type_hash(state)
189
- if state.is_a? String
190
- {filter: {type: state}}
191
- elsif state.is_a? Hash
192
- state.compact
193
- end
194
- end
195
-
196
201
  @built[:states] = {
197
202
  normal: filter_type_hash(@options.delete :normal),
198
203
  hover: filter_type_hash(@options.delete :hover),
@@ -322,5 +327,13 @@ module Apexcharts
322
327
  options.compact
323
328
  end
324
329
  end
330
+
331
+ def filter_type_hash(state)
332
+ if state.is_a? String
333
+ {filter: {type: state}}
334
+ elsif state.is_a? Hash
335
+ state.compact
336
+ end
337
+ end
325
338
  end
326
339
  end
@@ -3,6 +3,7 @@ module Apexcharts
3
3
  attr_reader :sanitized
4
4
 
5
5
  def initialize(data)
6
+ data = deep_copy(data)
6
7
  if data.is_a?(Array)
7
8
  if (data.to_h rescue nil)
8
9
  @sanitized = [data.to_h]
@@ -19,6 +20,10 @@ module Apexcharts
19
20
 
20
21
  @sanitized = {series: sanitized.each {|a| a[:data] = a[:data].map{|k,v| {x: k, y: v} } } }
21
22
  end
23
+
24
+ def deep_copy(data)
25
+ Marshal.load(Marshal.dump(data))
26
+ end
22
27
  end
23
28
  end
24
29
 
@@ -4,6 +4,7 @@ module Apexcharts
4
4
 
5
5
  def initialize(data)
6
6
  @labels = []
7
+ data = deep_copy(data)
7
8
  if data.is_a? Array
8
9
  if data.first.is_a? Numeric
9
10
  @series = data
@@ -22,6 +23,10 @@ module Apexcharts
22
23
  series: @series
23
24
  }.compact
24
25
  end
26
+
27
+ def deep_copy(data)
28
+ Marshal.load(Marshal.dump(data))
29
+ end
25
30
  end
26
31
  end
27
32
 
@@ -1,8 +1,8 @@
1
1
  require 'date'
2
2
 
3
3
  module Apexcharts::Utils
4
- class DateTime
5
- def self.convert input
4
+ module DateTime
5
+ def convert input
6
6
  case input
7
7
  when Time
8
8
  (input.to_f * 1000).round
@@ -18,8 +18,9 @@ module Apexcharts::Utils
18
18
  rescue
19
19
  input
20
20
  end
21
+ module_function :convert
21
22
 
22
- def self.convert_range input
23
+ def convert_range input
23
24
  case input.first
24
25
  when Time
25
26
  ((input.first.to_f * 1000).round..(input.last.to_f * 1000).round)
@@ -35,13 +36,14 @@ module Apexcharts::Utils
35
36
  rescue
36
37
  input
37
38
  end
39
+ module_function :convert_range
38
40
 
39
- def self.type input
41
+ def type input
40
42
  case input
41
- when Time, DateTime, Date
43
+ when Time, ::DateTime, Date
42
44
  'datetime'
43
45
  else
44
- if DateTime.iso8601(input).iso8601 == input
46
+ if ::DateTime.iso8601(input).iso8601 == input
45
47
  'datetime'
46
48
  elsif Date.iso8601(input).iso8601 == input
47
49
  'datetime'
@@ -52,5 +54,6 @@ module Apexcharts::Utils
52
54
  rescue
53
55
  'number'
54
56
  end
57
+ module_function :type
55
58
  end
56
59
  end
@@ -1,3 +1,3 @@
1
1
  module Apexcharts
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apexcharts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Setyadi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-18 00:00:00.000000000 Z
11
+ date: 2019-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: smart_kv
@@ -93,10 +93,10 @@ files:
93
93
  - lib/apexcharts/charts.rb
94
94
  - lib/apexcharts/charts/area.rb
95
95
  - lib/apexcharts/charts/bar.rb
96
- - lib/apexcharts/charts/capabilities/annotations.rb
97
96
  - lib/apexcharts/charts/cartesian.rb
98
97
  - lib/apexcharts/charts/column.rb
99
98
  - lib/apexcharts/charts/donut.rb
99
+ - lib/apexcharts/charts/features/annotations.rb
100
100
  - lib/apexcharts/charts/line.rb
101
101
  - lib/apexcharts/charts/mixed.rb
102
102
  - lib/apexcharts/charts/pie.rb