apexcharts 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,20 +1,19 @@
1
- module Apexcharts
2
- class BubbleChart < CartesianChart
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
4
+ class BubbleChart < BaseChart
3
5
  def chart_type
4
6
  'bubble'
5
7
  end
6
8
 
7
- def mixed_series
8
- end
9
-
10
- protected
9
+ private
11
10
 
12
11
  def sanitize_data(data)
13
- Apexcharts::BubbleSeries.new(data).sanitized
12
+ ApexCharts::BubbleSeries.new(data).sanitized
14
13
  end
15
14
 
16
15
  def x_sample
17
- @series[:series][0][:data][0][0]
16
+ series[:series][0][:data][0][0]
18
17
  end
19
18
  end
20
19
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
4
+ class CandlestickChart < CartesianChart
5
+ def chart_type
6
+ 'candlestick'
7
+ end
8
+ end
9
+ end
@@ -1,21 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'features/annotations'
4
+ require_relative 'features/mixable'
2
5
 
3
- module Apexcharts
4
- class CartesianChart
6
+ module ApexCharts
7
+ class CartesianChart < BaseChart
5
8
  include Annotations
9
+ include Mixable
6
10
 
7
11
  def initialize bindings, data, options={}, &block
8
12
  @bindings = bindings
9
- options = {**options, **more_options}
13
+ options = Utils::Hash.deep_merge(
14
+ Utils::Hash.camelize_keys(options),
15
+ Utils::Hash.camelize_keys(more_options)
16
+ )
17
+
10
18
  build_instance_variables if @bindings
11
19
 
12
20
  instance_eval &block if block_given?
13
21
 
14
22
  options[:annotations] = @annotations if @annotations
15
23
  @series = sanitize_data(data)
