Updated Bar chart to use variable for Label/Data Element

Created Pie Chart
master-patched
Nathanael Anderson 12 years ago
parent 22225147ab
commit 6af91f885c

@ -41,6 +41,10 @@ text {
{
label: "Five",
y: 4
},
{
label: "Six",
y: 3
}
];
@ -50,11 +54,11 @@ nv.addGraph({
var width = nv.utils.windowSize().width - 40,
height = nv.utils.windowSize().height - 40;
var chart = nv.models.bar()
.width(width)
.height(height);
.height(height)
.labelField('label')
.dataField('y');
d3.select("#test1")
.attr('width', width)

@ -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>

@ -254,27 +254,28 @@ nv.models.bar = function() {
var margin = {top: 20, right: 10, bottom: 20, left: 60},
width = 960,
height = 500,
animate = 500;
animate = 500,
label ='label',
field ='y';
var x = d3.scale.ordinal(),
y = d3.scale.linear(),
xAxis = d3.svg.axis().scale(x).orient('bottom').ticks(5),
xAxis = d3.svg.axis().scale(x).orient('bottom'),
yAxis = d3.svg.axis().scale(y).orient('left');
function chart(selection) {
selection.each(function(data) {
//x .domain(data.map(function(d,i) { return d.label }))
x .domain(["One", "Two", "Three", "Four", "Five"])
x .domain(data.map(function(d,i) { return d[label] }))
.rangeRoundBands([0, width - margin.left - margin.right], .1);
y .domain([0, d3.max(data, function(d) { return d.y; })])
y .domain([0, d3.max(data, function(d) { return d[field]; })])
.range([height - margin.top - margin.bottom, 0]);
xAxis.ticks( width / 100 );
yAxis.ticks( height / 36 ).tickSize(-(width - margin.right - margin.left), 0);
yAxis.tickSize(-(width - margin.right - margin.left), 0);
// yAxis.tickSize(-(width - margin.right - margin.left), 0);
var wrap = d3.select(this).selectAll('g.wrap').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'wrap').append('g');
@ -308,20 +309,20 @@ nv.models.bar = function() {
bars
.attr('transform', function(d,i) { return 'translate(' + x(d.label) + ',0)' })
.attr('transform', function(d,i) { return 'translate(' + x(d[label]) + ',0)' })
bars.selectAll('rect')
.order()
.attr('width', x.rangeBand )
.transition()
.duration(animate)
.attr('x', 0 )
.attr('y', function(d) { return y(d.y) })
.attr('height', function(d) { return y.range()[0] - y(d.y) });
.attr('y', function(d) { return y(d[field]) })
.attr('height', function(d) { return y.range()[0] - y(d[field]) });
bars.selectAll('text')
.attr('x', 0 )
.attr('y', function(d) { return y(d.y) })
.attr('y', function(d) { return y(d[field]) })
.attr('dx', x.rangeBand() / 2)
.text(function(d) { return d.y });
.text(function(d) { return d[field] });
g.select('.x.axis')
@ -365,6 +366,18 @@ nv.models.bar = function() {
return chart;
};
chart.labelField = function(_) {
if (!arguments.length) return (label);
label = _;
return chart;
};
chart.dataField = function(_) {
if (!arguments.length) return (field);
field = _;
return chart;
};
chart.xaxis = {};
// Expose the x-axis' tickFormat method.
d3.rebind(chart.xaxis, xAxis, 'tickFormat');
@ -1598,6 +1611,168 @@ nv.models.lineWithLegend = function() {
return chart;
}
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;
}
nv.models.scatter = function() {
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 960,

@ -154,7 +154,21 @@ text {
fill: rgba(0,0,0,1);
}
/***********
* Pie Chart
*/
.pie .hover path {
fill: lightblue;
}
.pie text {
fill: rgba(0,0,0,0);
}
.pie .hover text {
fill: black;
}
/**********
* Lines

@ -3,27 +3,28 @@ nv.models.bar = function() {
var margin = {top: 20, right: 10, bottom: 20, left: 60},
width = 960,
height = 500,
animate = 500;
animate = 500,
label ='label',
field ='y';
var x = d3.scale.ordinal(),
y = d3.scale.linear(),
xAxis = d3.svg.axis().scale(x).orient('bottom').ticks(5),
xAxis = d3.svg.axis().scale(x).orient('bottom'),
yAxis = d3.svg.axis().scale(y).orient('left');
function chart(selection) {
selection.each(function(data) {
//x .domain(data.map(function(d,i) { return d.label }))
x .domain(["One", "Two", "Three", "Four", "Five"])
x .domain(data.map(function(d,i) { return d[label] }))
.rangeRoundBands([0, width - margin.left - margin.right], .1);
y .domain([0, d3.max(data, function(d) { return d.y; })])
y .domain([0, d3.max(data, function(d) { return d[field]; })])
.range([height - margin.top - margin.bottom, 0]);
xAxis.ticks( width / 100 );
yAxis.ticks( height / 36 ).tickSize(-(width - margin.right - margin.left), 0);
yAxis.tickSize(-(width - margin.right - margin.left), 0);
// yAxis.tickSize(-(width - margin.right - margin.left), 0);
var wrap = d3.select(this).selectAll('g.wrap').data([data]);
var gEnter = wrap.enter().append('g').attr('class', 'wrap').append('g');
@ -57,20 +58,20 @@ nv.models.bar = function() {
bars
.attr('transform', function(d,i) { return 'translate(' + x(d.label) + ',0)' })
.attr('transform', function(d,i) { return 'translate(' + x(d[label]) + ',0)' })
bars.selectAll('rect')
.order()
.attr('width', x.rangeBand )
.transition()
.duration(animate)
.attr('x', 0 )
.attr('y', function(d) { return y(d.y) })
.attr('height', function(d) { return y.range()[0] - y(d.y) });
.attr('y', function(d) { return y(d[field]) })
.attr('height', function(d) { return y.range()[0] - y(d[field]) });
bars.selectAll('text')
.attr('x', 0 )
.attr('y', function(d) { return y(d.y) })
.attr('y', function(d) { return y(d[field]) })
.attr('dx', x.rangeBand() / 2)
.text(function(d) { return d.y });
.text(function(d) { return d[field] });
g.select('.x.axis')
@ -114,6 +115,18 @@ nv.models.bar = function() {
return chart;
};
chart.labelField = function(_) {
if (!arguments.length) return (label);
label = _;
return chart;
};
chart.dataField = function(_) {
if (!arguments.length) return (field);
field = _;
return chart;
};
chart.xaxis = {};
// Expose the x-axis' tickFormat method.
d3.rebind(chart.xaxis, xAxis, 'tickFormat');

@ -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…
Cancel
Save