apexcharts 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +231 -32
  3. data/lib/apexcharts.rb +3 -1
  4. data/lib/apexcharts/charts.rb +5 -0
  5. data/lib/apexcharts/charts/area.rb +3 -1
  6. data/lib/apexcharts/charts/bar.rb +3 -1
  7. data/lib/apexcharts/charts/base.rb +34 -0
  8. data/lib/apexcharts/charts/bubble.rb +7 -8
  9. data/lib/apexcharts/charts/candlestick.rb +9 -0
  10. data/lib/apexcharts/charts/cartesian.rb +15 -39
  11. data/lib/apexcharts/charts/column.rb +1 -1
  12. data/lib/apexcharts/charts/donut.rb +3 -1
  13. data/lib/apexcharts/charts/features/annotations.rb +1 -1
  14. data/lib/apexcharts/charts/features/mixable.rb +11 -0
  15. data/lib/apexcharts/charts/heatmap.rb +8 -3
  16. data/lib/apexcharts/charts/line.rb +3 -1
  17. data/lib/apexcharts/charts/mixed.rb +7 -25
  18. data/lib/apexcharts/charts/pie.rb +3 -1
  19. data/lib/apexcharts/charts/polar.rb +4 -33
  20. data/lib/apexcharts/charts/radar.rb +15 -0
  21. data/lib/apexcharts/charts/radial_bar.rb +3 -1
  22. data/lib/apexcharts/charts/scatter.rb +3 -1
  23. data/lib/apexcharts/charts/syncing.rb +3 -1
  24. data/lib/apexcharts/helper.rb +17 -6
  25. data/lib/apexcharts/options/annotations.rb +1 -1
  26. data/lib/apexcharts/options/axis.rb +4 -4
  27. data/lib/apexcharts/options/chart.rb +9 -7
  28. data/lib/apexcharts/options/data_labels.rb +5 -5
  29. data/lib/apexcharts/options/div_attributes.rb +1 -1
  30. data/lib/apexcharts/options/fill.rb +1 -1
  31. data/lib/apexcharts/options/grid.rb +3 -3
  32. data/lib/apexcharts/options/legend.rb +13 -13
  33. data/lib/apexcharts/options/markers.rb +7 -7
  34. data/lib/apexcharts/options/no_data.rb +4 -4
  35. data/lib/apexcharts/options/plot_options.rb +3 -2
  36. data/lib/apexcharts/options/root.rb +6 -6
  37. data/lib/apexcharts/options/states.rb +1 -1
  38. data/lib/apexcharts/options/stroke.rb +3 -3
  39. data/lib/apexcharts/options/subtitle.rb +1 -1
  40. data/lib/apexcharts/options/theme.rb +1 -1
  41. data/lib/apexcharts/options/title.rb +3 -3
  42. data/lib/apexcharts/options/tooltip.rb +5 -5
  43. data/lib/apexcharts/options/x_axis.rb +2 -2
  44. data/lib/apexcharts/options/y_axis.rb +5 -5
  45. data/lib/apexcharts/options_builder.rb +33 -32
  46. data/lib/apexcharts/renderer.rb +21 -0
  47. data/lib/apexcharts/series.rb +2 -0
  48. data/lib/apexcharts/series/bubble.rb +1 -1
  49. data/lib/apexcharts/series/cartesian.rb +14 -23
  50. data/lib/apexcharts/series/polar.rb +1 -1
  51. data/lib/apexcharts/support/rails.rb +3 -1
  52. data/lib/apexcharts/utils.rb +2 -0
  53. data/lib/apexcharts/utils/date_time.rb +1 -1
  54. data/lib/apexcharts/utils/hash.rb +7 -5
  55. data/lib/apexcharts/version.rb +4 -2
  56. metadata +52 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a866b4e6545c50cf331fe31f383f8493ee2caf991e7e034654a011cd41d824ac
4
- data.tar.gz: fd243a66fa09eb5ccbb7c9255dd536e6948f7b0f22d496986cd2788d966aec31
3
+ metadata.gz: 430b940ba031ce379c1d35a6685ba8ad19d62922c91d9d8a806779f1c4282bbd
4
+ data.tar.gz: 5d1bae083b7c949b1d0a984bcb43e64096558cdcf550df3b8a507eec58e64405
5
5
  SHA512:
