Skip to content

Commit 7760652

Browse files
dopryÆndrew Rininsland
authored andcommitted
PR c3js#1572 -- Add tests for path rendering instructions.
1 parent c50efe2 commit 7760652

File tree

2 files changed

+92
-18
lines changed

2 files changed

+92
-18
lines changed

spec/shape.line-spec.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
describe('c3 chart shape line', function () {
22
'use strict';
33

4+
5+
46
var chart, args;
57

68
beforeEach(function (done) {
@@ -21,6 +23,16 @@ describe('c3 chart shape line', function () {
2123
}
2224
};
2325
expect(true).toBeTruthy();
26+
27+
});
28+
29+
it("Should render the lines correctly", function(done) {
30+
setTimeout(function () {
31+
var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
32+
var commands = parseSvgPath( target.select('.c3-line-data1').attr('d'));
33+
expect(commands.length).toBe(6);
34+
done();
35+
}, 500);
2436
});
2537

2638
it("should not have shape-rendering when it's line chart", function () {
@@ -79,30 +91,43 @@ describe('c3 chart shape line', function () {
7991
}, 500);
8092
});
8193

82-
it('should change args to include null data on scatter plot', function () {
83-
args = {
84-
data: {
85-
columns: [
86-
['data1', 30, null, 100, 400, -150, 250],
87-
['data2', 50, 20, 10, 40, 15, 25],
88-
['data3', -150, 120, 110, 140, 115, 125]
89-
],
90-
type: 'scatter'
91-
}
92-
};
93-
expect(true).toBeTruthy();
94-
});
95-
96-
it('should not show the circle for null', function (done) {
94+
it('should not draw a line segment for null data', function(done) {
9795
setTimeout(function () {
9896
var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
99-
expect(+target.select('.c3-circle-0').style('opacity')).toBe(0.5);
100-
expect(+target.select('.c3-circle-1').style('opacity')).toBe(0);
101-
expect(+target.select('.c3-circle-2').style('opacity')).toBe(0.5);
97+
var commands = parseSvgPath( target.select('.c3-line-data1').attr('d'));
98+
var segments = 0;
99+
for(var i = 0; i < commands.length; i++) {
100+
(commands[i].command == 'L') ? segments++ : null;
101+
}
102+
expect(segments).toBe(3);
102103
done();
103104
}, 500);
104105
});
105106

107+
// it('should change args to include null data on scatter plot', function () {
108+
// args = {
109+
// data: {
110+
// columns: [
111+
// ['data1', 30, null, 100, 400, -150, 250],
112+
// ['data2', 50, 20, 10, 40, 15, 25],
113+
// ['data3', -150, 120, 110, 140, 115, 125]
114+
// ],
115+
// type: 'scatter'
116+
// }
117+
// };
118+
// expect(true).toBeTruthy();
119+
// });
120+
121+
// it('should not show the circle for null', function (done) {
122+
// setTimeout(function () {
123+
// var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
124+
// expect(+target.select('.c3-circle-0').style('opacity')).toBe(0.5);
125+
// expect(+target.select('.c3-circle-1').style('opacity')).toBe(0);
126+
// expect(+target.select('.c3-circle-2').style('opacity')).toBe(0.5);
127+
// done();
128+
// }, 500);
129+
// });
130+
106131
});
107132

108133
describe('spline.interpolation option', function () {
@@ -123,6 +148,7 @@ describe('c3 chart shape line', function () {
123148
}
124149
}
125150
};
151+
126152
expect(true).toBeTruthy();
127153
});
128154

spec/svg-helper.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Parse the d property of an SVG path into an array of drawing commands.
4+
* @param {String} d SvgPath d attribute.]
5+
* @return {Array} an array of drawing commands.
6+
*/
7+
function parseSvgPath(d) {
8+
var commands = [];
9+
var commandTokens = ['M','L','I','H','V','C','S','Q','T','A'];
10+
var command;
11+
var in_x = false;
12+
var in_y = false;
13+
var x = '';
14+
var y = '';
15+
for(var i=0; i <= d.length; i++) {
16+
if (commandTokens.indexOf(d[i]) !== -1) {
17+
if (in_x || in_y) {
18+
commands.push({command: command, x: x, y: y});
19+
x = '';
20+
y = '';
21+
}
22+
command = d[i];
23+
in_x = true;
24+
in_y = false;
25+
}
26+
else {
27+
if (d[i] == ',') {
28+
if (in_y) {
29+
commands.push({command: command, x: x, y: y});
30+
x = '';
31+
y = '';
32+
}
33+
in_x = !in_x;
34+
in_y = !in_y;
35+
}
36+
else if (in_x) {
37+
x += d[i];
38+
}
39+
else if (in_y) {
40+
y += d[i];
41+
}
42+
}
43+
}
44+
if (d[i] != ',' && in_y) {
45+
commands.push({command: command, x: x, y: y});
46+
}
47+
return commands;
48+
}

0 commit comments

Comments
 (0)