-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpie.js
143 lines (119 loc) · 3.47 KB
/
pie.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
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
/**
* DEep WiKi INspector (DEWKIN)
* Copyright (C) 2013-2015 Ricordisamoa
*
* https://meta.wikimedia.org/wiki/User:Ricordisamoa
* https://tools.wmflabs.org/ricordisamoa/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Generate a pie chart.
*
* @param {string} selector CSS selector for the element to put the chart into
* @param {number} x
* @param {number} y
* @param {number} width
* @param {number} height
* @param {number} radius
* @param {Object[]} data Objects storing id, name, value, label and color
* @return {Object} For custom interactions
*/
window.charts.pie = function ( selector, x, y, width, height, radius, data ) {
'use strict';
var arc, arcOver, pie, svg, chart, legend, u, g, paths;
arc = d3.svg.arc()
.outerRadius( radius )
.innerRadius( 0 );
arcOver = d3.svg.arc()
.outerRadius( radius * 1.1 )
.innerRadius( 0 );
pie = d3.layout.pie()
.sort( null )
.value( function ( d ) { return d.value; } );
svg = d3.select( selector )
.append( 'svg' )
.attr( 'width', width )
.attr( 'height', height );
chart = svg
.append( 'g' )
.attr( 'transform', 'translate(' + ( x + radius ) + ',' + ( y + radius ) + ')' );
legend = svg
.append( 'g' )
.attr( 'class', 'legend' )
.attr( 'transform', 'translate(' + ( x + radius * 2 + 20 ) + ',' + y + ')' );
u = legend.selectAll( '.leg' )
.data( pie( data ) )
.enter().append( 'g' )
.attr( 'class', 'leg' )
.attr( 'transform', function ( d, i ) { return 'translate(0,' + i * 20 + ')'; } );
u.append( 'circle' )
.attr( 'cx', 9 )
.attr( 'cy', 9 )
.attr( 'r', 5 )
.attr( 'fill', function ( d ) { return d.data.color; } );
u.append( 'text' )
.attr( 'x', 20 )
.attr( 'y', 9 )
.attr( 'dy', '.35em' )
.text( function ( d ) { return d.data.label; } );
g = chart.selectAll( '.arc' )
.data( pie( data ) )
.enter().append( 'g' )
.attr( 'class', 'arc' );
paths = g.append( 'path' )
.attr( 'd', arc )
.attr( 'fill', function ( d ) { return d.data.color; } )
.on( 'mouseover', function ( d ) {
var leg;
d3.select( this )
.interrupt()
.attr( 'd', arcOver );
leg = u
.filter( function ( dd ) { return dd.data.id === d.data.id; } );
leg.selectAll( 'text' )
.attr( 'font-weight', 'bold' );
leg.selectAll( 'circle' )
.interrupt()
.attr( 'r', 7.5 );
} )
.on( 'mouseout', function ( d ) {
var self, leg;
self = d3.select( this )
.interrupt();
if ( !self.classed( 'selected' ) ) {
self.transition()
.ease( 'bounce' )
.duration( 500 )
.attr( 'd', arc );
}
leg = u
.filter( function ( dd ) { return dd.data.id === d.data.id; } );
leg.selectAll( 'text' )
.attr( 'font-weight', null );
leg.selectAll( 'circle' )
.interrupt()
.transition()
.ease( 'bounce' )
.duration( 500 )
.attr( 'r', 5 );
} );
return {
arc: arc,
arcOver: arcOver,
svg: svg,
g: g,
paths: paths
};
};