Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<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");
//Find all the dots
var dots = chart.selectAll('.dot')
                .data(data);
//Add dots
dots.enter()
    .append("circle")
    .attr("class", "dot")
    .append("title");
//Remove dots
dots.exit().remove();
//Map data to visual attributes
dots.attr("r", function(d) { return radius(d.weight); })
    .attr("cx", function(d) { return x(d.horsepower); })
    .attr("cy", function(d) { return y(d.mpg); })
    .style("fill", function(d) { return color(d.origin); });
//Set the title text
dots.select("title")
  .text(function(d) { 
    return d.model + 
      " (" + d.origin + 
      "): " + d.horsepower + " hp, " +
      d.mpg + " mpg, " +
      d.weight + " lbs"; 
  });
//Add a legend for color/origin
var origins = chart.selectAll(".legend.origin")
  .data(color.domain())
  .enter().append("g")
  .attr("class", "legend origin")
  .attr("transform", 
        function(d, i) { return "translate(0," + i * 20 + ")"; });
origins.append("rect")
      .attr("x", graphWidth - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);
origins.append("text")
      .attr("x", graphWidth - 24)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .text(function(d) { return d; });
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
mjbrookspro
0viewers