var employees = [
{dept: 'A', age : 22},
{dept: 'B', age : 26},
{dept: 'C', age : 35},
{dept: 'D', age : 30},
{dept: 'E', age : 27}
];
var svgHeight = 400;
var svgWidth = 400;
var maxAge = 65; // You can also compute this from the data
var barSpacing = 1; // The amount of space you want to keep between the bars
var padding = {
left: 20, right: 0,
top: 20, bottom: 20
};
function animateBarsUp() {
var maxWidth = svgWidth - padding.left - padding.right;
var maxHeight = svgHeight - padding.top - padding.bottom;
// Define your conversion functions
var convert = {
x: d3.scale.ordinal(),
y: d3.scale.linear()
};
// Define your axis
var axis = {
x: d3.svg.axis().orient('bottom'),
y: d3.svg.axis().orient('left')
};
// Define the conversion function for the axis points
axis.x.scale(convert.x);
axis.y.scale(convert.y);
// Define the output range of your conversion functions
convert.y.range([maxHeight, 0]);
convert.x.rangeRoundBands([0, maxWidth]);
// Once you get your data, compute the domains
convert.x.domain(employees.map(function (d) {
return d.dept;
})
);
convert.y.domain([0, maxAge]);
// Setup the markup for your SVG
var svg = d3.select('.chart')
.attr({
width: svgWidth,
height: svgHeight
});
// The group node that will contain all the other nodes
// that render your chart
var chart = svg.append('g')
.attr({
transform: function (d, i) {
return 'translate(' + padding.left + ',' + padding.top + ')';
}
});
chart.append('g') // Container for the axis
.attr({
class: 'x axis',
transform: 'translate(0,' + maxHeight + ')'
})
.call(axis.x); // Insert an axis inside this node
chart.append('g') // Container for the axis
.attr({
class: 'y axis',
height: maxHeight
})
.call(axis.y); // Insert an axis inside this node
var bars = chart
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.