我无法弄清楚为什么我的数据没有绘制在我创建的折线图上。我正在使用以年为单位的时间尺度,并设法使图表的框架按预期呈现(X 轴值显示为年)。但是,数据没有绘制。这是我的代码(console.log 输出显示在脚本下方):
// 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 输出:
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.
从上面的控制台输出中可以看出,数据仅绘制在 X 值 439 处。有什么想法吗?域和范围输出似乎正确。