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.



27
28
29
30
31
32
33
34
35
# File 'lib/apex_charts/options_builder.rb', line 27

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 (readonly)

Returns the value of attribute built.



25
26
27
# File 'lib/apex_charts/options_builder.rb', line 25

def built
  @built
end

Instance Method Details

#boolean_to_hash(options) ⇒ Object



318
319
320
321
322
323
324
325
326
# File 'lib/apex_charts/options_builder.rb', line 318

def boolean_to_hash(options)
  return if options.nil?

  if [true, false].include? options
    yield(options)
  elsif options.is_a?(Hash)
    options.compact
  end
end

#build_annotationsObject



78
79
80
81
82
83
# File 'lib/apex_charts/options_builder.rb', line 78

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

#build_chartObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/apex_charts/options_builder.rb', line 85

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! Options::ChartOptions.check(chart.compact)
end

#build_colorsObject



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

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

#build_data_labelsObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/apex_charts/options_builder.rb', line 118

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
                          Options::DataLabelsOptions.check data_labels.compact
                        end
end

#build_deferObject



129
130
131
132
# File 'lib/apex_charts/options_builder.rb', line 129

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

#build_divObject



69
70
71
72
73
74
75
76
# File 'lib/apex_charts/options_builder.rb', line 69

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



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

def build_fill
  fill = @options.delete :fill
  @built[:fill] = if fill.is_a? String
                    {type: fill}
                  elsif fill.is_a? Hash
                    Options::FillOptions.check fill.compact
                  end
end

#build_general_optionsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/apex_charts/options_builder.rb', line 45

def build_general_options
  build_annotations
  build_colors
  build_data_labels
  build_defer
  build_fill
  build_grid
  build_labels
  build_legend
  build_markers
  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



144
145
146
147
148
149
150
151
# File 'lib/apex_charts/options_builder.rb', line 144

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

#build_labelsObject



153
154
155
156
157
# File 'lib/apex_charts/options_builder.rb', line 153

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

#build_legendObject



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

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
                      Options::LegendOptions.check legend.compact
                    end
end

#build_markersObject



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

def build_markers
  markers = @options.delete :markers
  @built[:markers] = if markers.is_a? String
                       {shape: markers}
                     elsif markers.is_a? Hash
                       Options::MarkersOptions.check markers.compact
                     end
end

#build_no_dataObject



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

def build_no_data
  no_data = @options.delete :noData
  @built[:noData] = if no_data.is_a? String
                      {text: no_data}
                    elsif no_data.is_a? Hash
                      Options::NoDataOptions.check no_data.compact
                    end
end

#build_optionsObject



37
38
39
40
41
42
43
# File 'lib/apex_charts/options_builder.rb', line 37

def build_options
  Options::RootOptions.check @options

  build_chart
  build_div
  build_general_options
end

#build_plot_optionsObject



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

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

  @built[:plotOptions] =
    Options::PlotOptions.check plot_options.compact
end

#build_responsiveObject



196
197
198
199
200
# File 'lib/apex_charts/options_builder.rb', line 196

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

#build_statesObject



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/apex_charts/options_builder.rb', line 202

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! Options::StatesOptions.check(states.compact) if states.is_a? Hash

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

#build_strokeObject



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/apex_charts/options_builder.rb', line 215

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! Options::StrokeOptions.check(stroke.compact)
  end

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

#build_subtitleObject



229
230
231
232
233
234
235
236
# File 'lib/apex_charts/options_builder.rb', line 229

def build_subtitle
  subtitle = @options.delete(:subtitle)
  @built[:subtitle] = if subtitle.is_a? String
                        {text: subtitle}
                      elsif subtitle.is_a? Hash
                        Options::TitleSubtitleOptions.check subtitle.compact
                      end
end

#build_themeObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/apex_charts/options_builder.rb', line 238

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

#build_titleObject



254
255
256
257
258
259
260
261
# File 'lib/apex_charts/options_builder.rb', line 254

def build_title
  title = @options.delete(:title)
  @built[:title] = if title.is_a? String
                     {text: title}
                   elsif title.is_a? Hash
                     Options::TitleSubtitleOptions.check title.compact
                   end
end

#build_tooltipObject



263
264
265
266
267
268
269
270
# File 'lib/apex_charts/options_builder.rb', line 263

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

#build_xaxisObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/apex_charts/options_builder.rb', line 272

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?

  if xaxis.is_a? String
    @built[:xaxis][:title] = {text: xaxis}
  elsif xaxis.is_a? Hash
    Options::XAxisOptions.check xaxis
    @built[:xaxis].merge! xaxis
  end

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

#build_yaxisObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/apex_charts/options_builder.rb', line 292

def build_yaxis
  yaxis = @options.delete :yaxis
  @built[:yaxis] = [{
    type: @options.delete(:ytype) { @ytype },
    title: {
      text: @options.delete(:ytitle)
    }.compact
  }.compact]
  @built[:yaxis][0].delete(:title) if @built[:yaxis][0][:title].empty?

  if yaxis.is_a? String
    @built[:yaxis][0][:title] = {text: yaxis}
  elsif yaxis.is_a? Hash
    Options::YAxisOptions.check yaxis
    @built[:yaxis][0].merge! yaxis
  end

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

#enabled(options) ⇒ Object



312
313
314
315
316
# File 'lib/apex_charts/options_builder.rb', line 312

def enabled(options)
  boolean_to_hash(options) do |opts|
    {enabled: opts}
  end
end

#filter_type_hash(state) ⇒ Object



328
329
330
331
332
333
334
# File 'lib/apex_charts/options_builder.rb', line 328

def filter_type_hash(state)
  if state.is_a? String
    {filter: {type: state}}
  elsif state.is_a? Hash
    state.compact
  end
end

#resolve_theme(theme) ⇒ Object



336
337
338
339
340
341
342
343
# File 'lib/apex_charts/options_builder.rb', line 336

def resolve_theme(theme)
  if Theme::PALETTES.include? theme
    {palette: theme}
  elsif Theme::Local.palette_names.include? theme
    @built[:colors] = Theme::Local.get_colors(theme)
    nil
  end
end