-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy path_segments.js
293 lines (251 loc) · 8.51 KB
/
_segments.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// --------- segments.js -----------
var segments = {
effectMap: {
"none": d3.easeLinear,
"bounce": d3.easeBounce,
"linear": d3.easeLinear,
"sin": d3.easeSin,
"elastic": d3.easeElastic,
"back": d3.easeBack,
"quad": d3.easeQuad,
"circle": d3.easeCircle,
"exp": d3.easeExp
},
/**
* Creates the pie chart segments and displays them according to the desired load effect.
* @private
*/
create: function(pie) {
var pieCenter = pie.pieCenter;
var colors = pie.options.colors;
var loadEffects = pie.options.effects.load;
var segmentStroke = pie.options.misc.colors.segmentStroke;
// we insert the pie chart BEFORE the title, to ensure the title overlaps the pie
var pieChartElement = pie.svg.insert("g", "#" + pie.cssPrefix + "title")
.attr("transform", function() { return math.getPieTranslateCenter(pieCenter); })
.attr("class", pie.cssPrefix + "pieChart");
var arc = d3.arc()
.innerRadius(pie.innerRadius)
.outerRadius(pie.outerRadius)
.startAngle(0)
.endAngle(function(d) {
return (d.value / pie.totalSize) * 2 * Math.PI;
});
var g = pieChartElement.selectAll("." + pie.cssPrefix + "arc")
.data(pie.options.data.content)
.enter()
.append("g")
.attr("class", pie.cssPrefix + "arc");
// if we're not fading in the pie, just set the load speed to 0
var loadSpeed = loadEffects.speed;
if (loadEffects.effect === "none") {
loadSpeed = 0;
}
g.append("path")
.attr("id", function(d, i) { return pie.cssPrefix + "segment" + i; })
.attr("fill", function(d, i) {
var color = colors[i];
if (pie.options.misc.gradient.enabled) {
color = "url(#" + pie.cssPrefix + "grad" + i + ")";
}
return color;
})
.style("stroke", segmentStroke)
.style("stroke-width", 1)
.transition()
.ease(d3.easeCubicInOut)
.duration(loadSpeed)
.attr("data-index", function(d, i) { return i; })
.attrTween("d", function(b) {
var i = d3.interpolate({ value: 0 }, b);
return function(t) {
return pie.arc(i(t));
};
});
pie.svg.selectAll("g." + pie.cssPrefix + "arc")
.attr("transform",
function(d, i) {
var angle = 0;
if (i > 0) {
angle = segments.getSegmentAngle(i-1, pie.options.data.content, pie.totalSize);
}
return "rotate(" + angle + ")";
}
);
pie.arc = arc;
},
addGradients: function(pie) {
var grads = pie.svg.append("defs")
.selectAll("radialGradient")
.data(pie.options.data.content)
.enter().append("radialGradient")
.attr("gradientUnits", "userSpaceOnUse")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", "120%")
.attr("id", function(d, i) { return pie.cssPrefix + "grad" + i; });
grads.append("stop").attr("offset", "0%").style("stop-color", function(d, i) { return pie.options.colors[i]; });
grads.append("stop").attr("offset", pie.options.misc.gradient.percentage + "%").style("stop-color", pie.options.misc.gradient.color);
},
addSegmentEventHandlers: function(pie) {
var arc = d3.selectAll("." + pie.cssPrefix + "arc,." + pie.cssPrefix + "labelGroup-inner,." + pie.cssPrefix + "labelGroup-outer");
arc.on("click", function() {
var currentEl = d3.select(this);
var segment;
// mouseover works on both the segments AND the segment labels, hence the following
if (currentEl.attr("class") === pie.cssPrefix + "arc") {
segment = currentEl.select("path");
} else {
var index = currentEl.attr("data-index");
segment = d3.select("#" + pie.cssPrefix + "segment" + index);
}
var isExpanded = segment.attr("class") === pie.cssPrefix + "expanded";
segments.onSegmentEvent(pie, pie.options.callbacks.onClickSegment, segment, isExpanded);
if (pie.options.effects.pullOutSegmentOnClick.effect !== "none") {
if (isExpanded) {
segments.closeSegment(pie, segment.node());
} else {
segments.openSegment(pie, segment.node());
}
}
});
arc.on("mouseover", function() {
var currentEl = d3.select(this);
var segment, index;
if (currentEl.attr("class") === pie.cssPrefix + "arc") {
segment = currentEl.select("path");
} else {
index = currentEl.attr("data-index");
segment = d3.select("#" + pie.cssPrefix + "segment" + index);
}
if (pie.options.effects.highlightSegmentOnMouseover) {
index = segment.attr("data-index");
var segColor = pie.options.colors[index];
segment.style("fill", helpers.getColorShade(segColor, pie.options.effects.highlightLuminosity));
}
if (pie.options.tooltips.enabled) {
index = segment.attr("data-index");
tt.showTooltip(pie, index);
}
var isExpanded = segment.attr("class") === pie.cssPrefix + "expanded";
segments.onSegmentEvent(pie, pie.options.callbacks.onMouseoverSegment, segment, isExpanded);
});
arc.on("mousemove", function() {
tt.moveTooltip(pie);
});
arc.on("mouseout", function() {
var currentEl = d3.select(this);
var segment, index;
if (currentEl.attr("class") === pie.cssPrefix + "arc") {
segment = currentEl.select("path");
} else {
index = currentEl.attr("data-index");
segment = d3.select("#" + pie.cssPrefix + "segment" + index);
}
if (pie.options.effects.highlightSegmentOnMouseover) {
index = segment.attr("data-index");
var color = pie.options.colors[index];
if (pie.options.misc.gradient.enabled) {
color = "url(#" + pie.cssPrefix + "grad" + index + ")";
}
segment.style("fill", color);
}
if (pie.options.tooltips.enabled) {
index = segment.attr("data-index");
tt.hideTooltip(pie, index);
}
var isExpanded = segment.attr("class") === pie.cssPrefix + "expanded";
segments.onSegmentEvent(pie, pie.options.callbacks.onMouseoutSegment, segment, isExpanded);
});
},
// helper function used to call the click, mouseover, mouseout segment callback functions
onSegmentEvent: function(pie, func, segment, isExpanded) {
if (!helpers.isFunction(func)) {
return;
}
var index = parseInt(segment.attr("data-index"), 10);
func({
segment: segment.node(),
index: index,
expanded: isExpanded,
data: pie.options.data.content[index]
});
},
openSegment: function(pie, segment) {
if (pie.isOpeningSegment) {
return;
}
pie.isOpeningSegment = true;
segments.maybeCloseOpenSegment();
d3.select(segment).transition()
.ease(segments.effectMap[pie.options.effects.pullOutSegmentOnClick.effect])
.duration(pie.options.effects.pullOutSegmentOnClick.speed)
.attr("transform", function(d, i) {
var c = pie.arc.centroid(d),
x = c[0],
y = c[1],
h = Math.sqrt(x*x + y*y),
pullOutSize = parseInt(pie.options.effects.pullOutSegmentOnClick.size, 10);
return "translate(" + ((x/h) * pullOutSize) + ',' + ((y/h) * pullOutSize) + ")";
})
.on("end", function(d, i) {
pie.currentlyOpenSegment = segment;
pie.isOpeningSegment = false;
d3.select(segment).attr("class", pie.cssPrefix + "expanded");
});
},
maybeCloseOpenSegment: function() {
if (d3.selectAll("." + pie.cssPrefix + "expanded").size() > 0) {
segments.closeSegment(pie, d3.select("." + pie.cssPrefix + "expanded").node());
}
},
closeSegment: function(pie, segment) {
d3.select(segment).transition()
.duration(400)
.attr("transform", "translate(0,0)")
.on("end", function(d, i) {
d3.select(segment).attr("class", "");
pie.currentlyOpenSegment = null;
});
},
getCentroid: function(el) {
var bbox = el.getBBox();
return {
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height / 2
};
},
/**
* General helper function to return a segment's angle, in various different ways.
* @param index
* @param opts optional object for fine-tuning exactly what you want.
*/
getSegmentAngle: function(index, data, totalSize, opts) {
var options = extend({
// if true, this returns the full angle from the origin. Otherwise it returns the single segment angle
compounded: true,
// optionally returns the midpoint of the angle instead of the full angle
midpoint: false
}, opts);
var currValue = data[index].value;
var fullValue;
if (options.compounded) {
fullValue = 0;
// get all values up to and including the specified index
for (var i=0; i<=index; i++) {
fullValue += data[i].value;
}
}
if (typeof fullValue === 'undefined') {
fullValue = currValue;
}
// now convert the full value to an angle
var angle = (fullValue / totalSize) * 360;
// lastly, if we want the midpoint, factor that sucker in
if (options.midpoint) {
var currAngle = (currValue / totalSize) * 360;
angle -= (currAngle / 2);
}
return angle;
}
};