0
我已經使用D3,使地圖在美國和填充顏色D3選擇<g>元素,並修改填寫
var map = d3.select("#map").append("svg")
.attr("width", svgWidth)
.attr("height", svgHeight);
d3.json("us.json", function (error, us) {
map.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.style("stroke", function (d) { return "#000000"; })
.style("fill", function (d) { return gradient(Math.random()); })
};
現在,我想改變每個國家的顏色,但不是取出地圖然後重新添加它,我想轉換顏色。
我曾嘗試:
d3.selectAll("#map").selectAll("g").selectAll("path")
但後來通過這個數組沒有幫助的元素試圖循環。
我在做什麼錯?
編輯: 我使用的嘗試和改變每個國家的顏色(每個路徑變量)的代碼是...
d3.select(this).transition().style("fill", gradient(Math.random()));
我不相信這個問題必須做上面的代碼 - 這是我試圖用來選擇給我帶來麻煩的路徑/狀態的代碼。
我也曾嘗試
d3.selectAll("path").attr("fill", function (d) { ... });
但是,這也沒有做任何事情。 :(
你能張貼您使用的是改變顏色嗎? –
只是增加了一些東西代碼... 希望這是你想要的! – Supervisor
如果你已經使用'。 style()''在你需要再次使用它之前,例如'd3.selectAll(「path」)。style(「fill」,function(d){...});'。 –