6
- metadata.gz: 40c1bf094d88f7bc524fefab6be0dc424454f552a490e83cd58df176dd236cdf799681a0e46af74548f176761681a6e69ab82fd67b7c1f9d2f5286f88b5aa3b1
7
- data.tar.gz: 0cf54ed3c2d01f5991bdc1578c8a5b2af829fe2f936eb86d76a8d96f19629219119b70eefd56cd7fd25b5c8df0bc04083883825168f3e8f8c01c304f024c8d27
6
+ metadata.gz: fc51493d941efd81893b7a847e7939149897b0c8c44f87a04a4d00f5466d5c629862353aad59e7a3df0bcab768926e674bb2f3a00b1b2adf6f987b324a540002
7
+ data.tar.gz: 272c361ad29c2b5456d62b7339809cbd1e664de015275eecd043127211627732b9ce50df5f68df2b5ce8d11a316fc208037f7a8bd4ac4383aaedd25712449bec
data/README.md CHANGED
@@ -9,6 +9,13 @@
9
9
  </p>
10
10
 
11
11
 
12
+ ## What is this?
13
+
14
+ ApexCharts.rb is a ruby gem that wraps a JavaScript charting library called with the same name,
15
+ [apexcharts.js](https://github.com/apexcharts/apexcharts.js), that's going to give you
16
+ a beautiful, interactive, and responsive charts for your ruby app.
17
+
18
+
12
19
  ## Usage
13
20
 
14
21
  ### Cartesian Charts
@@ -37,8 +44,9 @@ and I'll get the data in this format:
37
44
  ..
38
45
  }
39
46
  ```
40
- PS: `Property` can be any model you have and `inactive` and `active`
41
- are just some normal ActiveRecord scopes.
47
+ PS: `Property` can be any model you have and `inactive` and `active`
48
+ are just some normal ActiveRecord scopes. Keep scrolling down to see
49
+ accepted data formats.
42
50
 
43
51
  Example options used for cartesian charts:
44
52
 
@@ -58,7 +66,7 @@ Example options used for cartesian charts:
58
66
  ```erb
59
67
  <%= line_chart(series, options) %>
60
68
  ```
61
- ![Example Line Chart](images/line_chart.png)
69
+ ![Example Line Chart](images/line_chart.gif)
62
70
 
63
71
 
64
72
  #### Stepline Chart
@@ -66,7 +74,7 @@ Example options used for cartesian charts:
66
74
  ```erb
67
75
  <%= line_chart(series, {**options, theme: 'palette7', curve: 'stepline'}) %>
68
76
  ```
69
- ![Example Stepline Chart](images/stepline_chart.png)
77
+ ![Example Stepline Chart](images/stepline_chart.gif)
70
78
 
71
79
 
72
80
  #### Area Chart
@@ -74,7 +82,7 @@ Example options used for cartesian charts:
74
82
  ```erb
75
83
  <%= area_chart(series, {**options, theme: 'palette5'}) %>
76
84
  ```
77
- ![Example Area Chart](images/area_chart.png)
85
+ ![Example Area Chart](images/area_chart.gif)
78
86
 
79
87
 
80
88
  #### Column Chart
@@ -82,7 +90,7 @@ Example options used for cartesian charts:
82
90
  ```erb
83
91
  <%= column_chart(series, {**options, theme: 'palette4'}) %>
84
92
  ```
85
- ![Example Column Chart](images/column_chart.png)
93
+ ![Example Column Chart](images/column_chart.gif)
86
94
 
87
95
 
88
96
  #### Bar Chart
@@ -90,7 +98,7 @@ Example options used for cartesian charts:
90
98
  ```erb
91
99
  <%= bar_chart(series, {**options, xtitle: 'Properties', ytitle: 'Week', height: 800, theme: 'palette7'}) %>
92
100
  ```
93
- ![Example Bar Chart](images/bar_chart.png)
101
+ ![Example Bar Chart](images/bar_chart.gif)
94
102
 
95
103
 
96
104
  #### Scatter Chart
@@ -98,13 +106,54 @@ Example options used for cartesian charts:
98
106
  ```erb
99
107
  <%= scatter_chart(series, {**options, theme: 'palette3'}) %>
