Class: ApexCharts::OptionsBuilder

Inherits:
Object
  • Object
show all
Includes:
Utils::DateTime, Utils::Hash
Defined in:
lib/apex_charts/options_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::DateTime

convert, convert_range, to_milliseconds, to_milliseconds_range, type

Methods included from Utils::Hash

camelize, camelize_keys, deep_merge

Constructor Details

#initialize(sample, options) ⇒ OptionsBuilder

Returns a new instance of OptionsBuilder.



10
11
12
13
14
15
16
17
18
# File 'lib/apex_charts/options_builder.rb', line 10

def initialize(sample, options)
  @options = camelize_keys(options)
  if options[:plotOptions]&.[](:bar)&.[](:horizontal)
    @ytype = type(sample)
  else
    @xtype = type(sample)
  end
  @built = {}
end

Instance Attribute Details

#builtObject

Returns the value of attribute built.



8
9
10
# File 'lib/apex_charts/options_builder.rb', line 8

def built
  @built
end

Instance Method Details

#build_annotationsObject



60
61
62
63
64
65
# File 'lib/apex_charts/options_builder.rb', line 60

def build_annotations
  annotations = @options.delete :annotations
  built[:annotations] = (
    annotations.compact if annotations.is_a? Hash
  )
end

#build_chartObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/apex_charts/options_builder.rb', line 67

def build_chart
  built[:chart] =
    if target = @options.delete(:brushTarget)
      {
        brush: {enabled: true, target: target.to_s},
        selection: {enabled: true}
      }
    else
      {}
    end

  built[:chart].merge!({
    id: @options[:chartId] || @options[:id],
    group: @options.delete(:group),
    height: @options.delete(:height) { target ? 180 : 400 },
    width: @options.delete(:width),
    stacked: @options.delete(:stacked),
    animations: enabled(@options.delete(:animations)),
    sparkline: enabled(@options.delete(:sparkline)),
    background: @options.delete(:background),
    foreColor: @options.delete(:foreColor)
  }.compact)

  chart = @options.delete :chart

  return unless chart.is_a? Hash

  built[:chart].merge! chart.compact
end

#build_colorsObject



97
98
99
100
101
# File 'lib/apex_charts/options_builder.rb', line 97

def build_colors
  colors = @options.delete :colors
  colors &&= Array(colors)
  built[:colors] = colors
end

#build_data_labelsObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/apex_charts/options_builder.rb', line 103

def build_data_labels
  data_labels = @options.delete :dataLabels
  return if data_labels.nil?

  built[:dataLabels] = if [true, false].include? data_labels
                         {enabled: data_labels}
                       elsif data_labels.is_a? Hash
                         data_labels.compact
                       end
end

#build_deferObject



114
115
116
117
# File 'lib/apex_charts/options_builder.rb', line 114

def build_defer
  defer = @options.delete :defer
  built[:defer] = defer == true
end

#build_divObject



51
52
53
54
55
56
57
58
# File 'lib/apex_charts/options_builder.rb', line 51

def build_div
  built[:div] = {
    id: @options.delete(:id),
    var: @options.delete(:var),
    class: @options.delete(:class),
    style: @options.delete(:style)
  }.compact
end

#build_fillObject



124
125
126
127
128
129
130
131
132
# File 'lib/apex_charts/options_builder.rb', line 124

def build_fill
  fill = @options.delete :fill
  built[:fill] = case fill
                 when String
                   {type: fill}
                 when Hash
                   fill.compact
                 end
end

#build_general_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apex_charts/options_builder.rb', line 26

def build_general_options
  build_annotations
  build_colors
  build_data_labels
  build_defer
  build_fill
  build_grid
  build_labels
  build_legend
  build_markers
  build_module
  build_no_data
  build_plot_options
  build_responsive
  build_states
  build_stroke
  build_subtitle
  build_theme
  build_title
  build_tooltip
  build_xaxis
  build_yaxis
  built.compact
end

#build_gridObject



134
135
136
137
138
139
140
141
# File 'lib/apex_charts/options_builder.rb', line 134

def build_grid
  grid = @options.delete :grid
  built[:grid] = if [true, false].include? grid
                   {show: grid}
                 elsif grid.is_a? Hash
                   grid.compact
                 end
end

#build_labelsObject



143
144
145
146
147
# File 'lib/apex_charts/options_builder.rb', line 143

def build_labels
  labels = @options.delete :labels
  labels &&= Array(labels)
  built[:labels] = labels
end

#build_legendObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/apex_charts/options_builder.rb', line 149

def build_legend
  legend = @options.delete :legend
  built[:legend] = if [true, false].include? legend
                     {show: legend}
                   elsif legend.is_a? String
                     {show: true, position: legend}
                   elsif legend.is_a? Hash
                     legend.compact
                   end
end

#build_markersObject



160
161
162
163
164
165
166
167
168
# File 'lib/apex_charts/options_builder.rb', line 160

def build_markers
  markers = @options.delete :markers
  built[:markers] = case markers
                    when String
                      {shape: markers}
                    when Hash
                      markers.compact
                    end
