首先,請原諒我可憐的英語。
我正在使用網格地圖和外部csv進行項目工作。用下拉按鈕更新一張等值線地圖
網格圖不是由矩形或六邊形組成的,而是僅與將在末尾使用的任何種類的符號的質心組成。
所以我有一個Topojson文件,質心「ID」和質心「座標」。
外部CSV由多列組成,第一列具有相同的質心「ID」,另一列具有不同年份的值。
"ID","C2001","C2002","C2003","C2004","C2005","C2006","C2007","C2008","C2009","C2010","C2000"
6050,"-5.55753","-5.55914","-5.75444","-5.76307","-5.81660","-5.99361","-6.02150","-6.15979","-5.73530","-6.30509","-5.52990"
6051,"-5.55753","-5.55914","-5.75444","-5.76307","-5.81660","-5.99361","-6.02150","-6.15979","-5.73530","-6.30509","-5.52990"
這裏是我的代碼
var width = 960,
height = 600;
var options = [
{date: "2000", selected: "+d.C2000"},
{date: "2001", selected: "+d.C2001"},
{date: "2002", selected: "+d.C2002"},
{date: "2003", selected: "+d.C2003"},
{date: "2004", selected: "+d.C2004"},
{date: "2005", selected: "+d.C2005"},
{date: "2006", selected: "+d.C2006"},
{date: "2007", selected: "+d.C2007"},
{date: "2008", selected: "+d.C2008"},
{date: "2009", selected: "+d.C2009"},
{date: "2010", selected: "+d.C2010"},
];
var color = d3.scale.threshold()
.domain([-1985, -1400, -1000, -700, -300, -100, -25, -0])
.range(["#7f0000", "#b30000", "#d7301f", "#ef6548", "#fc8d59", "#fdbb84", "#fdd49e", "#fee8c8", "#fff7ec"]);
var path = d3.geo.path()
.projection(null)
.pointRadius(1.5);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
queue()
.defer(d3.json, "CO2_light.json")
.defer(d3.csv, "tdd_CO2_emissions.csv")
.await(ready);
function ready(error, centroid, CO2) {
var rateById = {};
console.log(rateById); //To
//CO2.forEach(function(d) { rateById[+d.ID] = +d.C2000; }); WORKING FINE = value in rateById
CO2.forEach(function(d) { rateById[+d.ID] = options[0].selected; }); //NOT WORKING = inside rateById "+d.C2000" instead the value
svg.selectAll("path")
.data(topojson.feature(centroid, centroid.objects.CENTROID).features)
.enter().append("path")
.attr("class", "centerGrid")
.attr("d", path)
.style("fill", function(d) { return color(rateById[+d.properties.ID]); });
d3.select(".loading").remove();
目前我離開按鈕,我的下一個問題,我下面
//CO2.forEach(function(d) { rateById[+d.ID] = +d.C2000; }); WORKING FINE = value in rateById
CO2.forEach(function(d) { rateById[+d.ID] = options[0].selected; }); //NOT WORKING = inside rateById "+d.C2000" instead the value
集中在這兩個線路如果我使用第一行我得到一個很好的網格地圖(see image),但如果我試圖從第二行options
陣列訪問特定年份的值,並且執行console.log(rateById);
我得到小號
Object
6050: "+d.C2000"
6051: "+d.C2000"
6712: "+d.C2000"
取而代之的是
Object
6050: -6.30509
6051: -6.30509
6712: -7.0441
'rateById [+ d.ID] = options [0] .selected;'只是做你要求它做的事情。 options [0] .selected的值是「+ d.C2000」。您想要查找二氧化碳文件的值,爲此,您需要正確格式化csv文件數據(在對象中,以便能夠根據年份和質心ID訪問值)。你會介意創建一個jsfiddle讓它更容易幫助你嗎? – 2014-09-27 15:54:05
它在那裏:[jsfiddle鏈接](http://jsfiddle.net/z7sLdyu2/1/)非常感謝 – 2014-09-27 16:19:49