100
108
  ```
101
- ![Example Scatter Chart](images/scatter_chart.png)
109
+ ![Example Scatter Chart](images/scatter_chart.gif)
110
+
111
+
112
+ #### Candlestick Chart
113
+ Candlestick chart is typically used to illustrate movements in the price of a
114
+ financial instrument over time. This chart is also popular by the name "ohlc chart".
115
+ That's why you can call it with `ohlc_chart` too.
116
+ So, here's how you make it.
117
+
118
+ Given:
119
+ ```erb
120
+ <%
121
+ require 'date'
122
+
123
+ def ohlc(ary)
124
+ [rand(ary.min..ary.max), ary.max, ary.min, rand(ary.min..ary.max)]
125
+ end
126
+
127
+ candlestick_data = 50.times.map do |i|
128
+ [Date.today - 50 + i, ohlc(Array.new(2){ rand(6570..6650) })]
129
+ end.to_h
130
+
131
+ candlestick_options = {
132
+ plot_options: {
133
+ candlestick: {
134
+ colors: {
135
+ upward: '#3C90EB',
136
+ downward: '#DF7D46'
137
+ }
138
+ }
139
+ }
140
+ }
141
+ %>
142
+ ```
143
+ You can make candlestick chart with this:
144
+ ```erb
145
+ <%= candlestick_chart(candlestick_data, candlestick_options) %>
146
+ ```
147
+ ![Example Candlestick Chart](images/candlestick_chart.gif)
148
+
149
+ Real life candlestick chart probably don't look like that.
150
+ That's because I just use random sets of numbers as the data.
102
151
 
103
152
 
104
153
  #### Mixed Charts
105
154
 
106
- You can mix charts by using `mixed_charts` or `combo_charts` methods. For example:
107
- Given that:
155
+ You can mix charts by using `mixed_charts` or `combo_charts` methods.
156
+ For example, given that:
108
157
  ```ruby
109
158
  @total_properties = Property.group_by_week(:created_at).count
110
159
  ```
@@ -155,7 +204,21 @@ You can synchronize charts by using `syncing_charts` or `synchronized_charts` me
155
204
  ![Example Brush Chart](images/brush_chart.gif)
156
205
 
157
206
 
158
- #### Heatmap Chart
207
+ #### Annotations
208
+
209
+ All cartesian charts can have annotations, for example:
210
+
211
+ ```erb
212
+ <%= area_chart(series, {**options, theme: 'palette9'}) do %>
213
+ <% x_annotation(value: ('2019-01-06'..'2019-02-24'), text: "Busy Time", color: 'green') %>
214
+ <% y_annotation(value: 29, text: "Max Properties", color: 'blue') %>
215
+ <% point_annotation(value: ['2018-10-07', 24], text: "First Peak", color: 'magenta') %>
216
+ <% end %>
217
+ ```
218
+ ![Example Area Chart with Annotations](images/chart_with_annotations.gif)
219
+
220
+
221
+ ### Heatmap Chart
159
222
 
160
223
  ```erb
161
224
  <% heatmap_series = 17.downto(10).map do |n|
@@ -171,7 +234,28 @@ end %>
171
234
  ![Example Heatmap Chart](images/heatmap_chart.png)
172
235
 
173
236
 
174
- #### Bubble Chart
237
+ ### Radar Chart
238
+
239
+ ```erb
240
+ <% radar_series = [
241
+ {
242
+ name: "What it should be",
243
+ data: { "Code review"=>10, "Issues"=>5, "Pull request"=>25, "Commits"=>60 }
244
+ },
245
+ {
246
+ name: "What it really is",
247
+ data: { "Code review"=>1, "Issues"=>3, "Pull request"=>7, "Commits"=>89 }
248
+ }
249
+ ] %>
250
+ <%= radar_chart(
251
+ radar_series,
252
+ {title: "GitHub Radar", markers: {size: 4}, theme: 'palette4'}
253
+ ) %>
254
+ ```
255
+ ![Example Radar Chart](images/radar_chart.gif)
256
+
257
+
258
+ ### Bubble Chart
175
259
 
