2012-11-08 123 views
1

在前往走上錯誤的道路,任何人都可以解釋,如果這是完全有可能的:陰影多邊形與D3

我有以下代碼:

var path = d3.geo.path(); 
var svg = d3.select("#d3Map").append("svg"); 
var countries = svg.append("g").attr("id", "countries"); 
d3.json("scripts/plugins/d3-master/examples/data/world-countries.json", function (json) { 
    d3.select("svg") 
    .selectAll("path") 
    .data(json.features) 
    .enter() 
    .append("path") 
    .attr("d",d3.geo.path().projection(d3.geo.equirectangular().scale(1500))); 
}); 

Whcich果然加載和顯示的國家世界。完美的作品。

但是我想要做的是改變一些國家的顏色。

看過SVG生成後,似乎沒有該國家的ID(即使JSON文件確實具有'name'屬性)。

有沒有做類似的東西的任何可能的方式:

亮點(國家名稱,RGB)

還是我在這裏使用了錯誤的工具?

回答

2

爲什麼不

d3.select("svg") 
    .selectAll("path") 
    .data(json.features) 
.enter() 
    .append("path") 
    .attr("d",d3.geo.path().projection(d3.geo.equirectangular().scale(1500))) 
    .attr("fill", "#c00")// <- Set color like this, or pass a function 
// Or 
    .attr("id", function(feature) { 
    /* something like: return feature.properties.id; */ 
    }) 
// Or 
    .attr("class", function(feature) { 
    return feature.population > 1e6 ? 'populous' : 'rural'; 
    }); 
+0

你的最後一個例子肯定有助於極大!仔細閱讀d3文檔後,我現在可以理解,但是您的示例肯定有幫助,所以我的感謝。 – JasonMHirst