-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathPie.js
More file actions
153 lines (133 loc) · 4.49 KB
/
Copy pathPie.js
File metadata and controls
153 lines (133 loc) · 4.49 KB
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
import select from 'd3-selection/src/select';
import mouse from 'd3-selection/src/mouse';
import pie from 'd3-shape/src/pie';
import arc from 'd3-shape/src/arc';
import Tooltip from './components/Tooltip';
import addLegend from './utils/addLegend';
import addLabels from './utils/addLabels';
import addFont from './utils/addFont';
import addFilter from './utils/addFilter';
import colors from './utils/colors';
import config from './config';
const margin = 50;
class Pie {
constructor(svg, {
title, data: { labels, datasets }, options,
}) {
this.options = {
unxkcdify: false,
innerRadius: 0.5,
legendPosition: config.positionType.upLeft,
dataColors: colors,
fontFamily: 'xkcd',
strokeColor: 'black',
backgroundColor: 'white',
showLegend: true,
...options,
};
this.title = title;
this.data = {
labels,
datasets,
};
this.filter = 'url(#xkcdify-pie)';
this.fontFamily = this.options.fontFamily || 'xkcd';
if (this.options.unxkcdify) {
this.filter = null;
this.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif';
}
this.svgEl = select(svg)
.style('stroke-width', '3')
.style('font-family', this.fontFamily)
.style('background', this.options.backgroundColor)
.attr('width', svg.parentElement.clientWidth)
.attr('height', Math.min((svg.parentElement.clientWidth * 2) / 3, window.innerHeight));
this.svgEl.selectAll('*').remove();
this.width = this.svgEl.attr('width');
this.height = this.svgEl.attr('height');
this.chart = this.svgEl.append('g')
.attr('transform',
`translate(${this.width / 2},${this.height / 2})`);
addFont(this.svgEl);
addFilter(this.svgEl);
this.render();
}
render() {
if (this.title) {
addLabels.title(this.svgEl, this.title, this.options.strokeColor);
}
const tooltip = new Tooltip({
parent: this.svgEl,
title: 'tooltip',
items: [{ color: 'red', text: 'weweyang: 12' }, { color: 'blue', text: 'timqian: 13' }],
position: { x: 30, y: 30, type: config.positionType.upRight },
unxkcdify: this.options.unxkcdify,
strokeColor: this.options.strokeColor,
backgroundColor: this.options.backgroundColor,
});
const radius = Math.min(this.width, this.height) / 2 - margin;
const thePie = pie();
const dataReady = thePie(this.data.datasets[0].data);
const theArc = arc()
.innerRadius(radius
* (this.options.innerRadius === undefined ? 0.5 : this.options.innerRadius))
.outerRadius(radius);
this.chart.selectAll('.xkcd-chart-arc')
.data(dataReady)
.enter()
.append('path')
.attr('class', '.xkcd-chart-arc')
.attr('d', theArc)
.attr('fill', 'none')
.attr('stroke', this.options.strokeColor)
.attr('stroke-width', 2)
.attr('fill', (d, i) => this.options.dataColors[i])
.attr('filter', this.filter)
// .attr("fill-opacity", 0.6)
.on('mouseover', (d, i, nodes) => {
select(nodes[i]).attr('fill-opacity', 0.6);
tooltip.show();
})
.on('mouseout', (d, i, nodes) => {
select(nodes[i]).attr('fill-opacity', 1);
tooltip.hide();
})
.on('mousemove', (d, i, nodes) => {
const tipX = mouse(nodes[i])[0] + (this.width / 2) + 10;
const tipY = mouse(nodes[i])[1] + (this.height / 2) + 10;
tooltip.update({
title: this.data.labels[i],
items: [{
color: this.options.dataColors[i],
text: `${this.data.datasets[0].label || ''}: ${d.data}`,
}],
position: {
x: tipX,
y: tipY,
type: config.positionType.downRight,
},
});
});
// Legend
const legendItems = this.data.datasets[0].data
.map((data, i) => ({ color: this.options.dataColors[i], text: this.data.labels[i] }));
// move legend down to prevent overlaping with title
const legendG = this.svgEl.append('g')
.attr('transform', 'translate(0, 30)');
if (this.options.showLegend) {
addLegend(legendG, {
items: legendItems,
position: this.options.legendPosition,
unxkcdify: this.options.unxkcdify,
parentWidth: this.width,
parentHeight: this.height,
strokeColor: this.options.strokeColor,
backgroundColor: this.options.backgroundColor,
});
}
}
// TODO: update chart
update() {
}
}
export default Pie;