end

#build_moduleObject



119
120
121
122
# File 'lib/apex_charts/options_builder.rb', line 119

def build_module
  omodule = @options.delete :module
  built[:module] = omodule == true
end

#build_no_dataObject



170
171
172
173
174
175
176
177
178
# File 'lib/apex_charts/options_builder.rb', line 170

def build_no_data
  no_data = @options.delete :noData
  built[:noData] = case no_data
                   when String
                     {text: no_data}
                   when Hash
                     no_data.compact
                   end
end

#build_optionsObject



20
21
22
23
24
# File 'lib/apex_charts/options_builder.rb', line 20

def build_options
  build_chart
  build_div
  build_general_options
end

#build_plot_optionsObject



180
181
182
183
184
185
186
# File 'lib/apex_charts/options_builder.rb', line 180

def build_plot_options
  plot_options = @options.delete :plotOptions
  return unless plot_options.is_a? Hash

  built[:plotOptions] =
    plot_options.compact
end

#build_responsiveObject



188
189
190
191
192
# File 'lib/apex_charts/options_builder.rb', line 188

def build_responsive
  responsive = @options.delete :responsive
  responsive &&= responsive.is_a?(Hash) ? [responsive] : Array(responsive)
  built[:responsive] = responsive
end

#build_statesObject



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/apex_charts/options_builder.rb', line 194

def build_states
  built[:states] = {
    normal: filter_type_hash(@options.delete(:normal)),
    hover: filter_type_hash(@options.delete(:hover)),
    active: filter_type_hash(@options.delete(:active))
  }.compact

  states = @options.delete :states
  built[:states].merge! states.compact if states.is_a? Hash

  built[:states] = nil if built[:states].empty?
end

#build_strokeObject



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/apex_charts/options_builder.rb', line 207

def build_stroke
  curve = @options.delete :curve
  built[:stroke] = {curve: curve}.compact

  stroke = @options.delete :stroke
  if [true, false].include? stroke
    built[:stroke].merge!(show: stroke)
  elsif stroke.is_a? Hash
    built[:stroke].merge! stroke.compact
  end

  built[:stroke] = nil if built[:stroke].empty?
end

#build_subtitleObject



221
222
223
224
225
226
227
228
229
# File 'lib/apex_charts/options_builder.rb', line 221

def build_subtitle
  subtitle = @options.delete(:subtitle)
  built[:subtitle] = case subtitle
                     when String
                       {text: subtitle}
                     when Hash
                       subtitle.compact
                     end
end

#build_themeObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/apex_charts/options_builder.rb', line 231

def build_theme
  theme = @options.delete(:theme)
  built[:theme] = case theme
                  when String
                    case theme
                    when 'random'
                      resolve_theme(Theme.all_palettes.sample)
                    when 'monochrome'
                      {monochrome: {enabled: true}}
                    else
                      resolve_theme(theme)
                    end
                  when Hash
                    theme.compact
                  end
end

#build_titleObject



248
249
250
251
252
253
254
255
256
# File 'lib/apex_charts/options_builder.rb', line 248

def build_title
  title = @options.delete(:title)
  built[:title] = case title
                  when String
                    {text: title}
                  when Hash
                    title.compact
                  end
end

#build_tooltipObject



258
259
260
261
262
263
264
265
# File 'lib/apex_charts/options_builder.rb', line 258

def build_tooltip
  tooltip = @options.delete :tooltip
  built[:tooltip] = if [true, false].include? tooltip
                      {enabled: tooltip}
                    elsif tooltip.is_a? Hash
                      tooltip.compact
                    end
end

#build_xaxisObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/apex_charts/options_builder.rb', line 267

def build_xaxis
  xaxis = @options.delete :xaxis
  built[:xaxis] = {
    type: @options.delete(:xtype) { @xtype },
    title: {
      text: @options.delete(:xtitle)
    }.compact
  }.compact
  built[:xaxis].delete(:title) if built[:xaxis][:title].empty?

  case xaxis
  when String
    built[:xaxis][:title] = {text: xaxis}
  when Hash
    built[:xaxis].merge! xaxis
  end

  built[:xaxis] = nil if built[:xaxis].empty?
end

#build_yaxisObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/apex_charts/options_builder.rb', line 287

def build_yaxis
  yaxis = @options.delete :yaxis

  case yaxis
  when Array
    built[:yaxis] = yaxis
  when Hash
    built[:yaxis] = {
      type: @options.delete(:ytype) { @ytype },
      title: {
        text: @options.delete(:ytitle)
      }.compact
    }.compact
    built[:yaxis].delete(:title) if built[:yaxis][:title].empty?
    built[:yaxis].merge! yaxis
  when String
    built[:yaxis] = {
      type: @options.delete(:ytype) { @ytype },
      title: {
        text: yaxis
      }
    }.compact
  when NilClass
    built[:yaxis] = if ytitle = @options.delete(:ytitle)
                      {title: {text: ytitle}}
                    else
                      {}
                    end
  end

  built[:yaxis] = nil if built[:yaxis].all?(&:empty?)
end