-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbar-chart-spec.js
1395 lines (1176 loc) · 54.3 KB
/
bar-chart-spec.js
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global appendChartID, loadDateFixture, makeDate, cleanDateRange, simulateChartBrushing */
describe('dc.BarChart', () => {
let id, chart, data;
let dimension, group;
beforeEach(() => {
data = crossfilter(loadDateFixture());
dimension = data.dimension(d => d3.utcDay(d.dd));
group = dimension.group();
id = 'bar-chart';
appendChartID(id);
chart = new dc.BarChart(`#${id}`);
chart.dimension(dimension).group(group)
.width(1100).height(200)
.x(d3.scaleUtc().domain([makeDate(2012, 0, 1), makeDate(2012, 11, 31)]))
.transitionDuration(0)
.controlsUseVisibility(true);
});
describe('rendering', () => {
beforeEach(() => {
chart.render();
});
it('should set bar height using y-values from data', () => {
forEachBar((bar, datum) => {
expect(+bar.attr('y')).toBe(chart.y()(datum.data.value));
});
});
it('should set bar width to the minimum for a relatively small chart', () => {
forEachBar(bar => {
expect(+bar.attr('width')).toBe(1);
});
});
it('should preserve method chaining', () => {
expect(chart.render()).toEqual(chart);
});
it('should not display bar labels without setting renderLabel(true)', () => {
expect(chart.selectAll('text.barLabel').size()).toBe(0);
});
describe('with centered bars', () => {
beforeEach(() => {
chart.centerBar(true).render();
});
it('should position bars centered around their data points', () => {
const halfBarWidth = 0.5;
forEachBar((bar, datum) => {
const barPosition = chart.x()(datum.data.key);
expect(+bar.attr('x')).toBeCloseTo(barPosition - halfBarWidth, 3);
});
});
});
describe('without centered bars', () => {
it('should position bars starting at their data points', () => {
forEachBar((bar, datum) => {
const barPosition = chart.x()(datum.data.key);
expect(+bar.attr('x')).toBeCloseTo(barPosition, 3);
});
});
});
describe('with bar labels', () => {
beforeEach(() => {
chart.renderLabel(true).render();
});
it('should generate a label for each datum', () => {
expect(chart.selectAll('text.barLabel').size()).toBe(6);
});
it('should generate labels with positions corresponding to their data', () => {
expect(nthStack(0).nthLabel(0).attr('x')).toBeWithinDelta(405, 1);
expect(nthStack(0).nthLabel(0).attr('y')).toBeWithinDelta(104, 1);
expect(nthStack(0).nthLabel(0).text()).toBe('1');
expect(nthStack(0).nthLabel(3).attr('x')).toBeWithinDelta(509, 1);
expect(nthStack(0).nthLabel(3).attr('y')).toBeWithinDelta(104, 1);
expect(nthStack(0).nthLabel(3).text()).toBe('1');
expect(nthStack(0).nthLabel(5).attr('x')).toBeWithinDelta(620, 1);
expect(nthStack(0).nthLabel(5).attr('y')).toBeWithinDelta(50, 1);
expect(nthStack(0).nthLabel(5).text()).toBe('2');
});
});
describe('with custom bar labels', () => {
beforeEach(() => {
chart.label(() => 'custom label').render();
});
it('should render a label for each datum', () => {
expect(chart.selectAll('text.barLabel').size()).toBe(6);
});
it('should use the custom function for each label', () => {
chart.selectAll('text.barLabel').each(function () {
expect(d3.select(this).text()).toBe('custom label');
});
});
describe('with labels disabled', () => {
beforeEach(() => {
chart.renderLabel(false).render();
});
it('should not display labels', () => {
expect(chart.selectAll('text.barLabel').size()).toBe(0);
});
});
});
describe('and then switching the group at runtime', () => {
beforeEach(() => {
chart.rescale(); // BUG: barWidth cannot change after initial rendering
const domain = [makeDate(2012, 4, 20), makeDate(2012, 7, 15)];
chart.x(d3.scaleUtc().domain(domain))
.group(dimension.group().reduceSum(d => +d.nvalue))
.elasticY(true)
.centerBar(false)
.xUnits(d3.utcDays)
.yAxis().ticks(5);
chart.render();
});
it('should generate a bar for each datum', () => {
expect(chart.selectAll('rect.bar').size()).toBe(6);
});
it('should automatically resize the bar widths', () => {
forEachBar(bar => {
expect(bar.attr('width')).toBe('9');
});
});
function nthYAxisText (n) {
return d3.select(chart.selectAll('g.y text').nodes()[n]);
}
it('should generate bars with positions corresponding to their data', () => {
expect(nthStack(0).nthBar(0).attr('x')).toBeWithinDelta(58, 1);
expect(nthStack(0).nthBar(0).attr('y')).toBeWithinDelta(84, 1);
expect(nthStack(0).nthBar(0).attr('height')).toBeWithinDelta(30, 1);
expect(nthStack(0).nthBar(3).attr('x')).toBeWithinDelta(492, 1);
expect(nthStack(0).nthBar(3).attr('y')).toBeWithinDelta(84, 1);
expect(nthStack(0).nthBar(3).attr('height')).toBeWithinDelta(23, 1);
expect(nthStack(0).nthBar(5).attr('x')).toBeWithinDelta(961, 1);
expect(nthStack(0).nthBar(5).attr('y')).toBeWithinDelta(61, 1);
expect(nthStack(0).nthBar(5).attr('height')).toBeWithinDelta(23, 1);
});
it('should generate the y-axis domain dynamically', () => {
expect(nthYAxisText(0).text()).toMatch(/[\-−]10/);
expect(nthYAxisText(1).text()).toMatch(/[\-−]5/);
expect(nthYAxisText(2).text()).toBe('0');
});
});
describe('with an ordinal x domain', () => {
let stateDimension;
beforeEach(() => {
stateDimension = data.dimension(d => d.state);
const stateGroup = stateDimension.group();
const ordinalDomainValues = ['California', 'Colorado', 'Delaware', 'Ontario', 'Mississippi', 'Oklahoma'];
chart.rescale(); // BUG: barWidth cannot change after initial rendering
chart.dimension(stateDimension)
.group(stateGroup)
.xUnits(dc.units.ordinal)
.x(d3.scaleBand().domain(ordinalDomainValues))
.barPadding(0)
.outerPadding(0.1)
.render();
});
it('should automatically disable the brush', () => {
expect(chart.brushOn()).toBeFalsy();
});
it('should generate a bar for each ordinal domain value', () => {
expect(chart.selectAll('rect.bar').size()).toBe(6);
});
it('should size the bars proportionally to the graph', () => {
expect(+chart.select('rect.bar').attr('width')).toBe(164);
});
it('should position the bar based on the ordinal range', () => {
expect(nthStack(0).nthBar(0).attr('x')).toBeWithinDelta(16, 1);
expect(nthStack(0).nthBar(3).attr('x')).toBeWithinDelta(674, 1);
expect(nthStack(0).nthBar(5).attr('x')).toBeWithinDelta(509, 1);
});
it('should fade deselected bars', () => {
chart.filter('Ontario').filter('Colorado').redraw();
expect(nthStack(0).nthBar(0).classed('deselected')).toBeTruthy();
expect(nthStack(0).nthBar(1).classed('deselected')).toBeFalsy();
expect(nthStack(0).nthBar(5).classed('deselected')).toBeFalsy();
expect(stateDimension.top(Infinity).length).toBe(3);
});
it('should respect the ordering of the specified domain', () => {
// Note that bar chart works differently from pie chart. The bar objects (the
// actual DOM nodes) don't get reordered by the custom ordering, but they are
// placed so that they are drawn in the order specified.
const ontarioXPos = nthStack(0).nthBar(5).attr('x');
const mississippiXPos = nthStack(0).nthBar(3).attr('x');
const oklahomaXPos = nthStack(0).nthBar(4).attr('x');
expect(ontarioXPos).toBeLessThan(mississippiXPos);
expect(mississippiXPos).toBeLessThan(oklahomaXPos);
});
describe('with elasticY enabled', () => {
beforeEach(() => {
chart.elasticY(true).render();
});
it('should use all ordinal keys to determine the maximum y', () => {
expect(chart.y().domain()).toEqual([0, 3]);
});
});
describe('with an unspecified domain', () => {
beforeEach(() => {
chart.x(d3.scaleBand()).render();
});
it('should use alphabetical ordering', () => {
const data02 = chart.selectAll('rect.bar').data();
const expectedData = ['California', 'Colorado', 'Delaware', 'Mississippi', 'Oklahoma', 'Ontario'];
expect(data02.map(datum => datum.x)).toEqual(expectedData);
let oldX = -Infinity;
forEachBar(bar => {
expect(bar.attr('x')).toBeGreaterThan(oldX);
oldX = bar.attr('x');
});
});
});
describe('redrawing after changing the value accessor', () => {
beforeEach(() => {
chart.valueAccessor(() => 30);
chart.redraw();
});
it('should position bars based on ordinal range', () => {
expect(nthStack(0).nthBar(0).attr('height')).toBe('1600');
expect(nthStack(0).nthBar(1).attr('height')).toBe('1600');
expect(nthStack(0).nthBar(2).attr('height')).toBe('1600');
});
});
describe('clicking', () => {
it('causes other dimension to be filtered', () => {
expect(dimension.top(Infinity).length).toEqual(10);
// fake a click
const abar = chart.selectAll('rect.bar:nth-child(3)');
dc.d3compat.callHandler(abar.on('click'), null, {}, abar.datum());
expect(dimension.top(Infinity).length).toEqual(1);
});
});
describe('clicking bar labels', () => {
beforeEach(() => {
chart.renderLabel(true).render();
});
it('causes other dimension to be filtered', () => {
expect(dimension.top(Infinity).length).toEqual(10);
// fake a click
const alabel = chart.select('text.barLabel');
dc.d3compat.callHandler(alabel.on('click'), null, {}, alabel.datum());
expect(dimension.top(Infinity).length).toEqual(3);
});
});
});
describe('d3.scaleOrdinal() deprecation for ordinal x domain', () => {
let stateDimension;
beforeEach(() => {
spyOn(dc.logger, 'warn');
stateDimension = data.dimension(d => d.state);
const stateGroup = stateDimension.group();
const ordinalDomainValues = ['California', 'Colorado', 'Delaware', 'Ontario', 'Mississippi', 'Oklahoma'];
chart.rescale(); // BUG: barWidth cannot change after initial rendering
chart.dimension(stateDimension)
.group(stateGroup)
.xUnits(dc.units.ordinal)
.x(d3.scaleOrdinal().domain(ordinalDomainValues))
.barPadding(0)
.outerPadding(0.1)
.render();
});
it('should work with a warning', () => {
expect(dc.logger.warn).toHaveBeenCalled();
expect(typeof chart.x().bandwidth).toEqual('function');
expect(nthStack(0).nthBar(0).attr('x')).toBeWithinDelta(16, 1);
expect(nthStack(0).nthBar(3).attr('x')).toBeWithinDelta(674, 1);
expect(nthStack(0).nthBar(5).attr('x')).toBeWithinDelta(509, 1);
});
});
describe('with a linear x domain', () => {
beforeEach(() => {
const linearDimension = data.dimension(d => +d.value);
const linearGroup = linearDimension.group();
chart.rescale(); // BUG: barWidth cannot change after initial rendering
chart.dimension(linearDimension)
.group(linearGroup)
.xUnits(dc.units.integers)
.x(d3.scaleLinear().domain([20, 70]))
.render();
});
it('should generate the correct number of bars', () => {
expect(chart.selectAll('rect.bar').size()).toBe(5);
});
it('should auto size bar width', () => {
forEachBar(bar => {
expect(bar.attr('width')).toBe('18');
});
});
it('should position bars based on linear range', () => {
expect(nthStack(0).nthBar(0).attr('x')).toBeWithinDelta(40, 1);
expect(nthStack(0).nthBar(2).attr('x')).toBeWithinDelta(489, 1);
expect(nthStack(0).nthBar(4).attr('x')).toBeWithinDelta(938, 1);
});
describe('with a custom click handler', () => {
beforeEach(() => {
chart.brushOn(false)
.on('renderlet', _chart => {
_chart.selectAll('rect.bar').on('click', dc.d3compat.eventHandler(d => _chart.onClick(d)));
})
.render();
});
it('clicking causes another dimension to be filtered', () => {
expect(dimension.top(Infinity).length).toEqual(10);
const abar = chart.selectAll('rect.bar:nth-child(3)');
dc.d3compat.callHandler(abar.on('click'), null, {}, abar.datum());
expect(dimension.top(Infinity).length).toEqual(3);
});
});
});
describe('with stacked data', () => {
describe('with positive data', () => {
beforeEach(() => {
const idGroup = dimension.group().reduceSum(d => d.id);
const sumGroup = dimension.group().reduceSum(d => d.value);
chart
.brushOn(false)
.x(d3.scaleUtc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]))
.group(idGroup, 'stack 0')
.title('stack 0', d => `stack 0: ${d.value}`)
.stack(sumGroup, 'stack 1')
.title('stack 1', d => `stack 1: ${d.value}`)
.stack(sumGroup, 'stack 2', d => 3)
.elasticY(true)
.renderLabel(true)
.render();
});
it('should set the y domain to encompass all stacks', () => {
expect(chart.y().domain()).toEqual([0, 152]);
});
it('should generate each stack using its associated group', () => {
expect(nthStack(0).selectAll('rect.bar').size()).toBe(6);
expect(nthStack(1).selectAll('rect.bar').size()).toBe(6);
expect(nthStack(2).selectAll('rect.bar').size()).toBe(6);
});
it('should render the correct number of stacks', () => {
expect(chart.selectAll('.stack').size()).toBe(3);
});
it('should display one label for each stack', () => {
expect(chart.selectAll('text.barLabel').size()).toBe(6);
});
it('should generate labels with total value of stack', () => {
expect(nthStack(2).nthLabel(0).text()).toBe('48');
expect(nthStack(2).nthLabel(3).text()).toBe('51');
expect(nthStack(2).nthLabel(5).text()).toBe('92');
});
it('should stack the bars', () => {
expect(+nthStack(0).nthBar(2).attr('y')).toBe(142);
expect(+nthStack(0).nthBar(4).attr('y')).toBe(144);
expect(+nthStack(1).nthBar(2).attr('y')).toBe(3);
expect(+nthStack(1).nthBar(4).attr('y')).toBe(86);
expect(+nthStack(2).nthBar(2).attr('y')).toBe(0);
expect(+nthStack(2).nthBar(4).attr('y')).toBe(83);
});
it('should have its own title accessor', () => {
expect(chart.title()({value: 1})).toBe('stack 0: 1');
expect(chart.title('stack 0')({value: 2})).toBe('stack 0: 2');
expect(chart.title('stack 1')({value: 3})).toBe('stack 1: 3');
});
it('should have titles rendered for extra stacks', () => {
nthStack(1).forEachBar((bar, datum) => {
expect(bar.selectAll('title').nodes().length).toBe(1);
expect(bar.select('title').text()).toBe(`stack 1: ${datum.data.value}`);
});
});
it('should default to first stack title for untitled stacks', () => {
nthStack(2).forEachBar((bar, datum) => {
expect(bar.select('title').text()).toBe(`stack 0: ${datum.data.value}`);
});
});
describe('extra redraws', () => {
beforeEach(() => {
chart.redraw();
chart.redraw();
});
it('should not create extra title elements', () => {
nthStack(1).forEachBar((bar, datum) => {
expect(bar.selectAll('title').nodes().length).toBe(1);
});
});
});
describe('with title rendering disabled', () => {
beforeEach(() => {
chart.renderTitle(false).render();
});
it('should not generate title elements', () => {
expect(chart.selectAll('rect.bar title').empty()).toBeTruthy();
});
});
describe('stack hiding', () => {
describe('first stack', () => {
beforeEach(() => {
chart.hideStack('stack 0').render();
});
it('should hide the stack', () => {
expect(nthStack(0).nthBar(0).attr('height')).toBe('52');
expect(nthStack(0).nthBar(1).attr('height')).toBe('78');
});
it('should show the stack', () => {
chart.showStack('stack 0').render();
expect(nthStack(0).nthBar(0).attr('height')).toBe('1');
expect(nthStack(0).nthBar(1).attr('height')).toBe('6');
});
});
describe('any other stack', () => {
beforeEach(() => {
chart.title('stack 2', d => `stack 2: ${d.value}`);
chart.hideStack('stack 1').render();
});
it('should hide the stack', () => {
expect(nthStack(1).nthBar(0).attr('height')).toBe('24');
expect(nthStack(1).nthBar(1).attr('height')).toBe('24');
});
it('should show the stack', () => {
chart.showStack('stack 1').render();
expect(nthStack(1).nthBar(0).attr('height')).toBe('46');
expect(nthStack(1).nthBar(1).attr('height')).toBe('70');
});
it('should still show the title for a visible stack', () => {
nthStack(1).forEachBar((bar, datum) => {
expect(bar.select('title').text()).toBe(`stack 2: ${datum.data.value}`);
});
});
});
describe('hiding all the stacks', () => {
beforeEach(() => {
chart.hideStack('stack 0')
.hideStack('stack 1')
.hideStack('stack 2')
.render();
});
it('should show a blank graph', () => {
expect(chart.selectAll('rect.bar').size()).toBe(0);
});
});
});
});
describe('with mixed positive and negative data', () => {
beforeEach(() => {
const mixedGroup = dimension.group().reduceSum(d => d.nvalue);
chart.group(mixedGroup).stack(mixedGroup).stack(mixedGroup);
chart.x(d3.scaleUtc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]));
chart.margins({top: 30, right: 50, bottom: 30, left: 30})
.yAxisPadding(5)
.elasticY(true)
.xUnits(d3.utcDays)
.yAxis().ticks(5);
chart.rescale(); // BUG: barWidth cannot change after initial rendering
chart.render();
});
it('should generate a bar for each datum across all stacks', () => {
expect(chart.selectAll('rect.bar').size()).toBe(18);
});
it('should automatically size the bar widths', () => {
forEachBar(bar => {
expect(bar.attr('width')).toBe('9');
});
});
it('should generate negative bars for stack 0', () => {
expect(nthStack(0).nthBar(0).attr('x')).toBeWithinDelta(58, 1);
expect(nthStack(0).nthBar(0).attr('y')).toBeWithinDelta(73, 1);
expect(nthStack(0).nthBar(0).attr('height')).toBeWithinDelta(8, 1);
expect(nthStack(0).nthBar(3).attr('x')).toBeWithinDelta(492, 1);
expect(nthStack(0).nthBar(3).attr('y')).toBeWithinDelta(73, 1);
expect(nthStack(0).nthBar(3).attr('height')).toBeWithinDelta(6, 1);
expect(nthStack(0).nthBar(5).attr('x')).toBeWithinDelta(961, 1);
expect(nthStack(0).nthBar(5).attr('y')).toBeWithinDelta(67, 1);
expect(nthStack(0).nthBar(5).attr('height')).toBeWithinDelta(6, 1);
});
it('should generate negative bar for stack 1', () => {
expect(nthStack(1).nthBar(0).attr('x')).toBeWithinDelta(58, 1);
expect(nthStack(1).nthBar(0).attr('y')).toBeWithinDelta(81, 1);
expect(nthStack(1).nthBar(0).attr('height')).toBeWithinDelta(7, 1);
expect(nthStack(1).nthBar(3).attr('x')).toBeWithinDelta(492, 1);
expect(nthStack(1).nthBar(3).attr('y')).toBeWithinDelta(79, 1);
expect(nthStack(1).nthBar(3).attr('height')).toBeWithinDelta(5, 1);
expect(nthStack(1).nthBar(5).attr('x')).toBeWithinDelta(961, 1);
expect(nthStack(1).nthBar(5).attr('y')).toBeWithinDelta(61, 1);
expect(nthStack(1).nthBar(5).attr('height')).toBeWithinDelta(6, 1);
});
it('should generate y axis domain dynamically', () => {
const nthText = function (n) {
return d3.select(chart.selectAll('g.axis.y .tick text').nodes()[n]);
};
expect(nthText(0).text()).toMatch(/[\-−]20/);
expect(nthText(1).text()).toBe('0');
expect(nthText(2).text()).toBe('20');
});
});
describe('with negative data', () => {
beforeEach(() => {
const negativeGroup = dimension.group().reduceSum(d => -Math.abs(d.nvalue));
chart.group(negativeGroup).stack(negativeGroup).stack(negativeGroup);
chart.x(d3.scaleUtc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]));
chart.margins({top: 30, right: 50, bottom: 30, left: 30})
.elasticY(true)
.xUnits(d3.utcDays)
.yAxis().ticks(3);
chart.render();
});
it('should generate y axis domain dynamically', () => {
const nthText = function (n) {
return d3.select(chart.selectAll('g.axis.y .tick text').nodes()[n]);
};
// d3@5 and d3@6 uses different characters to format negative numbers
expect(nthText(0).text()).toMatch(/[\-−]30/);
expect(nthText(1).text()).toMatch(/[\-−]20/);
expect(nthText(2).text()).toMatch(/[\-−]10/);
expect(nthText(3).text()).toBe('0');
});
});
});
it('should not be focused by default', () => {
expect(chart.refocused()).toBeFalsy();
});
describe('when focused', () => {
beforeEach(() => {
chart.elasticY(true).gap(1).xUnits(d3.utcDays);
chart.focus([makeDate(2012, 5, 11), makeDate(2012, 6, 9)]);
});
it('should render the one (focused) bar', () => {
expect(chart.selectAll('rect.bar').size()).toBe(1);
});
it('should resize the bar width according to the focused width', () => {
expect(chart.select('rect.bar').attr('width')).toBe('35');
});
it('should reset the y-axis domain based on the focus range', () => {
expect(chart.y().domain()).toEqual([0, 1]);
});
it('should redraw the x-axis scale and ticks', () => {
expect(xAxisText().slice(0, 4)).toEqual(['Mon 11', 'Wed 13', 'Fri 15', 'Jun 17']);
});
it('should set its focus flag', () => {
expect(chart.refocused()).toBeTruthy();
});
describe('with evadeDomainFilter', () => {
beforeEach(() => {
chart.evadeDomainFilter(true).redraw();
});
it('should still reset the y-axis domain based on the focus range', () => {
expect(chart.y().domain()).toEqual([0, 1]);
});
});
it('should reset the focus when focused to null', () => {
chart.focus(null);
itBehavesLikeItWasReset();
});
it('should reset the focus when focused to []', () => {
chart.focus([]);
itBehavesLikeItWasReset();
});
function itBehavesLikeItWasReset () {
expect(chart.refocused()).toBeFalsy();
expect(chart.x().domain()).toEqual([makeDate(2012, 0, 1), makeDate(2012, 11, 31)]);
expect(xAxisText().slice(0, 4)).toEqual(['2012', 'February', 'March', 'April']);
}
function xAxisText () {
return chart.selectAll('g.x text').nodes().map(x => d3.select(x).text());
}
});
describe('legend hovering', () => {
let firstItem;
beforeEach(() => {
chart.stack(group)
.legend(new dc.Legend().x(400).y(10).itemHeight(13).gap(5))
.render();
firstItem = chart.select('g.dc-legend g.dc-legend-item');
dc.d3compat.callHandler(firstItem.on('mouseover'), null, {}, firstItem.datum());
});
describe('when a legend item is hovered over', () => {
it('should highlight corresponding lines and areas', () => {
nthStack(0).forEachBar(bar => {
expect(bar.classed('highlight')).toBeTruthy();
});
});
it('should fade out non-corresponding lines and areas', () => {
nthStack(1).forEachBar(bar => {
expect(bar.classed('fadeout')).toBeTruthy();
});
});
});
describe('when a legend item is hovered out', () => {
it('should remove highlighting from corresponding lines and areas', () => {
dc.d3compat.callHandler(firstItem.on('mouseout'), null, {}, firstItem.datum());
nthStack(0).forEachBar(bar => {
expect(bar.classed('highlight')).toBeFalsy();
});
});
it('should fade in non-corresponding lines and areas', () => {
dc.d3compat.callHandler(firstItem.on('mouseout'), null, {}, firstItem.datum());
nthStack(1).forEachBar(bar => {
expect(bar.classed('fadeout')).toBeFalsy();
});
});
});
});
describe('filtering', () => {
beforeEach(() => {
d3.select(`#${id}`).append('span').attr('class', 'filter').style('visibility', 'hidden');
d3.select(`#${id}`).append('a').attr('class', 'reset').style('visibility', 'hidden');
chart.filter([makeDate(2012, 5, 1), makeDate(2012, 5, 30)]).redraw();
dc.config.dateFormat = d3.utcFormat('%m/%d/%Y');
chart.redraw();
});
it('should set the chart filter', () => {
expect(chart.filter()).toEqual([makeDate(2012, 5, 1), makeDate(2012, 5, 30)]);
});
it('should enable the reset link after rendering', () => {
expect(chart.select('a.reset').style('visibility')).not.toBe('none');
});
it('should set the filter printer', () => {
expect(chart.filterPrinter()).not.toBeNull();
});
it('should show the filter info', () => {
expect(chart.select('span.filter').style('visibility')).toBe('visible');
});
it('should set filter text after slice selection', () => {
expect(chart.select('span.filter').text()).toBe('[06/01/2012 -> 06/30/2012]');
});
describe('when a brush is defined', () => {
it('should position the brush with an offset', () => {
expect(chart.select('g.brush').attr('transform')).toMatchTranslate(chart.margins().left, 10);
});
it('should create a fancy brush resize handle', () => {
const selectAll = chart.select('g.brush').selectAll('path.custom-brush-handle');
expect(selectAll.size()).toBe(2);
selectAll.each(function (d, i) {
if (i === 0) {
expect(d3.select(this).attr('d'))
.toMatchPath('M-0.5,53 A6,6 0 0 0 -6.5,59 V100 A6,6 0 0 0 -0.5,106 ZM-2.5,61 V98 M-4.5,61 V98');
} else {
expect(d3.select(this).attr('d'))
.toMatchPath('M0.5,53 A6,6 0 0 1 6.5,59 V100 A6,6 0 0 1 0.5,106 ZM2.5,61 V98 M4.5,61 V98');
}
});
});
it('should stretch the background', () => {
expect(+chart.select('g.brush rect.overlay').attr('width')).toBe(1020);
});
it('should set the background height to the chart height', () => {
expect(+chart.select('g.brush rect.overlay').attr('height')).toBe(160);
});
it('should set extent height to the chart height', () => {
expect(+chart.select('g.brush rect.selection').attr('height')).toBe(160);
});
it('should set extent width based on filter set', () => {
expect(chart.select('g.brush rect.selection').attr('width')).toBeWithinDelta(81, 1);
});
it('should push unselected bars to the background', () => {
expect(nthStack(0).nthBar(0).classed('deselected')).toBeTruthy();
expect(nthStack(0).nthBar(1).classed('deselected')).toBeFalsy();
expect(nthStack(0).nthBar(3).classed('deselected')).toBeTruthy();
});
it('should push the selected bars to the foreground', () => {
expect(nthStack(0).nthBar(1).classed('deselected')).toBeFalsy();
});
describe('after reset', () => {
beforeEach(() => {
chart.filterAll();
chart.redraw();
});
it('should push all bars to the foreground', () => {
chart.selectAll('rect.bar').each(function () {
const bar = d3.select(this);
expect(bar.classed('deselected')).toBeFalsy();
});
});
});
});
});
describe('a chart with a large domain', () => {
beforeEach(() => {
chart.x(d3.scaleUtc().domain([makeDate(2000, 0, 1), makeDate(2012, 11, 31)]));
});
describe('when filters are applied', () => {
beforeEach(() => {
data.dimension(d => d.value).filter(66);
chart.redraw();
});
it('should not deselect any bars', () => {
forEachBar(bar => {
expect(bar.classed('deselected')).toBeFalsy();
});
});
it('should set the bars to the minimum bar width', () => {
forEachBar(bar => {
expect(+bar.attr('width')).toBe(1);
});
});
});
});
describe('a chart with a linear numerical domain', () => {
beforeEach(() => {
const numericalDimension = data.dimension(d => +d.value);
chart.dimension(numericalDimension).group(numericalDimension.group());
chart.x(d3.scaleLinear().domain([10, 80])).elasticY(true);
chart.render();
});
it('should base the y-axis height on the maximum value in the data', function () {
const yAxisMax = 3.0;
const ticks = chart.selectAll('g.y g.tick');
const tickValues = ticks.nodes().map(tick => +d3.select(tick).text());
const maxTickValue = Math.max.apply(this, tickValues);
expect(maxTickValue).toBe(yAxisMax);
});
describe('when filters are applied', () => {
beforeEach(() => {
data.dimension(d => d.countrycode).filter('CA');
chart.redraw();
});
it('should rescale the y-axis after applying a filter', function () {
const yAxisMax = 1.0;
const ticks = chart.selectAll('g.y g.tick');
const tickValues = ticks.nodes().map(tick => +d3.select(tick).text());
const maxTickValue = Math.max.apply(this, tickValues);
expect(maxTickValue).toBe(yAxisMax);
});
});
});
});
describe('with another ordinal domain', () => {
beforeEach(() => {
const rows = [];
rows.push({State: 'CA', 'Population': 2704659});
rows.push({State: 'TX', 'Population': 1827307});
data = crossfilter(rows);
dimension = data.dimension(dc.pluck('State'));
group = dimension.group().reduceSum(dc.pluck('Population'));
chart = new dc.BarChart(`#${id}`);
chart.xUnits(dc.units.ordinal)
.x(d3.scaleBand())
.transitionDuration(0)
.dimension(dimension)
.group(group, 'Population');
chart.render();
});
it('should not overlap bars', () => {
const x = numAttr('x'), wid = numAttr('width');
expect(x(nthStack(0).nthBar(0)) + wid(nthStack(0).nthBar(0)))
.toBeLessThan(x(nthStack(0).nthBar(1)));
});
});
describe('with yetnother ordinal domain', () => {
beforeEach(() => {
const rows = [{
name: 'Venezuela',
sale: 300
}, {
name: 'Saudi',
sale: 253
}, {
name: 'Canada',
sale: 150
}, {
name: 'Iran',
sale: 125
}, {
name: 'Russia',
sale: 110
}, {
name: 'UAE',
sale: 90
}, {
name: 'US',
sale: 40
}, {
name: 'China',
sale: 37
}];
data = crossfilter(rows);
dimension = data.dimension(d => d.name);
group = dimension.group().reduceSum(d => d.sale);
chart = new dc.BarChart(`#${id}`);
chart.transitionDuration(0)
.outerPadding(0)
.dimension(dimension)
.group(group)
.x(d3.scaleBand())
.xUnits(dc.units.ordinal);
chart.render();
});
it('should not overlap bars', () => {
for (let i = 0; i < 7; ++i) {
checkBarOverlap(i);
}
});
});
describe('with changing number of bars', () => {
beforeEach(() => {
const rows1 = [
{x: 1, y: 3},
{x: 2, y: 9},
{x: 5, y: 10},
{x: 6, y: 7}
];
data = crossfilter(rows1);
dimension = data.dimension(d => d.x);
group = dimension.group().reduceSum(d => d.y);
chart = new dc.BarChart(`#${id}`);
chart.width(500).transitionDuration(0)
.x(d3.scaleLinear().domain([0,7]))
.elasticY(true)
.dimension(dimension)
.group(group);
chart.render();
});
it('should not overlap bars', () => {
for (let i = 0; i < 3; ++i) {
checkBarOverlap(i);
}
});
describe('with bars added', () => {
beforeEach(() => {
const rows2 = [
{x: 7, y: 4},
{x: 12, y: 9}
];
data.add(rows2);
chart.x().domain([0,13]);
chart.render();
});
it('should not overlap bars', () => {
for (let i = 0; i < 5; ++i) {
checkBarOverlap(i);
}
});
});
});
describe('with elasticX and x-axis padding', () => {
const date = makeDate(2012, 5, 1);
beforeEach(() => {
const rows = [
{x: date, y: 4},
];
data = crossfilter(rows);
dimension = data.dimension(d => d.x);
group = dimension.group().reduceSum(d => d.y);
chart = new dc.BarChart(`#${id}`);
chart.width(500)
.transitionDuration(0)
.x(d3.scaleUtc())
.elasticY(true).elasticX(true)