in mc/Scripts/nv.d3.js [10201:10506]
function chart(selection) {
renderWatch.reset();
selection.each(function(data) {
var availableWidth = width - margin.left - margin.right
, availableHeight = height - margin.top - margin.bottom
, radius = Math.min(availableWidth, availableHeight) / 2
, arcsRadiusOuter = []
, arcsRadiusInner = []
;
container = d3.select(this)
if (arcsRadius.length === 0) {
var outer = radius - radius / 5;
var inner = donutRatio * radius;
for (var i = 0; i < data[0].length; i++) {
arcsRadiusOuter.push(outer);
arcsRadiusInner.push(inner);
}
} else {
arcsRadiusOuter = arcsRadius.map(function (d) { return (d.outer - d.outer / 5) * radius; });
arcsRadiusInner = arcsRadius.map(function (d) { return (d.inner - d.inner / 5) * radius; });
donutRatio = d3.min(arcsRadius.map(function (d) { return (d.inner - d.inner / 5); }));
}
nv.utils.initSVG(container);
// Setup containers and skeleton of chart
var wrap = container.selectAll('.nv-wrap.nv-pie').data(data);
var wrapEnter = wrap.enter().append('g').attr('class','nvd3 nv-wrap nv-pie nv-chart-' + id);
var gEnter = wrapEnter.append('g');
var g = wrap.select('g');
var g_pie = gEnter.append('g').attr('class', 'nv-pie');
gEnter.append('g').attr('class', 'nv-pieLabels');
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
g.select('.nv-pie').attr('transform', 'translate(' + availableWidth / 2 + ',' + availableHeight / 2 + ')');
g.select('.nv-pieLabels').attr('transform', 'translate(' + availableWidth / 2 + ',' + availableHeight / 2 + ')');
//
container.on('click', function(d,i) {
dispatch.chartClick({
data: d,
index: i,
pos: d3.event,
id: id
});
});
arcs = [];
arcsOver = [];
for (var i = 0; i < data[0].length; i++) {
var arc = d3.svg.arc().outerRadius(arcsRadiusOuter[i]);
var arcOver = d3.svg.arc().outerRadius(arcsRadiusOuter[i] + 5);
if (startAngle !== false) {
arc.startAngle(startAngle);
arcOver.startAngle(startAngle);
}
if (endAngle !== false) {
arc.endAngle(endAngle);
arcOver.endAngle(endAngle);
}
if (donut) {
arc.innerRadius(arcsRadiusInner[i]);
arcOver.innerRadius(arcsRadiusInner[i]);
}
if (arc.cornerRadius && cornerRadius) {
arc.cornerRadius(cornerRadius);
arcOver.cornerRadius(cornerRadius);
}
arcs.push(arc);
arcsOver.push(arcOver);
}
// Setup the Pie chart and choose the data element
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.disabled ? 0 : getY(d) });
// padAngle added in d3 3.5
if (pie.padAngle && padAngle) {
pie.padAngle(padAngle);
}
// if title is specified and donut, put it in the middle
if (donut && title) {
g_pie.append("text").attr('class', 'nv-pie-title');
wrap.select('.nv-pie-title')
.style("text-anchor", "middle")
.text(function (d) {
return title;
})
.style("font-size", (Math.min(availableWidth, availableHeight)) * donutRatio * 2 / (title.length + 2) + "px")
.attr("dy", "0.35em") // trick to vertically center text
.attr('transform', function(d, i) {
return 'translate(0, '+ titleOffset + ')';
});
}
var slices = wrap.select('.nv-pie').selectAll('.nv-slice').data(pie);
var pieLabels = wrap.select('.nv-pieLabels').selectAll('.nv-label').data(pie);
slices.exit().remove();
pieLabels.exit().remove();
var ae = slices.enter().append('g');
ae.attr('class', 'nv-slice');
ae.on('mouseover', function(d, i) {
d3.select(this).classed('hover', true);
if (growOnHover) {
d3.select(this).select("path").transition()
.duration(70)
.attr("d", arcsOver[i]);
}
dispatch.elementMouseover({
data: d.data,
index: i,
color: d3.select(this).style("fill")
});
});
ae.on('mouseout', function(d, i) {
d3.select(this).classed('hover', false);
if (growOnHover) {
d3.select(this).select("path").transition()
.duration(50)
.attr("d", arcs[i]);
}
dispatch.elementMouseout({data: d.data, index: i});
});
ae.on('mousemove', function(d, i) {
dispatch.elementMousemove({data: d.data, index: i});
});
ae.on('click', function(d, i) {
dispatch.elementClick({
data: d.data,
index: i,
color: d3.select(this).style("fill")
});
});
ae.on('dblclick', function(d, i) {
dispatch.elementDblClick({
data: d.data,
index: i,
color: d3.select(this).style("fill")
});
});
slices.attr('fill', function(d,i) { return color(d.data, i); });
slices.attr('stroke', function(d,i) { return color(d.data, i); });
var paths = ae.append('path').each(function(d) {
this._current = d;
});
slices.select('path')
.transition()
.attr('d', function (d, i) { return arcs[i](d); })
.attrTween('d', arcTween);
if (showLabels) {
// This does the normal label
var labelsArc = [];
for (var i = 0; i < data[0].length; i++) {
labelsArc.push(arcs[i]);
if (labelsOutside) {
if (donut) {
labelsArc[i] = d3.svg.arc().outerRadius(arcs[i].outerRadius());
if (startAngle !== false) labelsArc[i].startAngle(startAngle);
if (endAngle !== false) labelsArc[i].endAngle(endAngle);
}
} else if (!donut) {
labelsArc[i].innerRadius(0);
}
}
pieLabels.enter().append("g").classed("nv-label",true).each(function(d,i) {
var group = d3.select(this);
group.attr('transform', function (d, i) {
if (labelSunbeamLayout) {
d.outerRadius = arcsRadiusOuter[i] + 10; // Set Outer Coordinate
d.innerRadius = arcsRadiusOuter[i] + 15; // Set Inner Coordinate
var rotateAngle = (d.startAngle + d.endAngle) / 2 * (180 / Math.PI);
if ((d.startAngle + d.endAngle) / 2 < Math.PI) {
rotateAngle -= 90;
} else {
rotateAngle += 90;
}
return 'translate(' + labelsArc[i].centroid(d) + ') rotate(' + rotateAngle + ')';
} else {
d.outerRadius = radius + 10; // Set Outer Coordinate
d.innerRadius = radius + 15; // Set Inner Coordinate
return 'translate(' + labelsArc[i].centroid(d) + ')'
}
});
group.append('rect')
.style('stroke', '#fff')
.style('fill', '#fff')
.attr("rx", 3)
.attr("ry", 3);
group.append('text')
.style('text-anchor', labelSunbeamLayout ? ((d.startAngle + d.endAngle) / 2 < Math.PI ? 'start' : 'end') : 'middle') //center the text on it's origin or begin/end if orthogonal aligned
.style('fill', '#000')
});
var labelLocationHash = {};
var avgHeight = 14;
var avgWidth = 140;
var createHashKey = function(coordinates) {
return Math.floor(coordinates[0]/avgWidth) * avgWidth + ',' + Math.floor(coordinates[1]/avgHeight) * avgHeight;
};
pieLabels.watchTransition(renderWatch, 'pie labels').attr('transform', function (d, i) {
if (labelSunbeamLayout) {
d.outerRadius = arcsRadiusOuter[i] + 10; // Set Outer Coordinate
d.innerRadius = arcsRadiusOuter[i] + 15; // Set Inner Coordinate
var rotateAngle = (d.startAngle + d.endAngle) / 2 * (180 / Math.PI);
if ((d.startAngle + d.endAngle) / 2 < Math.PI) {
rotateAngle -= 90;
} else {
rotateAngle += 90;
}
return 'translate(' + labelsArc[i].centroid(d) + ') rotate(' + rotateAngle + ')';
} else {
d.outerRadius = radius + 10; // Set Outer Coordinate
d.innerRadius = radius + 15; // Set Inner Coordinate
/*
Overlapping pie labels are not good. What this attempts to do is, prevent overlapping.
Each label location is hashed, and if a hash collision occurs, we assume an overlap.
Adjust the label's y-position to remove the overlap.
*/
var center = labelsArc[i].centroid(d);
if (d.value) {
var hashKey = createHashKey(center);
if (labelLocationHash[hashKey]) {
center[1] -= avgHeight;
}
labelLocationHash[createHashKey(center)] = true;
}
return 'translate(' + center + ')'
}
});
pieLabels.select(".nv-label text")
.style('text-anchor', function(d,i) {
//center the text on it's origin or begin/end if orthogonal aligned
return labelSunbeamLayout ? ((d.startAngle + d.endAngle) / 2 < Math.PI ? 'start' : 'end') : 'middle';
})
.text(function(d, i) {
var percent = (d.endAngle - d.startAngle) / (2 * Math.PI);
var label = '';
if (!d.value || percent < labelThreshold) return '';
if(typeof labelType === 'function') {
label = labelType(d, i, {
'key': getX(d.data),
'value': getY(d.data),
'percent': valueFormat(percent)
});
} else {
switch (labelType) {
case 'key':
label = getX(d.data);
break;
case 'value':
label = valueFormat(getY(d.data));
break;
case 'percent':
label = d3.format('%')(percent);
break;
}
}
return label;
})
;
}
// Computes the angle of an arc, converting from radians to degrees.
function angle(d) {
var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
return a > 90 ? a - 180 : a;
}
function arcTween(a, idx) {
a.endAngle = isNaN(a.endAngle) ? 0 : a.endAngle;
a.startAngle = isNaN(a.startAngle) ? 0 : a.startAngle;
if (!donut) a.innerRadius = 0;
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function (t) {
return arcs[idx](i(t));
};
}
});
renderWatch.renderEnd('pie immediate');
return chart;
}