<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</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>
//Import the data
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
};
});
var range = {};
//Get the min and max of all of the quantitative variables
range.year = d3.extent(data, function(d) { return d.year; });
range.cylinders = d3.extent(data, function(d) { return d.cylinders; });
range.horsepower = d3.extent(data, function(d) { return d.horsepower; });
range.mpg = d3.extent(data, function(d) { return d.mpg; });
range.weight = d3.extent(data, function(d) { return d.weight; });
//Get the model names
range.model = d3.set(data.map(function(d){
return d.model;
})).values();
//Get the origin names
range.origin = d3.set(data.map(function(d){
return d.origin;
})).values();
//Create the SVG element
var width = 500;
var height = 400;
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height);
//Add insets for the axes
var margins = {
top: 20,
right: 20,
bottom: 50,
left: 30
};
var graphWidth = width - margins.right - margins.left;
var graphHeight = height - margins.top - margins.bottom;
var chart = svg.append('g')
.attr('transform',
'translate(' + margins.left + ',' + margins.top + ')');
//A helper function to extend the axes by a percentage
var autoextend = function(minmax) {
var r = minmax[1] - minmax[0];
var buffer = r * 0.08;
return [minmax[0] - buffer, minmax[1] + buffer];
};
//Create the axis scales
var y = d3.scale.linear()
.range([graphHeight, 0])
.domain(autoextend(range.mpg));
var x = d3.scale.linear()
.range([0, graphWidth])
.domain(autoextend(range.horsepower));
var radius = d3.scale.log()
.range([5, 10])
.domain(range.weight);
var color = d3.scale.category10();
//Create the axis renderers
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
//Draw the axes
var yAxisG = chart.append("g")
.classed("y axis", true)
.call(yAxis);
var xAxisG = chart.append("g")
.attr("transform", "translate(0, " + graphHeight + ")")
.classed("x axis", true)
.call(xAxis);
//Add axis labels
xAxisG.append("text")
.attr("class", "label")
.attr("x", graphWidth)
.attr("y", -5)
.style("text-anchor", "end")
.text("Horsepower");
yAxisG.append("text")
.attr("class", "label")
.attr("y", 6)
.attr("x", 6)
.attr("dy", ".5em")
.text("MPG");
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. |