forked from c3js/c3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.line-spec.ts
237 lines (214 loc) · 7.01 KB
/
shape.line-spec.ts
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
import { parseSvgPath } from './svg-helper'
import { d3, initChart } from './c3-helper'
describe('c3 chart shape line', function() {
'use strict'
var chart, args
beforeEach(function(done) {
chart = initChart(chart, args, done)
})
describe('shape-rendering for line chart', function() {
beforeAll(function() {
args = {
data: {
columns: [
['data1', 30, 200, 100, 400, -150, 250],
['data2', 50, 20, 10, 40, 15, 25],
['data3', -150, 120, 110, 140, 115, 125]
],
type: 'line'
},
line: {
step: {
type: 'step'
}
}
}
})
it('Should render the lines correctly', function(done) {
setTimeout(function() {
var target = chart.internal.main.select(
'.c3-chart-line.c3-target-data1'
)
var commands = parseSvgPath(target.select('.c3-line-data1').attr('d'))
expect(commands.length).toBe(6)
done()
}, 500)
})
it("should not have shape-rendering when it's line chart", function() {
d3.selectAll('.c3-line').each(function() {
var style = d3.select(this).style('shape-rendering')
expect(style).toBe('auto')
})
})
describe('should change to step chart', function() {
beforeAll(function() {
args.data.type = 'step'
})
it("should have shape-rendering = crispedges when it's step chart", function() {
d3.selectAll('.c3-line').each(function() {
var style = d3
.select(this)
.style('shape-rendering')
.toLowerCase()
expect(style).toBe('crispedges')
})
})
})
describe('should change to step chart with step-after', function() {
beforeAll(function() {
args.line.step.type = 'step-after'
})
it("should have shape-rendering = crispedges when it's step chart", function() {
d3.selectAll('.c3-line').each(function() {
var style = d3
.select(this)
.style('shape-rendering')
.toLowerCase()
expect(style).toBe('crispedges')
})
})
})
describe('should change to step chart with step-before', function() {
beforeAll(function() {
args.line.step.type = 'step-before'
})
it("should have shape-rendering = crispedges when it's step chart", function() {
d3.selectAll('.c3-line').each(function() {
var style = d3
.select(this)
.style('shape-rendering')
.toLowerCase()
expect(style).toBe('crispedges')
})
})
})
describe('should change to spline chart', function() {
beforeAll(function() {
args.data.type = 'spline'
})
it('should use cardinal interpolation by default', function() {
expect(chart.internal.config.spline_interpolation_type).toBe('cardinal')
})
})
})
describe('point.show option', function() {
describe('should change args to include null data', function() {
beforeAll(function() {
args = {
data: {
columns: [
['data1', 30, null, 100, 400, -150, 250],
['data2', 50, 20, 10, 40, 15, 25],
['data3', -150, 120, 110, 140, 115, 125]
],
type: 'line'
}
}
})
it('should not show the circle for null', function(done) {
setTimeout(function() {
var target = chart.internal.main.select(
'.c3-chart-line.c3-target-data1'
)
expect(+target.select('.c3-circle-0').style('opacity')).toBe(1)
expect(+target.select('.c3-circle-1').style('opacity')).toBe(0)
expect(+target.select('.c3-circle-2').style('opacity')).toBe(1)
done()
}, 500)
})
it('should not draw a line segment for null data', function(done) {
setTimeout(function() {
var target = chart.internal.main.select(
'.c3-chart-line.c3-target-data1'
)
var commands = parseSvgPath(target.select('.c3-line-data1').attr('d'))
var segments = 0
for (var i = 0; i < commands.length; i++) {
commands[i].command === 'L' ? segments++ : null
}
expect(segments).toBe(3)
done()
}, 500)
})
// it('should change args to include null data on scatter plot', function () {
// args = {
// data: {
// columns: [
// ['data1', 30, null, 100, 400, -150, 250],
// ['data2', 50, 20, 10, 40, 15, 25],
// ['data3', -150, 120, 110, 140, 115, 125]
// ],
// type: 'scatter'
// }
// };
// expect(true).toBeTruthy();
// });
// it('should not show the circle for null', function (done) {
// setTimeout(function () {
// var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
// expect(+target.select('.c3-circle-0').style('opacity')).toBe(0.5);
// expect(+target.select('.c3-circle-1').style('opacity')).toBe(0);
// expect(+target.select('.c3-circle-2').style('opacity')).toBe(0.5);
// done();
// }, 500);
// });
})
describe('should allow passing a function', function() {
beforeAll(function() {
args = {
data: {
columns: [['data1', 30, 50, 100]],
type: 'line'
},
point: {
show: function(d) {
return d.value > 50
}
}
}
})
it('should show point if function returns true', function() {
var target = chart.internal.main.select(
'.c3-chart-line.c3-target-data1'
)
expect(+target.select('.c3-circle-0').style('opacity')).toBe(0)
expect(+target.select('.c3-circle-1').style('opacity')).toBe(0)
expect(+target.select('.c3-circle-2').style('opacity')).toBe(1)
})
})
})
describe('spline.interpolation option', function() {
beforeAll(function() {
args = {
data: {
columns: [
['data1', 30, 200, 100, 400, -150, 250],
['data2', 50, 20, 10, 40, 15, 25],
['data3', -150, 120, 110, 140, 115, 125]
],
type: 'spline'
},
spline: {
interpolation: {
type: 'monotone'
}
}
}
})
it('updates interpolation function', function() {
expect(chart.internal.getInterpolate(chart.data()[0])).toBe(
d3.curveMonotoneX
)
})
describe('should not use a non-valid interpolation', function() {
beforeAll(function() {
args.spline.interpolation.type = 'foo'
})
it('should use cardinal interpolation when given option is not valid', function() {
expect(chart.internal.getInterpolate(chart.data()[0])).toBe(
d3.curveCardinal
)
})
})
})
})