16
- @options = Utils::Hash.camelize_keys(
17
- Utils::Hash.deep_merge(
18
- build_options(x_sample, options),
24
+ @options = Utils::Hash.deep_merge(
25
+ build_options(x_sample, options),
26
+ Utils::Hash.camelize_keys(
19
27
  {**@series, chart: {type: chart_type}}.compact
20
28
  )
21
29
  )
@@ -23,34 +31,10 @@ module Apexcharts
23
31
  get_selection_range if brush?
24
32
  end
25
33
 
26
- def chart_type
27
- end
28
-
29
34
  def more_options
30
35
  {}
31
36
  end
32
37
 
33
- def mixed_series
34
- @series[:series].each{|d| d.merge!(type: chart_type) }
35
- @series[:series]
36
- end
37
-
38
- def render
39
- attributes = @options.delete(:div) { {} }
40
- variable = attributes.delete(:var) { "chart#{attributes[:id]&.[](/\d+/)}" }
41
- element_id = attributes.delete(:id)
42
- css_class = attributes.delete(:class)
43
- height = "#{@options[:chart][:height].to_i}px"
44
- style = "height: #{height}; #{attributes.delete(:style)}"
45
- html =<<~HTML
46
- <div id="#{element_id}" class="#{css_class}" style="#{style}"></div>
47
- <script type="text/javascript">
48
- var #{variable} = new ApexCharts(document.querySelector("##{element_id}"), #{@options.to_json});
49
- #{variable}.render();
50
- </script>
51
- HTML
52
- end
53
-
54
38
  def method_missing method, *args, &block
55
39
  if @bindings
56
40
  @bindings.send method, *args, &block
@@ -68,11 +52,7 @@ module Apexcharts
68
52
  end
69
53
 
70
54
  def sanitize_data(data)
71
- Apexcharts::CartesianSeries.new(data).sanitized
72
- end
73
-
74
- def build_options(x_sample, options)
75
- Apexcharts::OptionsBuilder.new(x_sample, options).build_options
55
+ ApexCharts::CartesianSeries.new(data).sanitized
76
56
  end
77
57
 
78
58
  def brush?
@@ -96,10 +76,6 @@ module Apexcharts
96
76
  def handle_time(input)
97
77
  Utils::DateTime.convert(input)
98
78
  end
99
-
100
- def x_sample
101
- @series[:series][0][:data][0][:x]
102
- end
103
79
  end
104
80
  end
105
81
 
@@ -1,4 +1,4 @@
1
- module Apexcharts
1
+ module ApexCharts
2
2
  class ColumnChart < BarChart
3
3
  def more_options
4
4
  {plot_options: {bar: {horizontal: false}}}
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class DonutChart < PolarChart
3
5
  def chart_type
4
6
  'donut'
@@ -1,4 +1,4 @@
1
- module Apexcharts
1
+ module ApexCharts
2
2
  module Annotations
3
3
  def annotation(axis, value:, text:, color: nil, **options)
4
4
  @annotations ||= {}
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
4
+ module Mixable
5
+ def mixed_series
6
+ series[:series].each{|d| d.merge!(type: chart_type) }
7
+ series[:series]
8
+ end
9
+ end
10
+ end
11
+
@@ -1,10 +1,15 @@
1
- module Apexcharts
2
- class HeatmapChart < CartesianChart
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
4
+ class HeatmapChart < BaseChart
3
5
  def chart_type
4
6
  'heatmap'
5
7
  end
6
8
 
7
- def mixed_series
9
+ private
10
+
11
+ def sanitize_data(data)
12
+ ApexCharts::CartesianSeries.new(data).sanitized
8
13
  end
9
14
  end
10
15
  end
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class LineChart < CartesianChart
3
5
  def chart_type
4
6
  'line'
@@ -1,5 +1,7 @@
1
- module Apexcharts
2
- class MixedCharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
4
+ class MixedCharts < BaseChart
3
5
  include Annotations
4
6
 
5
7
  def initialize bindings, options={}, &block
@@ -11,9 +13,9 @@ module Apexcharts
11
13
  instance_eval &block
12
14
 
13
15
  options[:annotations] = @annotations if @annotations
14
- @options = Utils::Hash.camelize_keys(
15
- Utils::Hash.deep_merge(
16
- build_options(@series[:series][0][:data][0][:x], options),
16
+ @options = Utils::Hash.deep_merge(
17
+ build_options(x_sample, options),
18
+ Utils::Hash.camelize_keys(
17
19
  {chart: {type: 'area'}, **@series}
18
20
  )
19
21
  )
@@ -46,22 +48,6 @@ module Apexcharts
46
48
  @series[:series] += ScatterChart.new(bindings, data, options, &block).mixed_series
47
49
  end
48
50
 
49
- def render
50
- attributes = @options.delete(:div)
51
- variable = attributes.delete(:var) || "chart#{attributes[:id][/\d+/]}"
52
- element_id = attributes.delete(:id)
53
- css_class = attributes.delete(:class)
54
- height = "#{@options[:chart][:height].to_i}px"
55
- style = "height: #{height};#{attributes.delete(:style)}"
56
- html =<<~HTML
57
- <div id="#{element_id}" class="#{css_class}" style="#{style}"></div>
58
- <script type="text/javascript">
59
- var #{variable} = new ApexCharts(document.querySelector("##{element_id}"), #{@options.to_json});
60
- #{variable}.render();
61
- </script>
62
- HTML
63
- end
64
-
65
51
  def method_missing method, *args, &block
66
52
  @bindings.send method, *args, &block
67
53
  end
@@ -74,10 +60,6 @@ module Apexcharts
74
60
  end
75
61
  end
76
62
 
77
- def build_options(x_sample, options)
78
- Apexcharts::OptionsBuilder.new(x_sample, options).build_options
79
- end
80
-
81
63
  def brush?
82
64
  @options[:chart][:brush]&.[](:enabled) && \
83
65
  !@options[:chart][:selection]&.[](:xaxis)
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class PieChart < PolarChart
3
5
  def chart_type
4
6
  'pie'
@@ -1,42 +1,13 @@
1
- module Apexcharts
2
- class PolarChart
3
- def initialize data, options={}
4
- @series = sanitize_data(data)
5
- @options = Utils::Hash.camelize_keys(
6
- Utils::Hash.deep_merge(
7
- build_options(options),
8
- {**@series, chart: {type: chart_type}}.compact
9
- )
10
- )
11
- end
12
-
13
- def chart_type
14
- end
15
-
16
- def render
17
- attributes = @options.delete(:div) { {} }
18
- variable = attributes.delete(:var) { "chart#{attributes[:id]&.[](/\d+/)}" }
19
- element_id = attributes.delete(:id)
20
- css_class = attributes.delete(:class)
21
- height = "#{@options[:chart][:height].to_i}px"
22
- style = "height: #{height}; #{attributes.delete(:style)}"
23
- html =<<~HTML
24
- <div id="#{element_id}" class="#{css_class}" style="#{style}"></div>
25
- <script type="text/javascript">
26
- var #{variable} = new ApexCharts(document.querySelector("##{element_id}"), #{@options.to_json});
27
- #{variable}.render();
28
- </script>
29
- HTML
30
- end
1
+ module ApexCharts
2
+ class PolarChart < BaseChart
31
3
 
32
4
  protected
33
5
 
34
6
  def sanitize_data(data)
35
- Apexcharts::PolarSeries.new(data).sanitized
7
+ ApexCharts::PolarSeries.new(data).sanitized
36
8
  end
37
9
 
38
- def build_options(options)
39
- Apexcharts::OptionsBuilder.new(nil, options).build_options
10
+ def x_sample
40
11
  end
41
12
  end
42
13
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
4
+ class RadarChart < BaseChart
5
+ def chart_type
6
+ 'radar'
7
+ end
8
+
9
+ private
10
+
11
+ def sanitize_data(data)
12
+ ApexCharts::CartesianSeries.new(data).sanitized
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class RadialBarChart < PolarChart
3
5
  def chart_type
4
6
  'radialBar'
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class ScatterChart < CartesianChart
3
5
  def chart_type
4
6
  'scatter'
@@ -1,4 +1,6 @@
1
- module Apexcharts
1
+ # frozen_string_literal: true
2
+
3
+ module ApexCharts
2
4
  class SyncingCharts
3
5
  def initialize bindings, options={}, &block
4
6
  @bindings = bindings
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'series'
2
4
  require_relative 'options_builder'
3
5
  require_relative 'utils'
6
+ require_relative 'renderer'
4
7
  require_relative 'charts'
5
8
 
6
- module Apexcharts
9
+ module ApexCharts
7
10
  module Helper
8
11
  def line_chart series, options={}, &block
9
12
  bindings = eval("self", block.binding) if block_given?
@@ -30,14 +33,22 @@ module Apexcharts
30
33
  draw_chart(ScatterChart.new(bindings, *prepare_series_and_options(series, options), &block))
31
34
  end
32
35
 
33
- def heatmap_chart series, options={}, &block
36
+ def candlestick_chart series, options={}, &block
34
37
  bindings = eval("self", block.binding) if block_given?
35
- draw_chart(HeatmapChart.new(bindings, *prepare_series_and_options(series, options), &block))
38
+ draw_chart(CandlestickChart.new(bindings, *prepare_series_and_options(series, options), &block))
36
39
  end
40
+ alias_method :ohlc_chart, :candlestick_chart
37
41
 
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))
42
+ def heatmap_chart series, options={}
43
+ draw_chart(HeatmapChart.new(*prepare_series_and_options(series, options)))
44
+ end
45
+
46
+ def bubble_chart series, options={}
47
+ draw_chart(BubbleChart.new(*prepare_series_and_options(series, options)))
48
+ end
49
+
50
+ def radar_chart series, options={}
51
+ draw_chart(RadarChart.new(*prepare_series_and_options(series, options)))
41
52
  end
42
53
 
43
54
  def mixed_charts options={}, &block
@@ -1,4 +1,4 @@
1
- module Apexcharts
1
+ module ApexCharts
2
2
  class AnnotationsOptions < ::SmartKv
3
3
  optional *%i[
4
4
  points
@@ -1,14 +1,14 @@
1
- module Apexcharts
1
+ module ApexCharts
2
2
  class AxisOptions < ::SmartKv
3
3
  optional *%i[
4
- axis_border axisBorder
5
- axis_ticks axisTicks
4
+ axisBorder
5
+ axisTicks
6
6
  crosshairs
7
7
  floating
8
8
  labels
9
9
  max
10
10
  min
11
- tick_amount tickAmount
11
+ tickAmount
12
12
  title
13
13
  tooltip
14
14
  type
@@ -1,19 +1,21 @@
1
- module Apexcharts
1
+ module ApexCharts
2
2
  class ChartOptions < ::SmartKv
3
- optional *%i[ animations
3
+ optional *%i[
4
+ animations
4
5
  background
5
6
  brush
6
- default_locale defaultLocale
7
- drop_shadow dropShadow
7
+ defaultLocale
8
+ dropShadow
8
9
  events
9
- font_family fontFamily
10
- fore_color foreColor
10
+ fontFamily
11
+ foreColor
11
12
  height
12
13
  id
13
14
  locales
15
+ parentHeightOffset
14
16
  selection
15
17
  sparkline
16
- stack_type stackType
18
+ stackType
17
19
  stacked
18
20
  toolbar
19
21
  type