Class: ApexCharts::OptionsBuilder
Instance Attribute Summary collapse
Instance Method Summary
collapse
convert, convert_range, to_milliseconds, to_milliseconds_range, type
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/apexcharts/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
#built ⇒ Object
Returns the value of attribute built.
25
26
27
|
# File 'lib/apexcharts/options_builder.rb', line 25
def built
@built
end
|
Instance Method Details
#boolean_to_hash(options) ⇒ Object
308
309
310
311
312
313
314
315
316
|
# File 'lib/apexcharts/options_builder.rb', line 308
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_annotations ⇒ Object
77
78
79
80
81
82
|
# File 'lib/apexcharts/options_builder.rb', line 77
def build_annotations
annotations = @options.delete :annotations
@built[:annotations] = (
AnnotationsOptions.check annotations.compact if annotations.is_a? Hash
)
end
|
#build_chart ⇒ Object
84
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
|
# File 'lib/apexcharts/options_builder.rb', line 84
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! ChartOptions.check(chart.compact)
end
|
#build_colors ⇒ Object
111
112
113
114
115
|
# File 'lib/apexcharts/options_builder.rb', line 111
def build_colors
colors = @options.delete :colors
colors &&= Array(colors)
@built[:colors] = colors
end
|
#build_data_labels ⇒ Object
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/apexcharts/options_builder.rb', line 117
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
DataLabelsOptions.check data_labels.compact
end
end
|
#build_div ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/apexcharts/options_builder.rb', line 68
def build_div
@built[:div] = {
id: @options.delete(:id),
var: @options.delete(:var),
class: @options.delete(:class),
style: @options.delete(:style)
}.compact
end
|
#build_fill ⇒ Object
128
129
130
131
132
133
134
135
|
# File 'lib/apexcharts/options_builder.rb', line 128
def build_fill
fill = @options.delete :fill
@built[:fill] = if fill.is_a? String
{type: fill}
elsif fill.is_a? Hash
FillOptions.check fill.compact
end
end
|
#build_general_options ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/apexcharts/options_builder.rb', line 45
def build_general_options
build_annotations
build_colors
build_data_labels
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_grid ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/apexcharts/options_builder.rb', line 137
def build_grid
grid = @options.delete :grid
@built[:grid] = if [true, false].include? grid
{show: grid}
elsif grid.is_a? Hash
GridOptions.check grid.compact
end
end
|
#build_labels ⇒ Object
146
147
148
149
150
|
# File 'lib/apexcharts/options_builder.rb', line 146
def build_labels
labels = @options.delete :labels
labels &&= Array(labels)
@built[:labels] = labels
end
|
#build_legend ⇒ Object
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/apexcharts/options_builder.rb', line 152
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
LegendOptions.check legend.compact
end
end
|
#build_markers ⇒ Object
163
164
165
166
167
168
169
170
|
# File 'lib/apexcharts/options_builder.rb', line 163
def build_markers
markers = @options.delete :markers
@built[:markers] = if markers.is_a? String
{shape: markers}
elsif markers.is_a? Hash
MarkersOptions.check markers.compact
end
end
|
#build_no_data ⇒ Object
172
173
174
175
176
177
178
179
|
# File 'lib/apexcharts/options_builder.rb', line 172
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
NoDataOptions.check no_data.compact
end
end
|
#build_options ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/apexcharts/options_builder.rb', line 37
def build_options
RootOptions.check @options
build_chart
build_div
build_general_options
end
|
#build_plot_options ⇒ Object
181
182
183
184
|
# File 'lib/apexcharts/options_builder.rb', line 181
def build_plot_options
plot_options = @options.delete :plotOptions
@built[:plotOptions] = (PlotOptions.check plot_options.compact if plot_options.is_a? Hash)
end
|
#build_responsive ⇒ Object
186
187
188
189
190
|
# File 'lib/apexcharts/options_builder.rb', line 186
def build_responsive
responsive = @options.delete :responsive
responsive &&= responsive.is_a?(Hash) ? [responsive] : Array(responsive)
@built[:responsive] = responsive
end
|
#build_states ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/apexcharts/options_builder.rb', line 192
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! StatesOptions.check(states.compact) if states.is_a? Hash
@built[:states] = nil if @built[:states].empty?
end
|
#build_stroke ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/apexcharts/options_builder.rb', line 205
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! StrokeOptions.check(stroke.compact)
end
@built[:stroke] = nil if @built[:stroke].empty?
end
|
#build_subtitle ⇒ Object
219
220
221
222
223
224
225
226
|
# File 'lib/apexcharts/options_builder.rb', line 219
def build_subtitle
subtitle = @options.delete(:subtitle)
@built[:subtitle] = if subtitle.is_a? String
{text: subtitle}
elsif subtitle.is_a? Hash
SubtitleOptions.check subtitle.compact
end
end
|
#build_theme ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/apexcharts/options_builder.rb', line 228
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
ThemeOptions.check theme.compact
end
end
|
#build_title ⇒ Object
244
245
246
247
248
249
250
251
|
# File 'lib/apexcharts/options_builder.rb', line 244
def build_title
title = @options.delete(:title)
@built[:title] = if title.is_a? String
{text: title}
elsif title.is_a? Hash
TitleOptions.check title.compact
end
end
|
253
254
255
256
257
258
259
260
|
# File 'lib/apexcharts/options_builder.rb', line 253
def build_tooltip
tooltip = @options.delete :tooltip
@built[:tooltip] = if [true, false].include? tooltip
{enabled: tooltip}
elsif tooltip.is_a? Hash
TooltipOptions.check tooltip.compact
end
end
|
#build_xaxis ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/apexcharts/options_builder.rb', line 262
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
XAxisOptions.check xaxis
@built[:xaxis].merge! xaxis
end
@built[:xaxis] = nil if @built[:xaxis].empty?
end
|
#build_yaxis ⇒ Object
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/apexcharts/options_builder.rb', line 282
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
YAxisOptions.check yaxis
@built[:yaxis][0].merge! yaxis
end
@built[:yaxis] = nil if @built[:yaxis].all?(&:empty?)
end
|
#enabled(options) ⇒ Object
302
303
304
305
306
|
# File 'lib/apexcharts/options_builder.rb', line 302
def enabled(options)
boolean_to_hash(options) do |opts|
{enabled: opts}
end
end
|
#filter_type_hash(state) ⇒ Object
318
319
320
321
322
323
324
|
# File 'lib/apexcharts/options_builder.rb', line 318
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
326
327
328
329
330
331
332
333
|
# File 'lib/apexcharts/options_builder.rb', line 326
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
|