176
260
  ```erb
177
261
  <% bubble_series = (1..4).map do |n|
@@ -185,20 +269,6 @@ end %>
185
269
  ![Example Bubble Chart](images/bubble_chart.png)
186
270
 
187
271
 
188
- #### Annotations
189
-
190
- All cartesian charts can have annotations, for example:
191
-
192
- ```erb
193
- <%= area_chart(series, {**options, theme: 'palette9'}) do %>
194
- <% x_annotation(value: ('2019-01-06'..'2019-02-24'), text: "Busy Time", color: 'green') %>
195
- <% y_annotation(value: 29, text: "Max Properties", color: 'blue') %>
196
- <% point_annotation(value: ['2018-10-07', 24], text: "First Peak", color: 'magenta') %>
197
- <% end %>
198
- ```
199
- ![Example Area Chart with Annotations](images/chart_with_annotations.png)
200
-
201
-
202
272
  ### Polar Charts
203
273
 
204
274
  #### Pie Chart
@@ -217,7 +287,7 @@ All cartesian charts can have annotations, for example:
217
287
  #### Donut Chart
218
288
 
219
289
  ```erb
220
- <%= donut_chart([25, 100, 200, 125], theme: 'palette4' %>
290
+ <%= donut_chart([25, 100, 200, 125], theme: 'palette4') %>
221
291
  ```
222
292
  ![Example Pie Chart](images/donut_chart.gif)
223
293
 
@@ -236,10 +306,121 @@ Also called `circle_chart`.
236
306
  ![Example Circle Chart](images/radial_bar_chart.gif)
237
307
 
238
308
 
309
+ ## Data Formats
310
+
311
+ ### Cartesian Charts
312
+ The data format for line, stepline, area, column, bar, and scatter
313
+ charts should be in following format **per-series**:
314
+
315
+ ```ruby
316
+ {
317
+ <x value> => <y value>,
318
+ <x value> => <y value>,
319
+ ...
320
+ }
321
+ ```
322
+
323
+ or this:
324
+
325
+ ```ruby
326
+ [
327
+ [<x value>, <y value>],
328
+ [<x value>, <y value>],
329
+ ...
330
+ ]
331
+ ```
332
+
333
+ #### Candlestick Chart
334
+ Candlestick chart is just like other cartesian charts, only the y value is
335
+ an array of 4 members which called the OHLC (Open-High-Low-Close):
336
+
337
+ ```ruby
338
+ {
339
+ <x value> => [<Open>, <High>, <Low>, <Close>],
340
+ <x value> => [<Open>, <High>, <Low>, <Close>],
341
+ ...
342
+ }
343
+ ```
344
+
345
+ or this:
346
+
347
+ ```ruby
348
+ [
349
+ [<x value>, [<Open>, <High>, <Low>, <Close>]],
350
+ [<x value>, [<Open>, <High>, <Low>, <Close>]],
351
+ ...
352
+ ]
353
+ ```
354
+
355
+ ### Heatmap Chart
356
+ The data format for heatmap chart **per-series** is similar to cartesian
357
+ charts. But instead of y values they are heat values. The series names will
358
+ be the y values.
359
+
360
+ ```ruby
361
+ {
362
+ <x value> => <heat value>,
363
+ <x value> => <heat value>,
364
+ ...
365
+ }
366
+ ```
367
+
368
+ or this:
369
+
370
+ ```ruby
371
+ [
372
+ [<x value>, <heat value>],
373
+ [<x value>, <heat value>],
374
+ ...
375
+ ]
376
+ ```
377
+
378
+ ### Radar Chart
379
+ The data format for radar chart **per-series** is also similar but instead
380
+ of x values they are variables and instead of y values they are the only
381
+ values for the variables with type of Numeric.
382
+
383
+ ```ruby
384
+ {
385
+ <variable> => <value>,
386
+ <variable> => <value>,
387
+ ...
388
+ }
389
+ ```
390
+
391
+ or this:
392
+
393
+ ```ruby
394
+ [
395
+ [<variable>, <value>],
396
+ [<variable>, <value>],
397
+ ...
398
+ ]
399
+ ```
400
+
401
+
402
+ ### Bubble Chart
403
+ Bubble chart is similar to scatter chart, only they have one more value
404
+ for bubble size:
405
+
406
+ ```ruby
407
+ [
408
+ [<x value>, <bubble size>, <y value>],
409
+ [<x value>, <bubble size>, <y value>],
410
+ ...
411
+ ]
412
+ ```
413
+
414
+ ### Polar Charts
415
+ The data format for donut, pie, and radial bar are the simplest. They are
416
+ just any single value of type Numeric.
417
+
418
+
239
419
  ## Installation
240
420
  Add this line to your application's Gemfile:
241
421
 
242
422
  ```ruby
423
+ gem 'groupdate' # optional
243
424
  gem 'apexcharts'
244
425
  ```
245
426
 
@@ -252,7 +433,6 @@ $ bundle
252
433
  ## Web Support
253
434
 
254
435
  ### Rails
255
-
256
436
  After installing the gem, require it in your `app/assets/javascripts/application.js`.
257
437
  ```js
258
438
  //= require 'apexcharts'
@@ -267,13 +447,22 @@ and then require it in your `app/javascript/packs/application.js`.
267
447
  require("apexcharts")
268
448
  ```
269
449
 
270
- ## Objective
271
- - To bring out as much apexcharts.js capabilities as possible but in ruby ways.
450
+ ### HTML+ERB
451
+ After installing the gem, insert this to the top of your .html.erb files:
452
+
453
+ ```html+erb
454
+ <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
455
+ <% require 'apexcharts' %>
456
+ ```
457
+
272
458
 
273
459
  ## TODOs
274
- - Other charts (radar, heatmap, candlestick, etc.)
460
+ - Replace dependency `smart_kv` with `dry-schema`
461
+ - Create reusable theme palette
462
+ - Add more features (e.g. gradient line, background image, etc.)
463
+ - Range bar chart
275
464
  - Support other ruby frameworks (sinatra, hanami, etc.)
276
- - Render as Vue or React elements
465
+
277
466
 
278
467
  ## Contributing
279
468
  Everyone is encouraged to help improve this project by:
@@ -281,6 +470,16 @@ Everyone is encouraged to help improve this project by:
281
470
  - Fixing bugs and submiting pull requests
282
471
  - Fixing documentation
283
472
  - Suggesting new features
473
+ - Implementing TODOs above
474
+
284
475
 
285
476
  ## License
286
477
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
478
+
479
+
480
+ ## Like the charts?
481
+ Consider donating to the author of apexcharts.js to support his awesome library.
482
+ This project wouldn't be possible without it.
483
+
484
+ Become a sponsor on [Patreon](https://patreon.com/junedchhipa).
485
+ One time donation on [PayPal](https://paypal.me/junedchhipa)
@@ -1,2 +1,4 @@
1
- require 'apexcharts/support/rails' if defined?(Rails)
1
+ # frozen_string_literal: true
2
+
3
+ require 'apexcharts/support/rails' if defined? Rails
2
4
  require 'apexcharts/helper'
@@ -1,11 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'charts/base'
1
4
  require_relative 'charts/cartesian'
2
5
  require_relative 'charts/line'
3
6
  require_relative 'charts/area'
4
7
  require_relative 'charts/bar'
5
8
  require_relative 'charts/column'
6
9
  require_relative 'charts/scatter'
10
+ require_relative 'charts/candlestick'
7
11
  require_relative 'charts/heatmap'
8
12
  require_relative 'charts/bubble'
13
+ require_relative 'charts/radar'
9
14
  require_relative 'charts/mixed'
10
15
  require_relative 'charts/syncing'
11
16
  require_relative 'charts/polar'
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class AreaChart < CartesianChart
3
5
  def chart_type
4
6
  'area'
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class BarChart < CartesianChart
3
5
  def chart_type
4
6
  'bar'
@@ -0,0 +1,34 @@
1
+ module ApexCharts
2
+ class BaseChart
3
+ attr_reader :options, :series
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
+ )
13
+ end
14
+
15
+ def render
16
+ ApexCharts::Renderer.render_default(options)
17
+ end
18
+
19
+ def chart_type
20
+ end
21
+
22
+ protected
23
+
24
+ def build_options(x_sample, options)
25
+ ApexCharts::OptionsBuilder.new(x_sample, options).build_options
26
+ end
27
+
28
+ def x_sample
29
+ series[:series][0][:data][0][:x]
30
+ end
31
+ end
32
+ end
33
+
34
+