2014-12-31 46 views
0

我是d3js的新手,在這裏缺少一些基本的東西。在D3子彈圖中使用來自JSON數據的顏色

以我JSON數據,我添加了一個顏色元件,barColor(到第二組值):

[ {"title":"AMER Revenue","subtitle":"703 US$, in thousands","ranges":[840,945,1200],"measures":[703],"markers":[1050]}, {"title":"AMER COGS","subtitle":"587 US$, in thousands","ranges":[1200,832.5,740],"measures":[587],"markers":[925],"barColor":["red"]} ] 

的JSON barColor數據被正確地讀爲我可以在各種使用它的背景下,只是不在我想要的地方。

這裏是我有(完整的註釋掉其他成功的測試)。

/* Select the bars and use the color from the json data */ 
    d3.selectAll(".bullet .measure.s0") 
     //.style("fill", "red"); //This works. All .measure.s0 bars set to red. 
     .style("fill", function(d) { return d.barColor; }); //Why doesn't this work? 

具體來說,它是d3.selectAll(」子彈.measure.s0 「)風格(」 補「,function(d){return d.barColor;});這不符合我的期望。也就是說,我期望d.barColor將measure.s0的填充更改爲紅色。

我花了不少時間閱讀和搜索,但似乎卡住了。

在此先感謝您的任何指導。

回答

0

在這種情況下,您不想使用.style。由於barColor已在JSON數據中定義,因此請使用.attr

爲了解決這個問題,嘗試改變這一點:

.style("fill", function(d) { return d.barColor; });

要這樣:

.attr("style", function(d) { return d.barColor; });

我還是有點新的D3自己,所以你可能需要調整的是一些,但希望它會把你放在正確的軌道上。

+0

@ user2679918這個工作適合你嗎?如果沒有,請告訴我們,我們可以解決一些問題。 – BDD

相關問題