I'm having trouble figuring out why my data is not plotting on the line chart I've created. I am using a time scale passing in years and have managed to get the framework of the chart to render as anticipated (with X axis values appearing as years). However, the data is not plotting. Here is my code (console.log output is shown below the script):
// define dimensions of graph
var m = [40, 120, 40, 120]; // margins
var w = 1000 - m[1] - m[3]; // width
var h = 400 - m[0] - m[2]; // height
d3.csv("national-debt-2013.csv", function(data) {
var year = data.map(function(d){return d.year;}).reverse();
var nomGDP = data.map(function(d){return +d.nominalGDPMillions;}).reverse();
console.log("year", year);
console.log("nomGDP", nomGDP);
// create axes
var parse = d3.time.format("%Y").parse;
var x = d3.time.scale().domain([parse(year[0]),parse(year[year.length - 1])]).range([0, w]),
xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true),
y1 = d3.scale.linear().domain([0, d3.max(nomGDP)]).range([h, 0]),
yAxisLeft = d3.svg.axis().scale(y1).ticks(4).orient("left"),
console.log("x domain", x.domain());
console.log("x range", x.range());
console.log("y1 domain", y1.domain());
console.log("y range", y1.range());
// create a line function that can convert data[] into x and y points
var line1 = d3.svg.line()
// assign the X function to plot our line as we wish
.x(function(d,i) {
// verbose logging to show what's actually being done
console.log('Plotting X1 value for data point: ' + d + ' using index: ' + year[i] + ' to be at ( x(i) ) : ' + x(i) + ' using our xScale.');
// return the X coordinate where we want to plot this datapoint
return year[i];
})
.y(function(d,i) {
// verbose logging to show what's actually being done
console.log('Plotting Y1 value for data point: ' + d + ' to be at ( y1(d) ): ' + y1(d) + " using our y1Scale.");
// return the Y coordinate where we want to plot this datapoint
return y1(d);
})
// Add an SVG element with the desired dimensions and margin.
var graph = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
// Add the x-axis.
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// Add the y-axis to the left
graph.append("svg:g")
.attr("class", "y axis axisLeft")
.attr("transform", "translate(-15,0)")
.call(yAxisLeft);
// add lines
// do this AFTER the axes above so that the line is above the tick-lines
graph.append("svg:path")
.attr("d", function(d) { return line1(nomGDP); })
.attr("class", "data1");
})
console.log output:
year
["1911", "1912", "1913", "1914", "1915", "1916", "1917", "1918", "1919", "1920", "1921", "1922"...]
nomGDP
[34675, 37745, 39517, 36831, 39048, 50117, 60278, 76567, 79090, 89246, 74314, 74140, 86238...]
x domain
[Sun Jan 01 1911 00:00:00 GMT-0500 (EST), Tue Jan 01 2013 00:00:00 GMT-0500 (EST)]
x range [0, 760]
y1 domain [0, 16244600]
y range [320, 0]
Plotting X1 value for data point: 34675 using index: 1911 to be at ( x(i) ) : 439.60279328609266 using our xScale. bloch-new.html:150
Plotting Y1 value for data point: 34675 to be at ( y1(d) ): 319.3169422454231 using our y1Scale. bloch-new.html:157
Plotting X1 value for data point: 37745 using index: 1912 to be at ( x(i) ) : 439.6027932863288 using our xScale. bloch-new.html:150
Plotting Y1 value for data point: 37745 to be at ( y1(d) ): 319.25646676434013 using our y1Scale. bloch-new.html:157
...
Plotting X1 value for data point: 15533800 using index: 2011 to be at ( x(i) ) : 439.60279330970303 using our xScale. bloch-new.html:150
Plotting Y1 value for data point: 15533800 to be at ( y1(d) ): 14.001945261810022 using our y1Scale. bloch-new.html:157
Plotting X1 value for data point: 16244600 using index: 2012 to be at ( x(i) ) : 439.60279330993916 using our xScale. bloch-new.html:150
Plotting Y1 value for data point: 16244600 to be at ( y1(d) ): 0 using our y1Scale.
As you can see from the console output above, the data is only plotting at the X-value of 439. Any thoughts why? The domain and range output seem correct.
At the moment, your code is returning the string for the year as the x coordinate for the line. You need to pass this to your scale, which you set up correctly. This also requires the year to be parsed to a Date
. So altogether, your code would look like this.
.x(function(d, i) {
return x(parse(year[i]));
})
On a general note, the more D3 way to do this would be to have year and values in a single array and then pass that to the .data()
function. Then your line function would look something like
var line = d3.svg.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.nomGDP); });
and the code to add the line
graph.selectAll("path").data([data]).enter()
.append("path")
.attr("d", line);