Updated Bar chart to use variable for Label/Data Element
Created Pie Chartmaster-patched
parent
22225147ab
commit
6af91f885c
@ -0,0 +1,89 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8">
|
||||
<link href="../src/d3.css" rel="stylesheet" type="text/css">
|
||||
<style>
|
||||
|
||||
body {
|
||||
overflow-y:scroll;
|
||||
}
|
||||
|
||||
text {
|
||||
font: 12px sans-serif;
|
||||
}
|
||||
|
||||
</style>
|
||||
<body>
|
||||
|
||||
<svg id="test1"></svg>
|
||||
|
||||
<script src="../lib/d3.v2.js"></script>
|
||||
<script src="../nv.d3.js"></script>
|
||||
<script src="../src/models/pie.js"></script>
|
||||
<script>
|
||||
|
||||
var testdata = [
|
||||
{
|
||||
label: "One",
|
||||
y: 5
|
||||
},
|
||||
{
|
||||
label: "Two",
|
||||
y: 2
|
||||
},
|
||||
{
|
||||
label: "Three",
|
||||
y: 9
|
||||
},
|
||||
{
|
||||
label: "Four",
|
||||
y: 7
|
||||
},
|
||||
{
|
||||
label: "Five",
|
||||
y: 4
|
||||
},
|
||||
{
|
||||
label: "Six",
|
||||
y: 3
|
||||
}
|
||||
];
|
||||
|
||||
//Format A
|
||||
nv.addGraph({
|
||||
generate: function() {
|
||||
var width = nv.utils.windowSize().width - 40,
|
||||
height = nv.utils.windowSize().height - 40;
|
||||
|
||||
|
||||
|
||||
var chart = nv.models.pie()
|
||||
.width(width)
|
||||
.height(height);
|
||||
|
||||
d3.select("#test1")
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.datum(testdata)
|
||||
.call(chart);
|
||||
|
||||
return chart;
|
||||
},
|
||||
callback: function(graph) {
|
||||
window.onresize = function() {
|
||||
var width = nv.utils.windowSize().width - 40,
|
||||
height = nv.utils.windowSize().height - 40;
|
||||
|
||||
d3.select("#test1")
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.call(
|
||||
graph
|
||||
.width(width)
|
||||
.height(height)
|
||||
)
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
</script>
|
@ -0,0 +1,162 @@
|
||||
|
||||
nv.models.pie = function() {
|
||||
var margin = {top: 20, right: 10, bottom: 20, left: 60},
|
||||
width = 500,
|
||||
height = 500,
|
||||
animate = 2000,
|
||||
radius = Math.min(width, height) / 2,
|
||||
label ='label',
|
||||
field ='y',
|
||||
color = d3.scale.category20(),
|
||||
labelColor = 'purple';
|
||||
|
||||
|
||||
|
||||
function chart(selection) {
|
||||
selection.each(function(data) {
|
||||
|
||||
// Create the Wrapper Element
|
||||
/* var wrap = d3.select(this)
|
||||
.data([data])
|
||||
.attr('width', width)
|
||||
.attr('height', height)
|
||||
.append("svg:g")
|
||||
.attr("transform", "translate(" + radius + "," + radius + ")"); */
|
||||
|
||||
var wrap = d3.select(this).selectAll('g.wrap').data([data]);
|
||||
var gEnter = wrap.enter().append('g').attr('class', 'wrap');
|
||||
|
||||
wrap.attr('width', width)
|
||||
.attr('height', height)
|
||||
.attr("transform", "translate(" + radius + "," + radius + ")");
|
||||
|
||||
gEnter.append('g').attr('class', 'pie');
|
||||
|
||||
var arc = d3.svg.arc()
|
||||
.outerRadius((radius-(radius / 3)));
|
||||
|
||||
// Setup the Pie chart and choose the data element
|
||||
var pie = d3.layout.pie()
|
||||
.value(function (d) { return d[field]; });
|
||||
|
||||
var slices = wrap.select('.pie').selectAll(".slice")
|
||||
.data(pie);
|
||||
|
||||
slices.exit().remove();
|
||||
|
||||
|
||||
var ae = slices.enter().append("svg:g")
|
||||
.attr("class", "slice")
|
||||
.on('mouseover', function(d,i){ d3.select(this).classed('hover', true) })
|
||||
.on('mouseout', function(d,i){ d3.select(this).classed('hover', false) });
|
||||
|
||||
|
||||
var paths = ae.append("svg:path")
|
||||
.attr("fill", function(d, i) { return color(i); })
|
||||
.attr('d', arc);
|
||||
|
||||
// This does the normal label
|
||||
ae.append("text")
|
||||
.attr("transform", function(d) {
|
||||
d.outerRadius = radius + 10; // Set Outer Coordinate
|
||||
d.innerRadius = radius + 15; // Set Inner Coordinate
|
||||
return "translate(" + arc.centroid(d) + ")";
|
||||
})
|
||||
.attr("text-anchor", "middle") //center the text on it's origin
|
||||
.style("font", "bold 12px Arial")
|
||||
.text(function(d, i) { return d.data[label] + ': ' + d.data[field]; });
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Animation
|
||||
paths.transition()
|
||||
.ease("bounce")
|
||||
.duration(animate)
|
||||
.attrTween("d", tweenPie);
|
||||
|
||||
|
||||
|
||||
/* var slices = wrap.select('.slice').selectAll('.slice')
|
||||
.data(function(d) { return d }); */
|
||||
/*
|
||||
var barsEnter = arcs.enter().append('g')
|
||||
.attr('class', 'slice')
|
||||
.on('mouseover', function(d,i){ d3.select(this).classed('hover', true) })
|
||||
.on('mouseout', function(d,i){ d3.select(this).classed('hover', false) });
|
||||
|
||||
barsEnter.append('rect')
|
||||
.attr('y', function(d) { return y(0) });
|
||||
barsEnter.append('text')
|
||||
.attr('text-anchor', 'middle')
|
||||
.attr('dy', '-4px');
|
||||
*/
|
||||
|
||||
function tweenPie(b) {
|
||||
b.innerRadius = 0;
|
||||
var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
|
||||
return function(t) {
|
||||
return arc(i(t));
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
chart.margin = function(_) {
|
||||
if (!arguments.length) return margin;
|
||||
margin = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.width = function(_) {
|
||||
if (!arguments.length) return width;
|
||||
if (margin.left + margin.right + 20 > _) {
|
||||
width = margin.left + margin.right + 20; // Min width
|
||||
} else {
|
||||
width = _;
|
||||
}
|
||||
radius = Math.min(width, height) / 2;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.height = function(_) {
|
||||
if (!arguments.length) return height;
|
||||
if (margin.top + margin.bottom + 20 > _) {
|
||||
height = margin.top + margin.bottom + 20; // Min height
|
||||
} else {
|
||||
height = _;
|
||||
}
|
||||
radius = Math.min(width, height) / 2;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.animate = function(_) {
|
||||
if (!arguments.length) return animate;
|
||||
animate = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.labelField = function(_) {
|
||||
if (!arguments.length) return (label);
|
||||
label = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.dataField = function(_) {
|
||||
if (!arguments.length) return (field);
|
||||
field = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
|
||||
return chart;
|
||||
}
|
Loading…
Reference in New Issue