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.
28
29
30
31
32
33
34
35
36
|
# File 'lib/apex_charts/options_builder.rb', line 28
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.
26
27
28
|
# File 'lib/apex_charts/options_builder.rb', line 26
def built
@built
end
|
Instance Method Details
#build_annotations ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/apex_charts/options_builder.rb', line 79
def build_annotations
annotations = @options.delete :annotations
@built[:annotations] = (
if annotations.is_a? Hash
options_class(:Annotations).check annotations.compact
end
)
end
|
#build_chart ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/apex_charts/options_builder.rb', line 88
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_class(:Chart).check(chart.compact)
end
|
#build_colors ⇒ Object
118
119
120
121
122
|
# File 'lib/apex_charts/options_builder.rb', line 118
def build_colors
colors = @options.delete :colors
colors &&= Array(colors)
@built[:colors] = colors
end
|
#build_data_labels ⇒ Object
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/apex_charts/options_builder.rb', line 124
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_class(:DataLabels).check data_labels.compact
end
end
|
#build_defer ⇒ Object
135
136
137
138
|
# File 'lib/apex_charts/options_builder.rb', line 135
def build_defer
defer = @options.delete :defer
@built[:defer] = defer == true
end
|
#build_div ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/apex_charts/options_builder.rb', line 70
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
141
142
143
144
145
146
147
148
|
# File 'lib/apex_charts/options_builder.rb', line 141
def build_fill
fill = @options.delete :fill
@built[:fill] = if fill.is_a? String
{type: fill}
elsif fill.is_a? Hash
options_class(:Fill).check fill.compact
end
end
|
#build_general_options ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/apex_charts/options_builder.rb', line 46
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_grid ⇒ Object
150
151
152
153
154
155
156
157
|
# File 'lib/apex_charts/options_builder.rb', line 150
def build_grid
grid = @options.delete :grid
@built[:grid] = if [true, false].include? grid
{show: grid}
elsif grid.is_a? Hash
options_class(:Grid).check grid.compact
end
end
|
#build_labels ⇒ Object
159
160
161
162
163
|
# File 'lib/apex_charts/options_builder.rb', line 159
def build_labels
labels = @options.delete :labels
labels &&= Array(labels)
@built[:labels] = labels
end
|
#build_legend ⇒ Object
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/apex_charts/options_builder.rb', line 165
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_class(:Legend).check legend.compact
end
end
|
#build_markers ⇒ Object
176
177
178
179
180
181
182
183
|
# File 'lib/apex_charts/options_builder.rb', line 176
def build_markers
markers = @options.delete :markers
@built[:markers] = if markers.is_a? String
{shape: markers}
elsif markers.is_a? Hash
options_class(:Markers).check markers.compact
end
end
|
#build_no_data ⇒ Object
185
186
187
188
189
190
191
192
|
# File 'lib/apex_charts/options_builder.rb', line 185
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_class(:NoData).check no_data.compact
end
end
|
#build_options ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/apex_charts/options_builder.rb', line 38
def build_options
options_class(:Root).check @options
build_chart
build_div
build_general_options
end
|
#build_plot_options ⇒ Object
194
195
196
197
198
199
200
|
# File 'lib/apex_charts/options_builder.rb', line 194
def build_plot_options
plot_options = @options.delete :plotOptions
return unless plot_options.is_a? Hash
@built[:plotOptions] =
options_class(:Plot).check plot_options.compact
end
|
#build_responsive ⇒ Object
202
203
204
205
206
|
# File 'lib/apex_charts/options_builder.rb', line 202
def build_responsive
responsive = @options.delete :responsive
responsive &&= responsive.is_a?(Hash) ? [responsive] : Array(responsive)
@built[:responsive] = responsive
end
|
#build_states ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/apex_charts/options_builder.rb', line 208
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_class(:States).check(states.compact) if states.is_a? Hash
@built[:states] = nil if @built[:states].empty?
end
|
#build_stroke ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/apex_charts/options_builder.rb', line 221
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_class(:Stroke).check(stroke.compact)
end
@built[:stroke] = nil if @built[:stroke].empty?
end
|
#build_subtitle ⇒ Object
235
236
237
238
239
240
241
242
|
# File 'lib/apex_charts/options_builder.rb', line 235
def build_subtitle
subtitle = @options.delete(:subtitle)
@built[:subtitle] = if subtitle.is_a? String
{text: subtitle}
elsif subtitle.is_a? Hash
options_class(:TitleSubtitle).check subtitle.compact
end
end
|
#build_theme ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/apex_charts/options_builder.rb', line 244
def build_theme
theme = @options.delete(:theme)
@built[:theme] = if theme.is_a? String
case theme
when 'random'
resolve_theme(Theme.all_palettes.sample)
when 'monochrome'
{monochrome: {enabled: true}}
else
resolve_theme(theme)
end
elsif theme.is_a? Hash
options_class(:Theme).check theme.compact
end
end
|
#build_title ⇒ Object
260
261
262
263
264
265
266
267
|
# File 'lib/apex_charts/options_builder.rb', line 260
def build_title
title = @options.delete(:title)
@built[:title] = if title.is_a? String
{text: title}
elsif title.is_a? Hash
options_class(:TitleSubtitle).check title.compact
end
end
|
269
270
271
272
273
274
275
276
|
# File 'lib/apex_charts/options_builder.rb', line 269
def build_tooltip
tooltip = @options.delete :tooltip
@built[:tooltip] = if [true, false].include? tooltip
{enabled: tooltip}
elsif tooltip.is_a? Hash
options_class(:Tooltip).check tooltip.compact
end
end
|
#build_xaxis ⇒ Object
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/apex_charts/options_builder.rb', line 278
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_class(:XAxis).check xaxis
@built[:xaxis].merge! xaxis
end
@built[:xaxis] = nil if @built[:xaxis].empty?
end
|
#build_yaxis ⇒ Object
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/apex_charts/options_builder.rb', line 298
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_class(:YAxis).check yaxis
@built[:yaxis][0].merge! yaxis
end
@built[:yaxis] = nil if @built[:yaxis].all?(&:empty?)
end
|