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
| const format = (data, w) => {
const pairs = d3.pairs(data);
const deltas = pairs.flatMap(e => {
let sig = e.toString()
if (sig == '0,0') {
return [[1,0]];
} else if (sig == '0,1') {
return [[1,0],[0,-1]];
} else if (sig == '1,0') {
return [[1,0],[0,1]];
} else if (sig == '1,1') {
return [[1,0]];
} else {
throw new Error('invalid element.');
}
});
const points = deltas.reduce((acc, e) => {
const back = acc[acc.length - 1].slice();
back[0] += w * e[0];
back[1] += w * e[1];
return acc.concat([back])
}, [[0,0]]);
return points;
};
const [svgWidth, svgHeight] = [800, 800];
const svg = d3.select('svg')
.attr('width', svgWidth)
.attr('height', svgHeight);
const pad = 70;
const render = (data) => {
svg.selectAll('*').remove();
svg.append('path')
.datum(data)
.attr('transform', `translate(${pad}, ${pad})`)
.attr('stroke', 'black')
.attr('fill', 'none')
.attr('d', d3.line()
.x(d => d[0])
.y(d => d[1]));
};
render(format([0,0,1,0,1,0,1,1,1,1,0,0], 50));
|