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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
sinon = require 'sinon'
describe 'Epoch.Chart', ->
[defaultWidth, defaultHeight] = [320, 240]
describe 'Base', ->
[testDivWidth, testDivHeight] = [800, 200]
[resizeDivWidth, resizeDivHeight] = [200, 200]
before (done) ->
d3.select(doc.body).append('div').attr('id', 'testDiv').style
width: "#{testDivWidth}px"
height: "#{testDivHeight}px"
d3.select(doc.body).append('div').attr('id', 'resizeDiv').style
width: "#{resizeDivWidth}px"
height: "#{resizeDivHeight}px"
done()
after (done) ->
d3.select('#testDiv').remove()
d3.select('#resizeDiv').remove()
done()
describe 'constructor', ->
it 'should set default dimensions', ->
c = new Epoch.Chart.Base()
assert.equal c.width, defaultWidth, 'Did not set default width'
assert.equal c.height, defaultHeight, 'Did not set default height'
it 'should allow dimensions to be set via options', ->
[width, height] = [500, 780]
c = new Epoch.Chart.Base({ width: width, height: height })
assert.equal c.width, width, "Did not set width to #{width}"
assert.equal c.height, height, "Did not set height to #{height}"
it 'should use the dimensions of the given element when applicable', ->
c = new Epoch.Chart.Base({ el: '#testDiv' })
assert.equal c.width, testDivWidth, "Did not set width to that of the div"
assert.equal c.height, testDivHeight, "Did not set height to that of the div"
it 'should set default data to an empty array', ->
c = new Epoch.Chart.Base()
assert.isArray c.data
assert.equal c.data.length, 0
it 'should set data when given as an option', ->
data = [
{label: 'A', values: [{x: 0, y: 0}]},
{label: 'B', values: [{x: 1, y: 1}]}
]
c = new Epoch.Chart.Base({ data: data })
assert.sameMembers(c.data, data)
describe 'setData', ->
data = [
{label: 'A', values: [{x: 10, y: 20}]},
{label: 'B', values: [{x: 10, y: 20}]},
{label: 'C', values: [{x: 10, y: 20}]}
]
classNames = [
['layer', 'category1', 'a'],
['layer', 'category2', 'b'],
['layer', 'category3', 'c']
]
chart = null
before (done) ->
(chart = new Epoch.Chart.Base()).setData(data)
done()
it 'should set data correctly', ->
assert.sameMembers chart.data, data
for i in [0...data.length]
assert.equal chart.data[i].label, data[i].label
assert.equal chart.data[i].values[0], data[i].values[0]
it 'should add the correct categories and class names', ->
for i in [0...data.length]
className = chart.data[i].className
for name in classNames[i]
assert (className.indexOf(name) > -1), "Missing class '#{name}'"
describe 'draw', ->
it "should trigger the 'draw' event", (done) ->
errorCallback = ->
assert 'false', "The 'draw' event was never triggered"
done()
timeout = setTimeout(errorCallback, 1000)
chart = new Epoch.Chart.Base()
chart.on 'draw', ->
clearTimeout(timeout)
done()
chart.draw()
describe 'update', ->
it 'should call draw by default', (done) ->
errorCallback = ->
assert false, "update did not call draw by default."
done()
timeout = setTimeout(errorCallback, 1000)
chart = new Epoch.Chart.Base()
chart.on 'draw', ->
clearTimeout(timeout)
done()
chart.update([])
it 'should not call draw when instructed', (done) ->
chart = new Epoch.Chart.Base()
chart.on 'draw', ->
assert false, "Update incorrectly called draw."
done()
chart.update([], false)
done()
describe 'extent', ->
data = [
{values: [
{x: -1, y: 10},
{x: 2, y: 20},
{x: 4, y: 50},
{x: 8, y: 9900}
]},
{values: [
{x: 1, y: 170},
{x: 7, y: -2380},
{x: 19, y: 90},
{x: 33, y: 17}
]}
]
[xMin, xMax] = [-1, 33]
[yMin, yMax] = [-2380, 9900]
chart = null
before (done) ->
chart = new Epoch.Chart.Base({ data: data })
done()
it 'should find the correct extent given a y-comparitor', ->
[min, max] = chart.extent (d) -> d.y
assert.equal min, yMin, "Incorrect minimum y"
assert.equal max, yMax, "Incorrect maximum y"
it 'should find the correct extent give an x-comparitor', ->
[min, max] = chart.extent (d) -> d.x
assert.equal min, xMin, "Incorrect minimum x"
assert.equal max, xMax, "Incorrect maximum x"
describe 'option', ->
it 'should return all options for the chart when called with no arguments', ->
options = { a: 20, b: 30, c: { d: 40 } }
chart = new Epoch.Chart.Base options
assert.isObject chart.option()
assert.deepEqual chart.option(), options
it 'should return a single value when given a key', ->
options = { a: 20, b: 30 }
chart = new Epoch.Chart.Base options
assert.equal chart.option('a'), options.a
assert.equal chart.option('b'), options.b
assert.isUndefined chart.option('c')
it 'should return a deep value when given a hierarchical key', ->
options =
a:
b: 20
c:
d: 30
chart = new Epoch.Chart.Base options
assert.equal chart.option('a.b'), options.a.b
assert.equal chart.option('a.c.d'), options.a.c.d
it 'should set an option given a string and a value', ->
chart = new Epoch.Chart.Base()
[key, value] = ['a', 'hello world']
chart.option(key, value)
assert.equal chart.option(key), value
it 'should set a deep value when given a hierarchical key', ->
chart = new Epoch.Chart.Base()
map =
'a.b': 'deep'
'a.c.d': 'deeper'
'b': 'shallow'
for key, value of map
chart.option(key, value)
assert.equal chart.option(key), value
it 'should set all options given an object', ->
original = { a: 20, b: { c: 30 } }
newOptions = { a: 15, d: { e: 10, f: 30 } }
chart = new Epoch.Chart.Base()
chart.option(newOptions)
assert.deepEqual chart.option(), newOptions
it 'should trigger an event when an option is changed', (done) ->
[key, value] = ['a', 20]
eventName = "option:#{key}"
errorCallback = ->
assert false, "Setting an option did not trigger the appropriate event: #{eventName}"
done()
timeout = setTimeout(errorCallback, 1000)
chart = new Epoch.Chart.Base()
chart.on eventName, ->
clearTimeout(timeout)
done()
chart.option(key, value)
it 'should resize the containing element when the width option is changed', ->
newWidth = resizeDivWidth + 20
chart = new Epoch.Chart.Base({ el: '#resizeDiv' })
chart.option('width', newWidth)
assert.equal d3.select('#resizeDiv').width(), newWidth
it 'should resize the containing element when the height option is changed', ->
newHeight = resizeDivHeight + 20
chart = new Epoch.Chart.Base({ el: '#resizeDiv' })
chart.option('height', newHeight)
assert.equal d3.select('#resizeDiv').height(), newHeight
describe '_getScaleDomain', ->
chart = null
beforeEach ->
chart = new Epoch.Chart.Base
data: [ layerWithRange(-2030, 5050) ]
it 'returns a given array', ->
assert.deepEqual(chart._getScaleDomain([0,1]), [0,1])
it 'returns @options.range if it is an array', ->
chart.options.range = [-100, 100]
assert.equal chart._getScaleDomain(), chart.options.range
it 'returns @options.range.left if it is an array', ->
chart.options.range = {left: [-100, 100]}
assert.equal chart._getScaleDomain(), chart.options.range.left
it 'returns @options.range.right if it is an array', ->
chart.options.range = {right: [-100, 100]}
assert.equal chart._getScaleDomain(), chart.options.range.right
it 'returns the extent of the data', ->
assert.deepEqual chart._getScaleDomain(), chart.extent((d) -> d.y)
describe 'with range grouped layers', ->
beforeEach ->
chart = new Epoch.Chart.Base
data: [
layerWithRange(0, 10, 'left'),
layerWithRange(-5000, 5000, 'right'),
layerWithRange(-10, -5, 'left')
]
it 'returns the extent of the layers with the given range label', ->
assert.deepEqual chart._getScaleDomain('left'), [-10, 10]
it 'returns the extent of the data if the label is invalid', ->
assert.deepEqual chart._getScaleDomain('foobar'), chart.extent((d) -> d.y)
describe 'layers', ->
[chart, eventChart] = [null, null]
labels = ['A', 'B', 'C']
data = [
{ label: 'A', data: [{x: 0, y: 0}] },
{ label: 'B', data: [{x: 1, y: 1}] },
{ label: 'C', data: [{x: 2, y: 2}] }
]
data2 = [
{ label: 'A', data: [{x: 0, y: 0}] }
]
before (done) ->
chart = new Epoch.Chart.Base
el: doc.createElement('div')
data: data
eventChart = new Epoch.Chart.Base
el: doc.createElement('div')
data: data2
done()
describe '_findLayer', ->
it 'should find layers given a label', ->
for label in labels
layer = chart._findLayer(label)
assert.equal label, layer.label, "Could not find layer with label #{label}"
it 'should find layers given an index', ->
for i in [0...data.length]
layer = chart._findLayer(i)
assert.equal labels[i], layer.label, "Could not find layer with index #{i}"
it 'should return null if given an invalid label', ->
assert.isNull (chart._findLayer 'D')
assert.isNull (chart._findLayer 'not a thing')
it 'should return null if given an index that is out of bounds', ->
assert.isNull (chart._findLayer -1)
assert.isNull (chart._findLayer 5)
describe 'hideLayer', ->
it 'should hide a visible layer', ->
chart.hideLayer('A')
assert.isFalse chart._findLayer('A').visible
it 'should keep a hidden layer hidden', ->
assert.isFalse chart._findLayer('A').visible
chart.hideLayer('A')
assert.isFalse chart._findLayer('A').visible
it 'should trigger layer:hidden when a layer is hidden', (done) ->
errorCallback = ->
assert false, "layer:hidden was not triggered"
done()
timeout = setTimeout errorCallback, 1000
eventChart.on 'layer:hidden', ->
clearTimeout timeout
done()
eventChart.hideLayer('A')
describe 'showLayer', ->
it 'should have keep a visible layer visible', ->
assert.isTrue chart._findLayer('B').visible
chart.showLayer('B')
assert.isTrue chart._findLayer('B').visible
it 'should make a hidden layer visible', ->
assert.isFalse chart._findLayer('A').visible
chart.showLayer('A')
assert.isTrue chart._findLayer('A').visible
it 'should trigger layer:shown when a layer is shown', (done) ->
errorCallback = ->
assert false, "layer:shown was not triggered"
done()
timeout = setTimeout errorCallback, 1000
eventChart.on 'layer:shown', ->
clearTimeout timeout
done()
eventChart.showLayer('A')
describe 'toggleLayer', ->
it 'should hide a visible layer', ->
chart.hideLayer('A')
chart.toggleLayer('A')
assert.isTrue chart._findLayer('A').visible
it 'should show a hidden layer', ->
chart.showLayer('B')
chart.toggleLayer('B')
assert.isFalse chart._findLayer('B').visible
describe 'isLayerVisible', ->
it 'should report true if a layer is visible', ->
chart.showLayer('A')
assert.isTrue chart.isLayerVisible('A')
it 'should report false if a layer is not visible', ->
chart.hideLayer('A')
assert.isFalse chart.isLayerVisible('B')
describe 'getVisibleLayers', ->
it 'should only return visible layers', ->
chart.showLayer('A')
chart.showLayer('B')
chart.hideLayer('C')
visible = chart.getVisibleLayers()
assert.equal visible.length, 2
assert.equal visible[0].label, 'A'
assert.equal visible[1].label, 'B'
describe 'SVG', ->
[containerWidth, containerHeight] = [1000, 280]
container = null
before (done) ->
container = doc.createElement('DIV')
container.id = 'svg-container'
doc.body.appendChild(container)
d3.select('#svg-container').style
'width': "#{containerWidth}px"
'height': "#{containerHeight}px"
done()
after (done) ->
doc.body.removeChild(container)
done()
describe 'constructor', ->
it 'should create a new SVG when not given an element', ->
chart = new Epoch.Chart.SVG()
assert.ok chart.svg, "SVG not created"
it 'should set the default width and height of the SVG', ->
chart = new Epoch.Chart.SVG()
assert.equal chart.svg.attr('width'), defaultWidth, "Default width not set"
assert.equal chart.svg.attr('height'), defaultHeight, "Default height not set"
it 'should set custom dimensions for the SVG via options', ->
[customWidth, customHeight] = [500, 600]
chart = new Epoch.Chart.SVG({ width: customWidth, height: customHeight })
assert.equal chart.svg.attr('width'), customWidth, "Custom width not set"
assert.equal chart.svg.attr('height'), customHeight, "Custom height not set"
it 'should set the container dimensions for the SVG', ->
chart = new Epoch.Chart.SVG({ el: '#svg-container' })
assert.equal chart.svg.attr('width'), containerWidth
assert.equal chart.svg.attr('height'), containerHeight
describe 'dimensionsChanged', ->
[width, height, chart] = [200, 100, null]
before (done) ->
d3.select(doc.body).append('div').attr('id', 'svgResize').style
width: width + 'px'
height: height + 'px'
chart = new Epoch.Chart.SVG { el: '#svgResize' }
done()
after (done) ->
d3.select('#svgResize').remove()
done()
it 'should resize the SVG element when the width option is changed', ->
newWidth = width + 500
chart.option 'width', newWidth
assert.equal chart.svg.attr('width'), newWidth
it 'should resize the SVG element when the height option is changed', ->
newHeight = height + 500
chart.option 'height', newHeight
assert.equal chart.svg.attr('height'), newHeight
describe 'Canvas', ->
[containerWidth, containerHeight] = [1000, 280]
container = null
container_id = 'canvas-container'
containedChart = null
before (done) ->
container = doc.createElement('DIV')
container.id = container_id
doc.body.appendChild(container)
d3.select('#' + container_id).style
'width': "#{containerWidth}px"
'height': "#{containerHeight}px"
containedChart = new Epoch.Chart.Canvas
el: '#' + container_id
pixelRatio: 1
done()
after (done) ->
doc.body.removeChild(container)
done()
describe 'constructor', ->
it 'should correctly detect the pixelRatio', ->
chart = new Epoch.Chart.Canvas()
assert.equal chart.pixelRatio, 2
it 'should allow the pixelRatio to be explicitly overriden', ->
customPixelRatio = 4.2
chart = new Epoch.Chart.Canvas({ pixelRatio: customPixelRatio })
assert.equal chart.pixelRatio, customPixelRatio
it 'should create a child canvas', ->
chart = new Epoch.Chart.Canvas()
assert.ok chart.canvas, "Did not create canvas"
assert.equal chart.canvas.node().tagName.toLowerCase(), 'canvas', 'Did not create a canvas node'
it 'should append the child canvas to the containing element', ->
assert.equal containedChart.canvas.node().parentNode.id, container_id
it 'should set the default dimensions for the canvas', ->
chart = new Epoch.Chart.Canvas({ pixelRatio: 1 })
assert.equal chart.canvas.attr('width'), defaultWidth
assert.equal chart.canvas.attr('height'), defaultHeight
it 'should allow custom dimensions for the canvas', ->
[customWidth, customHeight] = [999, 888]
chart = new Epoch.Chart.Canvas
width: customWidth
height: customHeight
pixelRatio: 1
assert.equal chart.canvas.attr('width'), customWidth
assert.equal chart.canvas.attr('height'), customHeight
it 'should set container dimensions for the canvas', ->
assert.equal containedChart.canvas.attr('width'), containerWidth
assert.equal containedChart.canvas.attr('height'), containerHeight
it 'should fetch a graphics context from the canvas', ->
assert.ok containedChart.ctx, "Did not fetch graphics context from canvas"
it 'should take pixel ratio into account when setting canvas dimension attributes', ->
pixelRatio = 3
chart = new Epoch.Chart.Canvas({ pixelRatio: pixelRatio })
assert.equal chart.canvas.attr('width'), defaultWidth * pixelRatio
assert.equal chart.canvas.attr('height'), defaultHeight * pixelRatio
it 'should not take pixel ratio into account when setting canvas dimension styles', ->
chart = new Epoch.Chart.Canvas({ pixelRatio: 2 })
assert.equal +chart.canvas.style('width').replace('px', ''), defaultWidth
assert.equal +chart.canvas.style('height').replace('px', ''), defaultHeight
describe 'getWidth', ->
it 'should take pixel ratio into account', ->
pixelRatio = 2
chart = new Epoch.Chart.Canvas({ pixelRatio: pixelRatio })
assert.equal chart.getWidth(), pixelRatio * defaultWidth
describe 'getHeight', ->
it 'should take pixel ratio into account', ->
pixelRatio = 2
chart = new Epoch.Chart.Canvas({ pixelRatio: pixelRatio })
assert.equal chart.getHeight(), pixelRatio * defaultHeight
describe 'dimensionsChanged', ->
[width, height, chart, pixelRatio] = [200, 100, null, 2]
before (done) ->
d3.select(doc.body).append('div').attr('id', 'canvasResize').style
width: width + 'px'
height: height + 'px'
chart = new Epoch.Chart.Canvas { el: '#canvasResize', pixelRatio: pixelRatio }
done()
after (done) ->
d3.select('#canvasResize').remove()
done()
it 'should resize the canvas element when the width option is changed', ->
newWidth = width + 500
chart.option 'width', newWidth
assert.equal chart.canvas.attr('width'), pixelRatio * newWidth
assert.equal chart.canvas.width(), newWidth
it 'should resize the canvas element when the height option is changed', ->
newHeight = height + 500
chart.option 'height', newHeight
assert.equal chart.canvas.attr('height'), pixelRatio * newHeight
assert.equal chart.canvas.height(), newHeight
describe 'redraw', ->
chart = null
drawSpy = null
purgeSpy = null
before ->
chart = new Epoch.Chart.Canvas()
beforeEach ->
drawSpy = sinon.spy chart, 'draw'
purgeSpy = sinon.spy Epoch.QueryCSS, 'purge'
afterEach ->
chart.draw.restore()
Epoch.QueryCSS.purge.restore()
it 'should purge QueryCSS cache and redraw the canvas based chart with new styles', ->
chart.redraw()
assert drawSpy.calledOnce
assert purgeSpy.calledOnce
|