2013-10-08 60 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