2013-10-08 61 views
1

我无法弄清为什么我的数据没有绘制在创建的折线图上。我使用的时间尺度经过了几年,并设法使图表框架按预期进行渲染(X轴值显示为年)。但是,数据不是绘图。这里是我的代码(的console.log输出显示下面的脚本):D3.js:仅在单个X轴坐标上绘制数据

// 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. 

正如你可以从上面的控制台输出看,该数据仅绘制在439的X值。任何想法为什么?域和范围输出似乎是正确的。

IMG http://i40.tinypic.com/166chg0.png

+1

你可能想'返回X(解析(年[1]));'在'.X ()''line1'的函数。 –

+0

完美!感谢您的建议。 – thefreeline

+0

我会补充说,作为一个答案和更多的解释。 –

回答

3

此刻,你的代码的x坐标线返回字符串的一年。您需要将此传递给您的比例尺,并正确设置。这也需要将年份解析为Date。总之,你的代码看起来像这样。

.x(function(d, i) { 
    return x(parse(year[i])); 
}) 

在一般说明,更D3的方式来做到这将有一年和价值观在一个数组,然后传递到.data()功能。那么你的线功能将看起来像

var line = d3.svg.line() 
      .x(function(d) { return x(d.year); }) 
      .y(function(d) { return y(d.nomGDP); }); 

和代码中添加行

graph.selectAll("path").data([data]).enter() 
    .append("path") 
    .attr("d", line); 
+0

谢谢拉尔斯。我从几个不同的例子中拉出来。具体而言,http://bl.ocks.org/benjchristensen/2579619和http://bl.ocks.org/mbostock/1166403 – thefreeline