forked from SciRuby/daru-view
-
Notifications
You must be signed in to change notification settings - Fork 2
/
highcharts_spec.rb
450 lines (427 loc) · 18.4 KB
/
highcharts_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
require 'spec_helper.rb'
# some specs from lazy_high_charts specs
describe Daru::View::Plot, 'plotting with highcharts' do
before { Daru::View.plotting_library = :highcharts }
before(:each) do
@placeholder = "placeholder"
@html_options = {:class => "stylin"}
@data = [[1, 15], [2, 30], [4, 40]]
@data_vec1 = Daru::Vector.new([3,6,8,9,20])
@data_vec2 = Daru::Vector.new([9,4,1,3,12])
@data_df = Daru::DataFrame.new(rows: @data_vec1, values: @data_vec2)
@options_bar = {
chart: {
type: 'bar',
},
title: {
text: 'Demo Bar Chart'
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
}
}
}
@options_line = {
chart: {
type: 'line'
},
title: {
text: 'Demo line Chart'
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
}
}
@options_column = {
chart: {
type: 'column'
},
title: {
text: 'Demo Column Chart'
}
}
@series_dt = [{
name: 'Tokyo',
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
name: 'New York',
data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]
}]
@chart_line = Daru::View::Plot.new(@data_vec1, @options_line)
@chart_bar = Daru::View::Plot.new(@data_df, @options_bar)
@chart_multiple_series = Daru::View::Plot.new(@series_dt, @options_bar)
@chart_column = Daru::View::Plot.new
@chart_column.chart.options = @options_column
@chart_column.chart.series_data = @data
end
describe "initialization" do
it "shouldn't generate a nil placeholder" do
expect(Daru::View::Plot.new.chart.placeholder).not_to be_nil
end
let(:plt) { Daru::View::Plot.new }
before { plt.chart.placeholder = @placeholder }
it "should take an optional 'placeholder' argument" do
expect(plt.chart.placeholder).to eq(@placeholder)
expect(Daru::View::Plot.new.chart.placeholder).not_to eq(@placeholder)
end
it "should generate different placeholders for different charts" do
a_different_placeholder = Daru::View::Plot.new.chart.placeholder
expect(Daru::View::Plot.new.chart.placeholder).not_to eq(a_different_placeholder)
end
it "should able take an optional html_options argument (defaulting to 300px height)" do
expect(Daru::View::Plot.new.chart.html_options).to eq({})
end
before { plt.chart.html_options = @html_options }
it "should able take an optional html_options argument (defaulting to 300px height)" do
expect(plt.chart.html_options).to eq(@html_options)
end
it "should set options by default" do
expect(Daru::View::Plot.new.chart.options).to eq({
:title => {:text => nil},
:legend => {:layout => "vertical", :style => {}},
:xAxis => {},
:yAxis => {:title => {:text => nil}, :labels => {}},
:tooltip => {:enabled => true},
:credits => {:enabled => false},
:plotOptions => {:areaspline => {}},
:chart => {:defaultSeriesType => "line", :renderTo => nil},
:subtitle => {}
})
end
it "should set data empty by default" do
expect(Daru::View::Plot.new.chart.series_data).to eq(
[{:type=>nil, :name=>nil, :data=>[]}])
end
let(:graph) { Daru::View::Plot.new }
it "should take a block setting attributes" do
graph.chart.tap do |f|
f.series_data = @data
f.options = @options
end
expect(graph.chart.series_data).to eq(@data)
expect(graph.chart.options).to eq(@options)
end
it "should take a block setting attributes" do
# Didn't understand why it fails :
# 1. when we do graph.chart do |f| ..
# 2. when we do graph.chart {|f| ..}
graph_chart = graph.chart.tap do |f|
f.options[:legend][:layout] = "horizontal"
end
expect(graph_chart.options[:legend][:layout]).to eq("horizontal")
end
it "should take a block setting attributes" do
graph_chart = graph.chart.tap do |f|
f.options[:range_selector] = {}
f.options[:range_selector][:selected] = 1
end
expect(graph_chart.options[:range_selector][:selected]).to eq(1)
end
it "should change a block data without overriding options" do
graph = Daru::View::Plot.new.chart.tap do |f|
f.series(:name => 'John', :data => [3, 20])
f.series(:name => 'Jane', :data => [1, 3])
# without overriding
f.options[:chart][:defaultSeriesType] = "area"
f.options[:chart][:inverted] = true
f.options[:legend][:layout] = "horizontal"
f.options[:xAxis][:categories] = ["uno", "dos", "tres", "cuatro"]
end
# {type: nil, :name => nil, :data => []} series is added during
# the initialzation of chart. User can remove it using this line:
# graph.delete({type: nil, :name => nil, :data => []})
expect(graph.series_data).to eq([
{type: nil, :name => nil, :data => []},
{:name => "John", :data => [3, 20]},
{:name => "Jane", :data => [1, 3]}
])
expect(graph.options[:legend][:layout]).to eq("horizontal")
expect(graph.options[:xAxis][:categories]).to eq(["uno", "dos", "tres", "cuatro"])
expect(graph.options[:chart][:defaultSeriesType]).to eq("area")
expect(graph.options[:chart][:inverted]).to eq(true)
end
it "should change a block data with overriding entire options" do
graph = Daru::View::Plot.new.chart.tap do |f|
f.series(:name => 'John', :data => [3, 20])
f.series(:name => 'Jane', :data => [1, 3])
f.title({:text => nil})
# without overriding
f.xAxis(:categories => ["uno", "dos", "tres", "cuatro"], :labels => {:rotation => -45, :align => 'right'})
f.chart({:defaultSeriesType => "spline", :renderTo => "myRenderArea", :inverted => true})
end
expect(graph.options[:xAxis][:categories]).to eq(["uno", "dos", "tres", "cuatro"])
expect(graph.options[:xAxis][:labels][:rotation]).to eq(-45)
expect(graph.options[:xAxis][:labels][:align]).to eq("right")
expect(graph.options[:chart][:defaultSeriesType]).to eq("spline")
expect(graph.options[:chart][:renderTo]).to eq("myRenderArea")
expect(graph.options[:chart][:inverted]).to eq(true)
end
it "should have subtitles" do
graph = Daru::View::Plot.new.chart.tap do |f|
f.series(:name => 'John', :data => [3, 20])
f.series(:name => 'Jane', :data => [1, 3])
f.title({:text => nil})
# without overriding
f.x_axis(:categories => ["uno", "dos", "tres", "cuatro"], :labels => {:rotation => -45, :align => 'right'})
f.chart({:defaultSeriesType => "spline", :renderTo => "myRenderArea", :inverted => true})
f.subtitle({:text => "Bar"})
end
expect(graph.options[:subtitle][:text]).to eq("Bar")
end
it 'should override entire option by default when resetting it again' do
graph = Daru::View::Plot.new.chart.tap do |f|
f.xAxis(categories: [3, 5, 7])
f.xAxis(title: {text: 'x title'})
end
expect(graph.options[:xAxis][:categories]).to eq(nil)
expect(graph.options[:xAxis][:title][:text]).to eq('x title')
end
it 'should allow to build options step by step without overriding previously set values' do
graph = Daru::View::Plot.new.chart.tap do |f|
f.xAxis!(categories: [3, 5, 7])
f.xAxis!(title: {text: 'x title'})
end
expect(graph.options[:xAxis][:categories]).to eq([3, 5, 7])
expect(graph.options[:xAxis][:title][:text]).to eq('x title')
end
it 'should merge options and data into a full options hash' do
graph = Daru::View::Plot.new.chart.tap do |f|
f.series(name: 'John', data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4])
f.series(name: 'Jane', data: [140.02, 41.63, 66.72, 113.21, 107.98, 105.71, 28.59, 114.23, 5.56, 93.71, 137.35, 93.16])
f.title({text: 'Example Data'})
f.xAxis(categories: %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec), labels: {rotation: -45, align: 'right'})
f.options[:tooltip][:formatter] = "function(){ return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 2) +' %'; }"
end
json = graph.full_options
expect(json).to match /\"series\"/
expect(json).to match /\"title\"/
expect(json).to match /\"tooltip\": { \"enabled\": true,\"formatter\"/
expect(json).to match /\"data\": \[ 29.9,71.5,106.4,129.2,144.0,176.0,135.6,148.5,216.4,194.1,95.6,54.4 \]/
end
end
describe "#init_script" do
it "generates valid initial script" do
js = @chart_bar.init_script
expect(js).to match(/BEGIN js\/highstock.js/i)
expect(js).to match(/Highstock JS/i)
expect(js).to match(/END js\/highstock.js/i)
expect(js).to match(/BEGIN modules\/exporting.js/i)
expect(js).to match(/END modules\/exporting.js/i)
expect(js).to match(/BEGIN highcharts-3d.js/i)
expect(js).to match(/END highcharts-3d.js/i)
expect(js).to match(/BEGIN modules\/data.js/i)
expect(js).to match(/END modules\/data.js/i)
expect(js).to match(/<script type='text\/javascript'>/i)
expect(js).to match(
/var event = document.createEvent\(\"HTMLEvents\"\)/i)
expect(js).to match(
/event.initEvent\(\"load_highcharts\", false, false\)/i)
expect(js).to match(/window.dispatchEvent\(event\)/i)
expect(js).to match(
/console.log\(\"Finish loading highchartsjs\"\)/i)
end
end
# called from #div in plot.rb as @chart_line.div
describe "#generate_body" do
context "when Line Chart is drawn" do
it "should generate valid JS of the Line Chart" do
js = @chart_line.adapter.generate_body(@chart_line.chart)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_line.adapter.generate_body(@chart_line.chart)
expect(js).to match(/\"chart\": { \"type\": \"line\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
expect(js).to match(
/\"plotOptions\": { \"line\": { \"dataLabels\": { \"enabled\": true }/
)
end
it "should set correct data" do
js = @chart_line.adapter.generate_body(@chart_line.chart)
expect(js).to match(/series/)
expect(js).to match(/\"type\": null/)
expect(js).to match(/\"name\": null/)
expect(js).to match(/\"data\": \[ 3,6,8,9,20 \]/)
end
end
context "when Bar Chart is drawn" do
it "should generate valid JS of the Bar Chart" do
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
expect(js).to match(/\"chart\": { \"type\": \"bar\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
expect(js).to match(
/\"plotOptions\": { \"bar\": { \"dataLabels\": { \"enabled\": true }/
)
end
it "should set correct data" do
js = @chart_bar.adapter.generate_body(@chart_bar.chart)
expect(js).to match(/series/)
expect(js).to match(/\"type\": null/)
expect(js).to match(/\"name\": null/)
expect(js).to match(
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
)
end
end
context "when Column Chart is drawn" do
it "should generate valid JS of the Column Chart" do
js = @chart_column.adapter.generate_body(@chart_column.chart)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_column.adapter.generate_body(@chart_column.chart)
expect(js).to match(/\"chart\": { \"type\": \"column\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
end
it "should set correct data" do
js = @chart_column.adapter.generate_body(@chart_column.chart)
expect(js).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
end
end
context "when multiple series is used as data" do
it "should generate valid JS of the Chart" do
js = @chart_multiple_series.adapter.generate_body(
@chart_multiple_series.chart
)
expect(js).to match(/script/)
expect(js).to match(/Highcharts.Chart\(options\)/)
expect(js).to match(/window.chart_/)
end
it "should set the correct options" do
js = @chart_multiple_series.adapter.generate_body(
@chart_multiple_series.chart
)
expect(js).to match(/\"chart\": { \"type\": \"bar\"/)
expect(js).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
end
it "should set correct data" do
js = @chart_multiple_series.adapter.generate_body(
@chart_multiple_series.chart
)
# indicates series contains array of hashes
expect(js).to match(/series": \[\{/)
end
end
end
describe "#generate_html" do
it "should generate valid HTML of the Line Chart" do
html = @chart_line.adapter.generate_html(@chart_line.chart)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN js\/highstock.js/i)
expect(html).to match(/END js\/highstock.js/i)
expect(html).to match(/BEGIN modules\/exporting.js/i)
expect(html).to match(/END modules\/exporting.js/i)
expect(html).to match(/BEGIN highcharts-3d.js/i)
expect(html).to match(/END highcharts-3d.js/i)
expect(html).to match(/BEGIN modules\/data.js/i)
expect(html).to match(/END modules\/data.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"line\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
expect(html).to match(/\"data\": \[ 3,6,8,9,20 \]/)
end
it "should generate valid HTML of the Bar Chart" do
html = @chart_bar.adapter.generate_html(@chart_bar.chart)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN js\/highstock.js/i)
expect(html).to match(/END js\/highstock.js/i)
expect(html).to match(/BEGIN modules\/exporting.js/i)
expect(html).to match(/END modules\/exporting.js/i)
expect(html).to match(/BEGIN highcharts-3d.js/i)
expect(html).to match(/END highcharts-3d.js/i)
expect(html).to match(/BEGIN modules\/data.js/i)
expect(html).to match(/END modules\/data.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"bar\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
expect(html).to match(
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
)
end
it "should generate valid HTML of the Column Chart" do
html = @chart_column.adapter.generate_html(@chart_column.chart)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN js\/highstock.js/i)
expect(html).to match(/END js\/highstock.js/i)
expect(html).to match(/BEGIN modules\/exporting.js/i)
expect(html).to match(/END modules\/exporting.js/i)
expect(html).to match(/BEGIN highcharts-3d.js/i)
expect(html).to match(/END highcharts-3d.js/i)
expect(html).to match(/BEGIN modules\/data.js/i)
expect(html).to match(/END modules\/data.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"column\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
expect(html).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
end
end
describe "#export_html_file" do
it "should write valid html code of the Line Chart to the file" do
@chart_line.export_html_file('./plot.html')
path = File.expand_path('../../plot.html', __dir__)
html = File.read(path)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN js\/highstock.js/i)
expect(html).to match(/END js\/highstock.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"line\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo line Chart\" }/)
end
it "should write valid html code of the Bar Chart to the file" do
@chart_bar.export_html_file('./plot.html')
path = File.expand_path('../../plot.html', __dir__)
html = File.read(path)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN js\/highstock.js/i)
expect(html).to match(/END js\/highstock.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"bar\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Bar Chart\" }/)
expect(html).to match(
/\"data\": \[ \[ 3,9 \],\[ 6,4 \],\[ 8,1 \],\[ 9,3 \],\[ 20,12 \] \]/
)
end
it "should write valid html code of the Column Chart to the file" do
@chart_column.export_html_file('./plot.html')
path = File.expand_path('../../plot.html', __dir__)
html = File.read(path)
expect(html).to match(/html/)
expect(html).to match(/Chart/)
expect(html).to match(/BEGIN js\/highstock.js/i)
expect(html).to match(/END js\/highstock.js/i)
expect(html).to match(/script/)
expect(html).to match(/Highcharts.Chart\(options\)/)
expect(html).to match(/\"chart\": { \"type\": \"column\"/)
expect(html).to match(/\"title\": { \"text\": \"Demo Column Chart\" }/)
expect(html).to match(/series": \[\[ 1,15 \],\[ 2,30 \],\[ 4,40 \]\]/)
end
end
end