apexcharts 0.1.3 → 0.1.4

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: 911c4f7abf6adcf9da0230826af3f658b6281a6ed2e5a56079240c4a124e0e3d
4
- data.tar.gz: 865dbe2ef98854e98c6a66af3c6e81195a1124dcfd85117a3d38cac5c5d39e32
3
+ metadata.gz: a866b4e6545c50cf331fe31f383f8493ee2caf991e7e034654a011cd41d824ac
4
+ data.tar.gz: fd243a66fa09eb5ccbb7c9255dd536e6948f7b0f22d496986cd2788d966aec31
5
5
  SHA512:
6
- metadata.gz: 70632205846e3a684dc892bb047fac617b92e9eba36173d53ac7da298e84c401d0b73dce3d628f451533aae024fd2972b89f120d6698e0d49a3d4cd915e19246
7
- data.tar.gz: 17524c53370e02d38af478600d18455d979821d97cf6a45d01f6a2bb0ac3c0ead50f58e0264dadb9cf5ed0dbe55a9d9c3ea27ef2dc59b38749651d3b663537e1
6
+ metadata.gz: 40c1bf094d88f7bc524fefab6be0dc424454f552a490e83cd58df176dd236cdf799681a0e46af74548f176761681a6e69ab82fd67b7c1f9d2f5286f88b5aa3b1
7
+ data.tar.gz: 0cf54ed3c2d01f5991bdc1578c8a5b2af829fe2f936eb86d76a8d96f19629219119b70eefd56cd7fd25b5c8df0bc04083883825168f3e8f8c01c304f024c8d27
data/README.md CHANGED
@@ -37,7 +37,8 @@ and I'll get the data in this format:
37
37
  ..
38
38
  }
39
39
  ```
40
- PS: `Property` can be any model you have and `inactive` and `active` is just a normal scope.
40
+ PS: `Property` can be any model you have and `inactive` and `active`
41
+ are just some normal ActiveRecord scopes.
41
42
 
42
43
  Example options used for cartesian charts:
43
44
 
@@ -100,9 +101,9 @@ Example options used for cartesian charts:
100
101
  ![Example Scatter Chart](images/scatter_chart.png)
101
102
 
102
103
 
103
- #### Mixed Chart
104
+ #### Mixed Charts
104
105
 
105
- You can mix charts by using `mixed_chart` or `combo_chart` methods. For example:
106
+ You can mix charts by using `mixed_charts` or `combo_charts` methods. For example:
106
107
  Given that:
107
108
  ```ruby
108
109
  @total_properties = Property.group_by_week(:created_at).count
@@ -115,21 +116,21 @@ and
115
116
  ```
116
117
  you can do this:
117
118
  ```erb
118
- <%= combo_chart({**options, theme: 'palette4', stacked: false, data_labels: false}) do %>
119
+ <%= combo_charts({**options, theme: 'palette4', stacked: false, data_labels: false}) do %>
119
120
  <% line_chart(total_series) %>
120
121
  <% area_chart(series.last) %>
121
122
  <% column_chart(series.first) %>
122
123
  <% end %>
123
124
  ```
124
- ![Example Mixed Chart](images/mixed_chart.gif)
125
+ ![Example Mixed Charts](images/mixed_charts.gif)
125
126
 
126
127
 
127
- #### Syncing Chart
128
+ #### Syncing Charts
128
129
 
129
- You can synchronize charts by using `syncing_chart` or `synchronized_chart` methods. For example:
130
+ You can synchronize charts by using `syncing_charts` or `synchronized_charts` methods. For example:
130
131
  ```erb
131
- <%= syncing_chart(chart: {toolbar: false}, height: 250, style: 'display: inline-block; width: 32%;') do %>
132
- <% mixed_chart(theme: 'palette4', data_labels: false) do %>
132
+ <%= syncing_charts(chart: {toolbar: false}, height: 250, style: 'display: inline-block; width: 32%;') do %>
133
+ <% mixed_charts(theme: 'palette4', data_labels: false) do %>
133
134
  <% line_chart({name: "Total", data: @total_properties}) %>
134
135
  <% area_chart({name: "Active", data: @active_properties}) %>
