<html>
<head>
<meta charset="utf-8">
<title>Cars Barchart</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script type="text/csv" id="cars">
Model,Origin,Year,Cylinders,Horsepower,MPG,Weight
amc ambassador dpl,US,70,8,190,15,3850
chevrolet vega (sw),US,71,4,72,22,2408
chevrolet vega,US,72,4,90,20,2408
ford pinto runabout,US,72,4,86,21,2226
mazda rx2 coupe,Japan,72,3,97,19,2330
amc matador (sw),US,72,8,150,15,3892
ford pinto (sw),US,72,4,86,22,2395
dodge coronet custom,US,73,8,150,15,3777
toyota carina,Japan,73,4,88,20,2279
chevrolet vega,US,73,4,72,21,2401
datsun 610,Japan,73,4,94,22,2379
ford pinto,US,73,4,85,19,2310
chevrolet chevelle malibu classic,US,74,6,100,16,3781
buick century,US,75,6,110,17,3907
chevroelt chevelle malibu,US,75,6,105,16,3897
plymouth fury,US,75,6,95,18,3785
amc matador,US,76,8,120,15.5,3962
mercedes-benz 280s,Europe,76,6,120,16.5,3820
chevrolet caprice classic,US,77,8,145,17.5,3880
chevrolet caprice classic,US,79,8,130,17,3840
mercury grand marquis,US,79,8,138,16.5,3955
dodge st. regis,US,79,8,135,18.2,3830
vw rabbit,Europe,80,4,76,41.5,2144
mazda glc,Japan,80,4,65,46.6,2110
vw pickup,Europe,82,4,52,44,2130
</script>
</body>
</html>
var data = d3.select('#cars').html().trim();
data = d3.csv.parse(data, function (d) {
return {
model: d.Model,
origin: d.Origin,
year: +d.Year,
cylinders: +d.Cylinders,
horsepower: +d.Horsepower,
mpg: +d.MPG,
weight: +d.Weight
};
});
// Create the SVG element
var width = 500;
var height = 400;
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height);
// Calculate the max weight and car model names
var max_weight = d3.max(data, function(d) {
return d.weight;
});
var model_names = data.map(function(d){
return d.model;
});
// Define the margins
var margins = {
top: 20,
right: 20,
bottom: 50,
left: 110
};
var graphWidth = width - margins.right - margins.left;
var graphHeight = height - margins.top - margins.bottom;
// Make a group for drawing the chart
var chart = svg.append('g')
.attr('transform', 'translate(' + margins.left + ',' + margins.top + ')');
// Create the scales
var x = d3.scale.linear()
.range([0, graphWidth])
.domain([0, max_weight]);
var y = d3.scale.ordinal()
.domain(model_names)
.rangeRoundBands([ 0, graphHeight], 0.1);
// Draw the axes
var xAxis = d3.svg.axis()
.scale(x)
.orient("top");
chart.append("g")
.classed("x axis", true)
.call(xAxis);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
chart.append("g")
.classed("y axis", true)
.call(yAxis);
// Draw the bars
var bars = chart.selectAll('rect.bar')
.data(data);
bars.enter()
.append('rect')
.attr('class', 'bar');
bars.exit()
.remove();
bars.attr('x', 0)
.attr('y', function(d) {
return y(d.model);
})
.attr('height', y.rangeBand())
.attr('width', function(d) {
return x(d.weight);
});
var cylinders = data.map(function(d) {
return d.cylinders;
});
var color = d3.scale.category10()
.domain(cylinders);
bars.attr('fill', function(d) {
return color(d.cylinders);
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |