/
shape.arc-spec.js
692 lines (588 loc) 路 16.8 KB
/
shape.arc-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
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
/* eslint-disable */
import {selectAll as d3SelectAll} from "d3-selection";
import CLASS from "../../src/config/classes";
import util from "../assets/util";
import {getBoundingRect} from "../../src/internals/util";
describe("SHAPE ARC", () => {
const selector = {
arc: `.${CLASS.chartArc}.${CLASS.target}.${CLASS.target}`,
shapes: `g.${CLASS.shapes}.${CLASS.arcs}.${CLASS.arcs}`,
shape: `path.${CLASS.shape}.${CLASS.arc}.${CLASS.arc}`
};
describe("show pie chart", () => {
const chart = util.generate({
data: {
columns: [
["data1", 30],
["data2", 150],
["data3", 120]
],
type: "pie"
}
});
it("should have correct classes", () => {
const chartArc = chart.$.main.select(`.${CLASS.chartArcs}`);
const selector = {
arc: `.${CLASS.chartArc}.${CLASS.target}.${CLASS.target}`,
shapes: `g.${CLASS.shapes}.${CLASS.arcs}.${CLASS.arcs}`,
shape: `path.${CLASS.shape}.${CLASS.arc}.${CLASS.arc}`
}
const arcs = {
data1: chartArc.select(`${selector.arc}-data1`)
.select(`${selector.shapes}-data1`)
.select(`${selector.shape}-data1`),
data2: chartArc.select(`${selector.arc}-data2`)
.select(`${selector.shapes}-data2`)
.select(`${selector.shape}-data2`),
data3: chartArc.select(`${selector.arc}-data3`)
.select(`${selector.shapes}-data3`)
.select(`${selector.shape}-data3`)
};
expect(arcs.data1.size()).to.be.equal(1);
expect(arcs.data2.size()).to.be.equal(1);
expect(arcs.data3.size()).to.be.equal(1);
});
it("should have correct d", () => {
const main = chart.$.main;
expect(main.select(`.${CLASS.arc}-data1`).attr("d"))
.to.match(/M-124\..+,-171\..+A211\..+,211\..+,0,0,1,-3\..+,-211\..+L0,0Z/);
expect(main.select(`.${CLASS.arc}-data2`).attr("d"))
.to.match(/M1\..+,-211\..+A211\..+,211\..+,0,0,1,1\..+,211\..+L0,0Z/);
expect(main.select(`.${CLASS.arc}-data3`).attr("d"))
.to.match(/M1\..+,211\..+A211\..+,211\..+,0,0,1,-124\..+,-171\..+L0,0Z/);
});
it("check when hiding data", () => {
const arc = chart.$.arc;
let total = 0;
// when
chart.hide("data1");
chart.data.shown().map(v => v.id)
.forEach(id => total += parseFloat(arc.select(`.${CLASS.target}-${id} text`).text()));
expect(total).to.be.equal(100);
});
it("should have correct d even if data id can be converted to a color", done => {
const chart = util.generate({
data: {
columns: [
["black", 30],
["data2", 150],
["data3", 120]
],
type: "pie"
}
});
setTimeout(() => {
expect(chart.$.main.select(`.${CLASS.arc}-black`).attr("d"))
.to.match(/M-124\..+,-171\..+A211\..+,211\..+,0,0,1,-3\..+,-211\..+L0,0Z/);
done();
}, 500);
});
});
describe("Check attribute", () => {
const chart = util.generate({
data: {
columns: [
["data1", null],
["data2", null],
["data3", null]
],
type: "pie"
}
});
it("should have correct d attribute", () => {
const chartArc = chart.$.main.select(`.${CLASS.chartArcs}`);
const arcs = {
data1: chartArc.select(`${selector.arc}-data1`)
.select(`${selector.shapes}-data1`)
.select(`${selector.shape}-data1`),
data2: chartArc.select(`${selector.arc}-data2`)
.select(`${selector.shapes}-data2`)
.select(`${selector.shape}-data2`),
data3: chartArc.select(`${selector.arc}-data3`)
.select(`${selector.shapes}-data3`)
.select(`${selector.shape}-data3`)
};
expect(arcs.data1.attr("d").indexOf("NaN")).to.be.equal(-1);
expect(arcs.data2.attr("d").indexOf("NaN")).to.be.equal(-1);
expect(arcs.data3.attr("d").indexOf("NaN")).to.be.equal(-1);
});
it("check for donut's options", () => {
const value = 0.05;
const chart = util.generate({
data: {
columns: [
["data1", 60],
["data2", 40]
],
type: "donut"
},
donut: {
title: "text1\ntext2\ntext3",
padAngle: value
}
});
expect(chart.internal.pie.padAngle()()).to.be.equal(value);
expect(chart.$.main.selectAll(`text.${CLASS.chartArcsTitle} tspan`).size()).to.be.equal(3);
d3SelectAll(`.${CLASS.chartArc} text`).each(function(d) {
const value = parseInt(this.textContent);
expect(value).to.be.equal(d.value);
});
});
it("check for padding and innerRadius", () => {
const padding = 5;
const innerRadius = 20;
const chart = util.generate({
data: {
columns: [
["data1", 60],
["data2", 40]
],
type: "pie"
},
pie: {
padding,
innerRadius,
format: value => `$$\n${value}`
}
});
const internal = chart.internal;
expect(internal.pie.padAngle()()).to.be.equal(padding * 0.01);
expect(internal.innerRadius).to.be.equal(innerRadius);
d3SelectAll(`.${CLASS.chartArc} text`).each(function(d) {
const value = parseInt(this.textContent);
expect(value).to.be.equal(d.value);
});
});
it("check for variant innerRadius", done => {
const innerRadius = {
data1: 50,
data2: 80,
data3: 0
};
const chart = util.generate({
data: {
columns: [
["data1", 30],
["data2", 50],
["data3", 20]
],
type: "pie"
},
pie: {
innerRadius
}
});
const expected = {
data1: "M-8.110822788676742e-14,211.85A211.85,211.85,0,0,1,-201.48132297712823,-65.46525025833276L-47.55282581475767,-15.450849718747406A50,50,0,0,0,-1.9142843494634746e-14,50Z",
data2: "M1.2972071219968338e-14,-211.85A211.85,211.85,0,1,1,-8.110822788676742e-14,211.85L-3.06285495914156e-14,80A80,80,0,1,0,4.898587196589413e-15,-80Z",
data3: "M-201.48132297712823,-65.46525025833276A211.85,211.85,0,0,1,1.4924438455356651e-13,-211.85L0,0Z"
};
expect(chart.internal.innerRadius).to.be.deep.equal(innerRadius);
setTimeout(() => {
chart.$.arc.selectAll("path").each(function(d) {
expect(this.getAttribute("d")).to.be.equal(expected[d.data.id]);
});
done();
}, 500);
});
});
describe("show gauge", () => {
it("should have correct d for Pi radian gauge", done => {
const chart = util.generate({
gauge: {
width: 10,
max: 10,
expand: true
},
data: {
columns: [
["data", 8]
],
type: "gauge"
}
});
const chartArc = chart.$.main.select(`.${CLASS.chartArcs}`);
const data = chartArc.select(`${selector.arc}-data`)
.select(`${selector.shapes}-data`)
.select(`${selector.shape}-data`);
setTimeout(() => {
expect(data.attr("d"))
.to.match(/M-304,-3\..+A304,304,0,0,1,245\..+,-178\..+L237\..+,-172\..+A294,294,0,0,0,-294,-3\..+Z/);
expect(chartArc.select(`.${CLASS.gaugeValue}`).attr("dy")).to.be.equal("-.1em");
done();
}, 500);
});
it("should have correct d for 2 Pi radian gauge starting at Pi/2", done => {
const chart = util.generate({
gauge: {
width: 10,
max: 10,
expand: true,
fullCircle: true
},
data: {
columns: [
["data", 8]
],
type: "gauge"
}
});
const chartArc = chart.$.main.select(`.${CLASS.chartArcs}`);
const data = chartArc.select(`${selector.arc}-data`)
.select(`${selector.shapes}-data`)
.select(`${selector.shape}-data`);
setTimeout(() => {
// This test has bee updated to make tests pass. @TODO double-check this test is accurate.
expect(data.attr("d"))
.to.match(/M-221.*?,-2\..+A221.*?,221.*?,0,1,1,-68.*?,210.*?L-65.*?,201.*?A211.*?,211.*?,0,1,0,-211.*?,-2.*?Z/);
done();
}, 500);
});
it("should show custom gauge labels", () => {
const chart = util.generate({
gauge: {
width: 10,
max: 100,
expand: true,
label: {
extents: (value, isMax) => {
if (isMax) {
return `Max: ${value}%`;
}
return `Min: ${value}%`;
},
format: function(value) {
return value +"%\nTest\nValue";
}
}
},
data: {
columns: [
["data", 8]
],
type: "gauge"
}
});
const chartArc = chart.$.arc;
const min = chartArc.select(`.${CLASS.chartArcsGaugeMin}`);
const max = chartArc.select(`.${CLASS.chartArcsGaugeMax}`);
expect(min.text()).to.equal("Min: 0%");
expect(max.text()).to.equal("Max: 100%");
// gauge text should be multilined
expect(chartArc.selectAll(`.${CLASS.target}-data tspan`).size()).to.be.equal(3);
});
it("should not show gauge labels", () => {
const chart = util.generate({
gauge: {
label: {
show: false
}
},
data: {
columns: [
["data", 75]
],
type: "gauge"
}
});
const chartArc = chart.$.main.select(`.${CLASS.chartArcs}`);
const min = chartArc.select(`.${CLASS.chartArcsGaugeMin}`);
const max = chartArc.select(`.${CLASS.chartArcsGaugeMax}`)
expect(min.empty()).to.be.true;
expect(max.empty()).to.be.true;
});
it("check for fullCircle option", done => {
const chart = util.generate({
gauge: {
width: 10,
max: 10,
expand: true,
fullCircle: true
},
data: {
columns: [
["data", 10]
],
type: "gauge"
}
});
setTimeout(() => {
const chartArc = chart.$.main.select(`.${CLASS.chartArcs}`);
const min = chartArc.select(`.${CLASS.chartArcsGaugeMin}`);
const max = chartArc.select(`.${CLASS.chartArcsGaugeMax}`);
// check if gauge value text is centered
expect(+chartArc.select(`.${CLASS.gaugeValue}`).attr("dy")).to.be.above(0);
// check background height
expect(chartArc.select(`.${CLASS.chartArcsBackground}`).node().getBBox().height).to.be.above(400);
// with fullCircle option, only min text is showed
expect(min.empty()).to.be.false;
expect(max.empty()).to.be.true;
done();
}, 100);
});
it("check for stack data #1", done => {
const chart = util.generate({
size: {
width: 640,
height: 480
},
data: {
columns: [
["data1", 50, 10],
["data2", 200],
["data3", 70],
["data4", 50],
["data5", 50],
],
type: "gauge",
order: null
}
});
const expected = [
"M-304,-3.7229262694079536e-14A304,304,0,0,1,-275.25626419791655,-129.03483645824778L-165.15375851874995,-77.42090187494867A182.4,182.4,0,0,0,-182.4,-2.2337557616447722e-14Z",
"M-275.25626419791655,-129.03483645824778A304,304,0,0,1,98.15564305051961,-287.71769103991323L58.893385830311765,-172.63061462394796A182.4,182.4,0,0,0,-165.15375851874995,-77.42090187494867Z",
"M98.15564305051961,-287.71769103991323A304,304,0,0,1,226.4107435541097,-202.86491861156077L135.84644613246581,-121.71895116693646A182.4,182.4,0,0,0,58.893385830311765,-172.63061462394796Z",
"M226.4107435541097,-202.86491861156077A304,304,0,0,1,283.9408970546946,-108.59819049954436L170.36453823281676,-65.15891429972662A182.4,182.4,0,0,0,135.84644613246581,-121.71895116693646Z",
"M283.9408970546946,-108.59819049954436A304,304,0,0,1,304,0L182.4,0A182.4,182.4,0,0,0,170.36453823281676,-65.15891429972662Z"
];
setTimeout(() => {
chart.$.arc.selectAll(`.${CLASS.target} path`).each(function(d, i) {
expect(this.getAttribute("d")).to.be.equal(expected[i]);
});
done();
}, 100);
});
it("check for stack data #2", done => {
const args = {
size: {
width: 640,
height: 480
},
data: {
columns: [
["data1", 50],
["data2", 200]
],
type: "gauge",
order: "desc"
},
gauge: {
min: 50,
max: 550,
title: "TitleText"
}
};
const chart = util.generate(args);
const expected = [
"M-93.941166289984,-289.1211809537267A304,304,0,0,1,1.8614631347039768e-14,-304L1.1168778808223861e-14,-182.4A182.4,182.4,0,0,0,-56.364699773990395,-173.47270857223603Z",
"M-304,-3.7229262694079536e-14A304,304,0,0,1,-93.941166289984,-289.1211809537267L-56.364699773990395,-173.47270857223603A182.4,182.4,0,0,0,-182.4,-2.2337557616447722e-14Z"
];
setTimeout(() => {
const data = chart.data();
expect(chart.$.arc.select(`.${CLASS.chartArcsGaugeTitle}`).text()).to.be.equal(args.gauge.title);
chart.$.arc.selectAll(`.${CLASS.target} path`).each(function(d, i) {
expect(d.value).to.be.equal(data[i].values[0].value);
expect(this.getAttribute("d")).to.be.equal(expected[i]);
});
done();
}, 100);
});
it("check for startingAngle option", () => {
const chart = util.generate({
data: {
columns: [
["data", 50]
],
type: "gauge"
},
gauge: {
startingAngle: 0
}
});
const arc = chart.$.arc;
// gauge backgound shouldn't be aligned with the 'startingAngle' option
expect(
getBoundingRect(arc.select(`.${CLASS.chartArcsBackground}`).node()).width
).to.be.above(600);
expect(
arc.select(`.${CLASS.arc}-data`).datum().startAngle
).to.be.equal(
chart.config("gauge.startingAngle")
);
});
});
describe("check for interaction", () => {
let chart;
const spyOver = sinon.spy();
const spyOut = sinon.spy();
before(() => {
chart = util.generate({
data: {
columns: [
["data1", 30],
["data2", 150],
["data3", 120]
],
type: "pie",
onover: spyOver,
onout: spyOut
}
});
});
/*beforeEach(() => {
spyOver.resetHistory();
spyOut.resetHistory();
});*/
it("should interact properly for mouseover & mouseout", done => {
setTimeout(() => {
const path = chart.$.main.select(`path.${CLASS.arc}-data2`).node();
util.fireEvent(path, "mouseover", {
clientX: 500,
clientY: 200
}, chart);
util.fireEvent(path, "mouseout", {
clientX: 0,
clientY: 0
}, chart);
expect(spyOver.calledOnce).to.be.true;
expect(spyOut.calledOnce).to.be.true;
expect(chart.$.tooltip.select(".value").text()).to.be.equal("50.0%");
done();
}, 500);
});
});
describe("check for 0 or null value rendering", () => {
let chart;
before(() => {
chart = util.generate({
data: {
columns: [["data1", 100], ["data2", 0], ["data3", null]],
type: "donut"
},
donut: {
title: "Iris Petal Width"
}
});
});
it("the dimension should be 0x0", () => {
["data2", "data3"].forEach(id => {
const rect = chart.$.arc.select(`.${CLASS.arc}-${id}`).node().getBBox();
expect(rect.width === 0).to.be.true;
expect(rect.height === 0).to.be.true;
});
});
});
describe("check for multiline label format", () => {
const labelFormat = {
label: { format: value => `$$\n${value}` }
};
let chart;
let args = {
data: {
columns: [
["data1", 60],
["data2", 40]
],
type: "pie"
},
pie: labelFormat,
donut: labelFormat
};
const checkMultiline = arc => {
arc.selectAll("text").each(function() {
expect(this.querySelectorAll("tspan").length).to.be.equal(2);
});
}
beforeEach(() => {
chart = util.generate(args);
});
it("should be multilined in pie", () => {
checkMultiline(chart.$.arc);
chart.toggle("data1");
chart.toggle("data1");
checkMultiline(chart.$.arc);
});
it("set options data.type='donut'", () => {
args.data.type = "donut";
});
it("should be multilined in donut", () => {
checkMultiline(chart.$.arc);
});
});
describe("check for multiline text position", () => {
let chart;
let args = {
data: {
columns: [
["data1", 50],
["data2", 50]
],
type: "donut"
},
donut: {
title: "Title 1\nTitle 2"
}
};
beforeEach(() => {
chart = util.generate(args);
});
const checkAtMiddle = done => {
const arc = chart.$.arc.node().getBoundingClientRect();
const title = chart.$.arc.select("text").node().getBoundingClientRect();
const titlePos = title.top - arc.top + (title.height / 2);
expect(titlePos).to.be.closeTo(arc.height / 2, 2);
done && done();
};
it("check for two lined text position", done => {
setTimeout(() => {
checkAtMiddle(done);
}, 100);
});
it("set option args.donut.title", () => {
args.donut.title = "Title 1\nTitle 2\nTitle 3";
});
it("check for three lined text position", done => {
setTimeout(() => {
checkAtMiddle(done);
}, 100);
});
});
describe("check for data loading", () => {
it("Interaction of chart when initialized with 0 and .load()", done => {
const chart = util.generate({
data: {
columns: [
["data1", 0],
["data2", 0],
],
type: "pie",
}
});
setTimeout(function() {
chart.load({
columns: [
["data1", 3],
["data2", 6],
],
done: () => {
const legend = chart.$.legend.select(`.${CLASS.legendItem}-data2`).node();
util.fireEvent(legend, "mouseover");
util.fireEvent(legend, "mouseout");
setTimeout(() => {
chart.$.arc.selectAll("path").each(function() {
const rect = this.getBoundingClientRect();
expect(this.getAttribute("d")).to.not.be.equal("M 0 0");
expect(rect.width > 0 && rect.height > 0).to.be.true;
});
done();
}, 1000);
}
});
}, 1000);
});
});
});