/
shape.bar-spec.ts
375 lines (335 loc) · 11.4 KB
/
shape.bar-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
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
import { d3, initChart } from './c3-helper'
describe('c3 chart shape bar', function() {
'use strict'
var chart, args
beforeEach(function(done) {
chart = initChart(chart, args, done)
})
describe('Path boxes', function() {
beforeAll(function() {
args = {
data: {
columns: [['data1', 30]],
type: 'bar'
},
bar: {
width: {
max: 40
}
}
}
})
it('bars should have expected Path Box', function() {
var expected = {
x: 279,
y: 40,
width: 40,
height: 387
}
var shapes = chart.internal.main
.selectAll('.' + chart.internal.CLASS.shapes)
.selectAll('.' + chart.internal.CLASS.shape)
shapes.each(function() {
var pathBox = chart.internal.getPathBox(this)
expect(pathBox.x).toBeCloseTo(expected.x, -1)
expect(pathBox.y).toBeCloseTo(expected.y, -1)
expect(pathBox.width).toBeCloseTo(expected.width, -1)
expect(pathBox.height).toBeCloseTo(expected.height, -1)
})
})
})
describe('with groups', function() {
describe('with indexed data', function() {
beforeAll(function() {
args = {
data: {
columns: [
['data1', 30, 200, -100, 400, -150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
groups: [['data1', 'data2']],
type: 'bar'
}
}
})
it('should be stacked', function() {
var expectedBottom = [275, 293, 365, 281, 395, 290]
chart.internal.main
.selectAll('.c3-bars-data1 .c3-bar')
.each(function(d, i) {
var rect = d3
.select(this)
.node()
.getBoundingClientRect()
expect(rect.bottom).toBeCloseTo(expectedBottom[i], -1)
})
})
})
describe('with timeseries data', function() {
beforeAll(function() {
args = {
data: {
x: 'date',
columns: [
[
'date',
'2012-12-24',
'2012-12-25',
'2012-12-26',
'2012-12-27',
'2012-12-28',
'2012-12-29'
],
['data1', 30, 200, -100, 400, -150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
groups: [['data1', 'data2']],
type: 'bar'
},
axis: {
x: {
type: 'timeseries'
}
}
}
})
it('should be stacked', function() {
var expectedBottom = [275, 293, 365, 281, 395, 290]
chart.internal.main
.selectAll('.c3-bars-data1 .c3-bar')
.each(function(d, i) {
var rect = d3
.select(this)
.node()
.getBoundingClientRect()
expect(rect.bottom).toBeCloseTo(expectedBottom[i], -1)
})
})
})
describe('with category data', function() {
beforeAll(function() {
args = {
data: {
x: 'date',
columns: [
[
'date',
'2012-12-24',
'2012-12-25',
'2012-12-26',
'2012-12-27',
'2012-12-28',
'2012-12-29'
],
['data1', 30, 200, -100, 400, -150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
groups: [['data1', 'data2']],
type: 'bar'
},
axis: {
x: {
type: 'category'
}
}
}
})
it('should be stacked', function() {
var expectedBottom = [275, 293, 365, 281, 395, 290]
chart.internal.main
.selectAll('.c3-bars-data1 .c3-bar')
.each(function(d, i) {
var rect = d3
.select(this)
.node()
.getBoundingClientRect()
expect(rect.bottom).toBeCloseTo(expectedBottom[i], -1)
})
})
})
})
describe('internal.isWithinBar', function() {
describe('with normal axis', 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: 'bar'
},
axis: {
rotated: false
}
}
})
it('should not be within bar', function() {
var bar = d3.select('.c3-target-data1 .c3-bar-0').node()
expect(chart.internal.isWithinBar([0, 0], bar)).toBeFalsy()
})
it('should be within bar', function() {
var bar = d3.select('.c3-target-data1 .c3-bar-0').node()
expect(chart.internal.isWithinBar([31, 280], bar)).toBeTruthy()
})
it('should not be within bar of negative value', function() {
var bar = d3.select('.c3-target-data3 .c3-bar-0').node()
expect(chart.internal.isWithinBar([68, 280], bar)).toBeFalsy()
})
it('should be within bar of negative value', function() {
var bar = d3.select('.c3-target-data3 .c3-bar-0').node()
expect(chart.internal.isWithinBar([68, 350], bar)).toBeTruthy()
})
})
describe('with rotated axis', function() {
beforeAll(function() {
args.axis.rotated = true
})
it('should not be within bar', function() {
var bar = d3.select('.c3-target-data1 .c3-bar-0').node()
expect(chart.internal.isWithinBar([0, 0], bar)).toBeFalsy()
})
it('should be within bar', function() {
var bar = d3.select('.c3-target-data1 .c3-bar-0').node()
expect(chart.internal.isWithinBar([190, 20], bar)).toBeTruthy()
})
it('should be within bar of negative value', function() {
var bar = d3.select('.c3-target-data3 .c3-bar-0').node()
expect(chart.internal.isWithinBar([68, 50], bar)).toBeTruthy()
})
})
})
describe('bar spacing', function() {
var createArgs = function(spacing) {
return {
size: {
width: 500
},
data: {
columns: [
['data1', 30, 200, 100],
['data2', 50, 20, 10],
['data3', 150, 120, 110],
['data4', 12, 24, 20]
],
type: 'bar',
groups: [['data1', 'data4']]
},
bar: {
space: spacing
}
}
}
var getBBox = function(selector) {
return d3
.select(selector)
.node()
.getBBox()
}
var getBarContainerWidth = function() {
return parseInt(getBBox('.c3-chart-bars').width)
}
var getBarContainerOffset = function() {
return parseInt(getBBox('.c3-chart-bars').x)
}
var getBarBBox = function(name, idx) {
return getBBox('.c3-target-' + name + ' .c3-bar-' + (idx || 0))
}
var getBarWidth = function(name, idx) {
return parseInt(getBarBBox(name, idx).width)
}
var getBarOffset = function(name1, name2, idx) {
var bbox1 = getBarBBox(name1, idx)
var bbox2 = getBarBBox(name2, idx)
return Math.floor(bbox2.x - (bbox1.x + bbox1.width))
}
it('should set bar spacing to 0', function() {
args = createArgs(0)
expect(true).toBeTruthy()
})
it('should display the bars without any spacing', function() {
// all bars should have the same width
expect(getBarWidth('data1', 0)).toEqual(30)
expect(getBarWidth('data2', 0)).toEqual(30)
expect(getBarWidth('data3', 0)).toEqual(30)
expect(getBarWidth('data1', 1)).toEqual(30)
expect(getBarWidth('data2', 1)).toEqual(30)
expect(getBarWidth('data3', 1)).toEqual(30)
expect(getBarWidth('data1', 2)).toEqual(30)
expect(getBarWidth('data2', 2)).toEqual(30)
expect(getBarWidth('data3', 2)).toEqual(30)
// all offsets should be the same
expect(getBarOffset('data1', 'data2', 0)).toEqual(0)
expect(getBarOffset('data2', 'data3', 0)).toEqual(0)
expect(getBarOffset('data1', 'data2', 1)).toEqual(0)
expect(getBarOffset('data2', 'data3', 1)).toEqual(0)
expect(getBarOffset('data1', 'data2', 2)).toEqual(0)
expect(getBarOffset('data2', 'data3', 2)).toEqual(0)
// default width/offset of the container for this chart
expect(getBarContainerWidth()).toEqual(396)
expect(getBarContainerOffset()).toEqual(31)
})
it('should set bar spacing to 0.25', function() {
args = createArgs(0.25)
expect(true).toBeTruthy()
})
it('should display the bars with a spacing ratio of 0.25', function() {
// with bar_space of 0.25, the space between bars is
// expected to be 25% of the original bar's width
// which is ~7
// expect all bars to be the same width
expect(getBarWidth('data1', 0)).toEqual(22)
expect(getBarWidth('data2', 0)).toEqual(22)
expect(getBarWidth('data3', 0)).toEqual(22)
expect(getBarWidth('data1', 1)).toEqual(22)
expect(getBarWidth('data2', 1)).toEqual(22)
expect(getBarWidth('data3', 1)).toEqual(22)
expect(getBarWidth('data1', 2)).toEqual(22)
expect(getBarWidth('data2', 2)).toEqual(22)
expect(getBarWidth('data3', 2)).toEqual(22)
// all offsets should be the same
expect(getBarOffset('data1', 'data2', 0)).toEqual(7)
expect(getBarOffset('data2', 'data3', 0)).toEqual(7)
expect(getBarOffset('data1', 'data2', 1)).toEqual(7)
expect(getBarOffset('data2', 'data3', 1)).toEqual(7)
expect(getBarOffset('data1', 'data2', 2)).toEqual(7)
expect(getBarOffset('data2', 'data3', 2)).toEqual(7)
// expect the container to shrink a little because of
// the offsets from the first/last chart
// we add/subtract 1 because of approximation due to rounded values
expect(getBarContainerWidth()).toEqual(396 - 7 - 1)
expect(getBarContainerOffset()).toEqual(31 + (Math.floor(7 / 2) + 1))
})
it('should set bar spacing to 0.5', function() {
args = createArgs(0.5)
expect(true).toBeTruthy()
})
it('should display the bars with a spacing ratio of 0.5', function() {
// with bar_space of 0.5, the space between bars is
// expected to be 50% of the original bar's width
// which is ~15
// expect all bars to be the same width
expect(getBarWidth('data1', 0)).toEqual(15)
expect(getBarWidth('data2', 0)).toEqual(15)
expect(getBarWidth('data3', 0)).toEqual(15)
expect(getBarWidth('data1', 1)).toEqual(15)
expect(getBarWidth('data2', 1)).toEqual(15)
expect(getBarWidth('data3', 1)).toEqual(15)
expect(getBarWidth('data1', 2)).toEqual(15)
expect(getBarWidth('data2', 2)).toEqual(15)
expect(getBarWidth('data3', 2)).toEqual(15)
// all offsets should be the same
expect(getBarOffset('data1', 'data2', 0)).toEqual(15)
expect(getBarOffset('data2', 'data3', 0)).toEqual(15)
expect(getBarOffset('data1', 'data2', 1)).toEqual(15)
expect(getBarOffset('data2', 'data3', 1)).toEqual(15)
expect(getBarOffset('data1', 'data2', 2)).toEqual(15)
expect(getBarOffset('data2', 'data3', 2)).toEqual(15)
// expect the container to shrink a little because of
// the offsets from the first/last chart
expect(getBarContainerWidth()).toEqual(396 - 15)
expect(getBarContainerOffset()).toEqual(31 + Math.floor(15 / 2))
})
})
})