2014-08-27 97 views
0

我目前正在D3中製作散點圖矩陣。我的數據點的結構是這樣的:將D3參數傳入D3

 function plot(p) { 
     var cell = d3.select(this); 

     x.domain(domainByTrait[p.x]); 
     y.domain(domainByTrait[p.y]); 

     // Data points 
     cell.selectAll("circle") 
     .data(data) 
     .enter().append("circle") 
     .attr("cx", function (d) { return x(d[p.x]); }) 
     .attr("cy", function (d) { return y(d[p.y]); }) 
     .attr("r", 3) 
     .style("fill", function (d) { return color(d.species); }); 
    } 

我也有一個JavaScript函數:

 function create(columnsplit) { 

     cell = svg.selectAll(".cell") 
     .data(cross(traits, traits)) 
     .enter().append("g") 
     .attr("class", "cell") 
     .attr("transform", function (d) { return "translate(" + (n - d.i - 1) * size + "," + d.j * size + ")"; }) 
     .each(plot); 

     cell.call(brush); 

    } 

其中情節分配。但是,我想用我創建函數中的columnsplit參數替換硬編碼的d.species(除非d.species不像我原先想象的那樣工作)。我到底該怎麼做?

下面是我正在創建的數據d的特點,從我能理解:

domainByTrait = {}, 
     traits = d3.keys(data[0]).filter(function (d) { return d !== columnsplit; }), 
     n = traits.length; 

traits.forEach(function (trait) { 
       domainByTrait[trait] = d3.extent(data, function (d) { return d[trait]; }); 
      }); 

我用這作爲我的模板:http://bl.ocks.org/mbostock/4063663

感謝

+0

'我想用我在創建函數中使用的columnsplit參數替換硬編碼的d.spec'你是什麼意思?爲什麼你不能這樣做? – McGarnagle 2014-08-27 23:19:49

+0

我需要將它設置爲d。「無論列在columnplit中定義」,但因爲「。」使它成爲一個字面屬性,我不能只是去d.columnsplit,因爲那麼它會查看當前基準並尋找名爲columnsplit的屬性。 – 2014-08-28 15:35:29

回答

1

如果據我所知,你想訪問名爲「columnsplit」值的屬性。要做到這一點,你只需要使用索引符號來訪問屬性:

d[columnsplit] 

換句話說,該d.species語法等效於d["species"]reference)。

+0

這很好用。謝謝! – 2014-08-28 15:54:21