135
136
  <% end %>
@@ -137,7 +138,7 @@ You can synchronize charts by using `syncing_chart` or `synchronized_chart` meth
137
138
  <% line_chart({name: "Inactive", data: @active_properties}, theme: 'palette8') %>
138
139
  <% end %>
139
140
  ```
140
- ![Example Syncing Chart](images/syncing_chart.gif)
141
+ ![Example Syncing Charts](images/syncing_charts.gif)
141
142
 
142
143
 
143
144
  #### Brush Chart
@@ -146,7 +147,7 @@ You can synchronize charts by using `syncing_chart` or `synchronized_chart` meth
146
147
  <%= area_chart(total_series, {
147
148
  **options, chart_id: 'the-chart', xtitle: nil, theme: 'palette2'
148
149
  }) %>
149
- <%= mixed_chart(brush_target: 'the-chart', theme: 'palette7') do %>
150
+ <%= mixed_charts(brush_target: 'the-chart', theme: 'palette7') do %>
150
151
  <% column_chart(series.first) %>
151
152
  <% line_chart(series.last) %>
152
153
  <% end %>
@@ -154,6 +155,36 @@ You can synchronize charts by using `syncing_chart` or `synchronized_chart` meth
154
155
  ![Example Brush Chart](images/brush_chart.gif)
155
156
 
156
157
 
158
+ #### Heatmap Chart
159
+
160
+ ```erb
161
+ <% heatmap_series = 17.downto(10).map do |n|
162
+ {
163
+ name: "#{n}:00",
164
+ data: 15.times.map do |i|
165
+ ["W#{i+1}", rand(90)]
166
+ end.to_h
167
+ }
168
+ end %>
169
+ <%= heatmap_chart(heatmap_series) %>
170
+ ```
171
+ ![Example Heatmap Chart](images/heatmap_chart.png)
172
+
173
+
174
+ #### Bubble Chart
175
+
176
+ ```erb
177
+ <% bubble_series = (1..4).map do |n|
178
+ {
179
+ name: "Bubble#{n}",
180
+ data: 20.times.map{[rand(750),rand(10..60),rand(70)]}
181
+ }
182
+ end %>
183
+ <%= bubble_chart(bubble_series, data_labels: false, theme: 'palette6') %>
184
+ ```
185
+ ![Example Bubble Chart](images/bubble_chart.png)
186
+
187
+
157
188
  #### Annotations
158
189
 
159
190
  All cartesian charts can have annotations, for example:
@@ -4,6 +4,8 @@ require_relative 'charts/area'
4
4
  require_relative 'charts/bar'
5
5
  require_relative 'charts/column'
6
6
  require_relative 'charts/scatter'
7
+ require_relative 'charts/heatmap'
8
+ require_relative 'charts/bubble'
7
9
  require_relative 'charts/mixed'
8
10
  require_relative 'charts/syncing'
9
11
  require_relative 'charts/polar'
@@ -0,0 +1,20 @@
1
+ module Apexcharts
2
+ class BubbleChart < CartesianChart
3
+ def chart_type
4
+ 'bubble'
5
+ end
6
+
7
+ def mixed_series
8
+ end
9
+
10
+ protected
11
+
12
+ def sanitize_data(data)
13
+ Apexcharts::BubbleSeries.new(data).sanitized
14
+ end
15
+
16
+ def x_sample
17
+ @series[:series][0][:data][0][0]
18
+ end
19
+ end
20
+ end
@@ -15,7 +15,7 @@ module Apexcharts
15
15
  @series = sanitize_data(data)
16
16
  @options = Utils::Hash.camelize_keys(
17
17
  Utils::Hash.deep_merge(
18
- build_options(@series[:series][0][:data][0][:x], options),
18
+ build_options(x_sample, options),
19
19
  {**@series, chart: {type: chart_type}}.compact
20
20
  )
21
21
  )
@@ -59,7 +59,7 @@ module Apexcharts
59
59
  end
60
60
  end
61
61
 
62
- private
62
+ protected
63
63
 
64
64
  def build_instance_variables
65
65
  (@bindings.instance_variables - instance_variables).each do |i|
@@ -96,6 +96,10 @@ module Apexcharts
96
96
  def handle_time(input)
97
97
  Utils::DateTime.convert(input)
98
98
  end
99
+
100
+ def x_sample
101
+ @series[:series][0][:data][0][:x]
102
+ end
99
103
  end
100
104
  end
101
105
 
@@ -0,0 +1,10 @@
1
+ module Apexcharts
2
+ class HeatmapChart < CartesianChart
3
+ def chart_type
4
+ 'heatmap'
5
+ end
6
+
7
+ def mixed_series
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module Apexcharts
2
- class MixedChart
2
+ class MixedCharts
3
3
  include Annotations
4
4
 
5
5
  def initialize bindings, options={}, &block
@@ -29,7 +29,7 @@ module Apexcharts
29
29
  HTML
30
30
  end
31
31
 
32
- private
32
+ protected
33
33
 
34
34
  def sanitize_data(data)
35
35
  Apexcharts::PolarSeries.new(data).sanitized
@@ -1,5 +1,5 @@
1
1
  module Apexcharts
2
- class SyncingChart
2
+ class SyncingCharts
3
3
  def initialize bindings, options={}, &block
4
4
  @bindings = bindings
5
5
  @html = ""
@@ -42,12 +42,12 @@ module Apexcharts
42
42
  @html += ScatterChart.new(bindings, data, @options.merge(options), &block).render
43
43
  end
44
44
 
45
- def mixed_chart options={}, &block
45
+ def mixed_charts options={}, &block
46
46
  options[:id] = apexcharts_id
47
47
  bindings = eval "self", block.binding
48
- @html += MixedChart.new(bindings, @options.merge(options), &block).render
48
+ @html += MixedCharts.new(bindings, @options.merge(options), &block).render
49
49
  end
50
- alias_method :combo_chart, :mixed_chart
50
+ alias_method :combo_charts, :mixed_charts
51
51
 
52
52
  def render
53
53
  @html
@@ -30,17 +30,27 @@ module Apexcharts
30
30
  draw_chart(ScatterChart.new(bindings, *prepare_series_and_options(series, options), &block))
31
31
  end
32
32
 
33
- def mixed_chart options={}, &block
33
+ def heatmap_chart series, options={}, &block
34
+ bindings = eval("self", block.binding) if block_given?
35
+ draw_chart(HeatmapChart.new(bindings, *prepare_series_and_options(series, options), &block))
36
+ end
37
+
38
+ def bubble_chart series, options={}, &block
39
+ bindings = eval("self", block.binding) if block_given?
40
+ draw_chart(BubbleChart.new(bindings, *prepare_series_and_options(series, options), &block))
41
+ end
42
+
43
+ def mixed_charts options={}, &block
34
44
  bindings = eval("self", block.binding)
35
- draw_chart(MixedChart.new(bindings, deep_copy(options), &block))
45
+ draw_chart(MixedCharts.new(bindings, deep_copy(options), &block))
36
46
  end
37
- alias_method :combo_chart, :mixed_chart
47
+ alias_method :combo_charts, :mixed_charts
38
48
 
39
- def syncing_chart options={}, &block
49
+ def syncing_charts options={}, &block
40
50
  bindings = eval("self", block.binding)
41
- draw_chart(SyncingChart.new(bindings, deep_copy(options), &block))
51
+ draw_chart(SyncingCharts.new(bindings, deep_copy(options), &block))
42
52
  end
43
- alias_method :synchronized_chart, :syncing_chart
53
+ alias_method :synchronized_charts, :syncing_charts
44
54
 
45
55
  def pie_chart series, options={}
46
56
  draw_chart(PieChart.new(*prepare_series_and_options(series, options)))
@@ -1,2 +1,3 @@
1
1
  require_relative 'series/cartesian'
2
+ require_relative 'series/bubble'
2
3
  require_relative 'series/polar'
@@ -0,0 +1,37 @@
1
+ module Apexcharts
2
+ class BubbleSeries
3
+ attr_reader :sanitized
4
+
5
+ def initialize(data)
6
+ data = deep_copy(data)
7
+ @sanitized = case data
8
+ when Array
9
+ if array_of_threes?(data)
10
+ [{data: data}]
11
+ else
12
+ data
13
+ end
14
+
15
+ when Hash
16
+ if data_value = data[:data]
17
+ if array_of_threes?(data_value)
18
+ [data]
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ @sanitized = {series: @sanitized}
25
+ end
26
+
27
+ def deep_copy(data)
28
+ Marshal.load(Marshal.dump(data))
29
+ end
30
+
31
+ def array_of_threes?(data)
32
+ return false if data.empty?
33
+ data.all?{|d| d.length == 3 }
34
+ end
35
+ end
36
+ end
37
+
@@ -4,26 +4,71 @@ module Apexcharts
4
4
 
5
5
  def initialize(data)
6
6
  data = deep_copy(data)
7
- if data.is_a?(Array)
8
- if (data.to_h rescue nil)
9
- @sanitized = [data.to_h]
10
- else
11
- @sanitized = data.map(&:to_h)
12
- end
13
- else
14
- @sanitized = [data]
15
- end
16
-
17
- unless sanitized[0][:data]
18
- @sanitized = sanitized.map{|a| {data: a} }
19
- end
20
-
21
- @sanitized = {series: sanitized.each {|a| a[:data] = a[:data].map{|k,v| {x: k, y: v} } } }
7
+ @sanitized = case data
8
+ when Array
9
+ if array_of_pairs?(data)
10
+ case first_data = data[0]
11
+ when Array
12
+ [
13
+ {
14
+ data: array_of_array_to_array_of_xy(data)
15
+ }
16
+ ]
17
+
18
+ when Hash
19
+ if first_data[:data]
20
+ data.each{|h| h[:data] = array_of_array_to_array_of_xy(h[:data]) }
21
+ data
22
+ end
23
+
24
+ end
25
+
26
+ else
27
+ data.map(&:to_h)
28
+
29
+ end
30
+
31
+ when Hash
32
+ if data_value = data[:data]
33
+ if array_of_pairs?(data_value)
34
+ data[:data] = array_of_array_to_array_of_xy(data_value)
35
+ [data]
36
+ end
37
+
38
+ else
39
+ if data[:x] && data[:y]
40
+ [{data: [data]}]
41
+
42
+ else
43
+ [
44
+ {
45
+ data: data.map do |k,v|
46
+ {x: k, y: v}
47
+ end
48
+ }
49
+ ]
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ @sanitized = {series: @sanitized}
22
58
  end
23
59
 
24
60
  def deep_copy(data)
25
61
  Marshal.load(Marshal.dump(data))
26
62
  end
63
+
64
+ def array_of_pairs?(data)
65
+ return false if data.empty?
66
+ data.all?{|d| d.length == 2 }
67
+ end
68
+
69
+ def array_of_array_to_array_of_xy(data)
70
+ data.map{|d| {x: d.first, y: d.last} }
71
+ end
27
72
  end
28
73
  end
29
74
 
@@ -1,3 +1,3 @@
1
1
  module Apexcharts
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
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.3
4
+ version: 0.1.4
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-29 00:00:00.000000000 Z
11
+ date: 2019-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: smart_kv
@@ -93,10 +93,12 @@ 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/bubble.rb
96
97
  - lib/apexcharts/charts/cartesian.rb
97
98
  - lib/apexcharts/charts/column.rb
98
99
  - lib/apexcharts/charts/donut.rb
99
100
  - lib/apexcharts/charts/features/annotations.rb
101
+ - lib/apexcharts/charts/heatmap.rb
100
102
  - lib/apexcharts/charts/line.rb
101
103
  - lib/apexcharts/charts/mixed.rb
102
104
  - lib/apexcharts/charts/pie.rb
@@ -127,6 +129,7 @@ files:
127
129
  - lib/apexcharts/options/y_axis.rb
128
130
  - lib/apexcharts/options_builder.rb
129
131
  - lib/apexcharts/series.rb
132
+ - lib/apexcharts/series/bubble.rb
130
133
  - lib/apexcharts/series/cartesian.rb
131
134
  - lib/apexcharts/series/polar.rb
132
135
  - lib/apexcharts/support/rails.rb