-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
70 lines (56 loc) · 1.47 KB
/
index.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
const D3Node = require('d3-node');
function pie({
data,
selector: _selector = '#chart',
container: _container = `
<div id="container">
<h2>Pie Chart</h2>
<div id="chart"></div>
</div>
`,
style: _style = '',
colorRange: _colorRange = undefined,
width: _width = 960,
height: _height = 500,
radius: _radius = 200
} = {}) {
const _svgStyles = `
.arc text {font: 10px sans-serif; text-anchor: middle;}
.arc path {stroke: #fff;}
`;
const d3n = new D3Node({
selector: _selector,
styles: _svgStyles + _style,
container: _container
});
const d3 = d3n.d3;
const radius = _radius;
const color = d3.scaleOrdinal(_colorRange ? _colorRange : d3.schemeCategory10);
const arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(0);
const labelArc = d3.arc()
.outerRadius(radius - 40)
.innerRadius(radius - 40);
const pie = d3.pie()
.sort(null)
.value((d) => d.value);
const svg = d3n.createSVG()
.attr('width', _width)
.attr('height', _height)
.append('g')
.attr('transform', `translate( ${_radius} , ${_radius} )`);
const g = svg.selectAll('.arc')
.data(pie(data))
.enter().append('g')
.attr('class', 'arc');
g.append('path')
.attr('d', arc)
.style('fill', (d) => color(d.data.label));
g.append('text')
.attr('transform', (d) => `translate(${labelArc.centroid(d)})`)
.attr('dy', '.35em')
.text((d) => d.data.label);
return d3n;
}
module.exports = pie;