0
votes

Hey guys still trying to get used to D3 and might sound basic. I used the wiki and some other posts but I think some of my stuff might be outdated. Im trying to take the year and MPG from my csv file and make a scatter plot with it with x as year. I also dont want to shortcut and modify the csv file bec i want to learn how to do it no matter how many attributes there are. I think part of the problem im having is this part of my code:

d3.csv("data.csv", function(data) {
  dataset = data.map(function(d) { return [ +d["Year"], +d["MPG"] ]; });
});

  x.domain(d3.extent(data, function(d) { return d.Year; })).nice();
  y.domain(d3.extent(data, function(d) { return d.MPG; })).nice();

But I will let you guys judge that. This is what I have so far:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.dot {
  stroke: #000;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var color = d3.scale.category10();

var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("data.csv", function(data) {
  dataset = data.map(function(d) { return [ +d["Year"], +d["MPG"] ]; });
});

  x.domain(d3.extent(data, function(d) { return d.Year; })).nice();
  y.domain(d3.extent(data, function(d) { return d.MPG; })).nice();

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .append("text")
      .attr("class", "label")
      .attr("x", width)
      .attr("y", -6)
      .style("text-anchor", "end")
      .text("Year");

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("class", "label")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("MPG")

  svg.selectAll(".dot")
      .data(data)
    .enter().append("circle")
      .attr("class", "dot")
      .attr("r", 3.5)
      .attr("cx", function(d) { return x(d.Year); })
      .attr("cy", function(d) { return y(d.MPG); })
      .style("fill", function(d) { return color(d.species); });

  var legend = svg.selectAll(".legend")
      .data(color.domain())
    .enter().append("g")
      .attr("class", "legend")
      .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("rect")
      .attr("x", width - 18)
      .attr("width", 18)
      .attr("height", 18)
      .style("fill", color);

  legend.append("text")
      .attr("x", width - 24)
      .attr("y", 9)
      .attr("dy", ".35em")
      .style("text-anchor", "end")
      .text(function(d) { return d; });

});

</script>

My csv file loooks like this:

Car, Manufacturer,MPG,Cylinders,Displacement,Horsepower,Weight,Acceleration,ModelYear,Origin chevelle malibu,chevrolet,18,8,307,130,3504,12,70,American skylark 320,buick,15,8,350,165,3693,11.5,70,American satellite,plymouth,18,8,318,150,3436,11,70,American rebel sst,amc,16,8,304,150,3433,12,70,American torino,ford,17,8,302,140,3449,10.5,70,American

1
d3.csv is asynchronous. You need to put all the code that uses dataset inside the callback.Lars Kotthoff

1 Answers

0
votes

Had to add the proper function to get it to read the csv file from the server and input the variables I wanted it to get