首先,请原谅我可怜的英语。
我正在开发一个带有网格图和外部csv的项目。
网格图不是由矩形或六边形组成,而是由最终将使用的任何符号的质心组成。
所以我有一个Topojson文件,质心为“ID”,质心为“Coordinates”。
外部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
答案 0 :(得分:1)
修正了这里:http://jsfiddle.net/z7sLdyu2/2/
在您的代码中进行2次更改:
首先,您的选项选择不应包含要执行的javascript代码(+d.
),而只包含年份值:
var options = [
{date: "2000", selected: "C2000"},
{date: "2001", selected: "C2001"},
{date: "2002", selected: "C2002"},
{date: "2003", selected: "C2003"},
{date: "2004", selected: "C2004"},
{date: "2005", selected: "C2005"},
{date: "2006", selected: "C2006"},
{date: "2007", selected: "C2007"},
{date: "2008", selected: "C2008"},
{date: "2009", selected: "C2009"},
{date: "2010", selected: "C2010"}
];
然后在循环中,通过访问所选年份的数据rateById
属性来分配d
值,如下所示:
rateById[+d.ID] = +d[options[0].selected];
P.S。:我必须删除jsfiddle中的部分代码,因为在你的jsfiddle版本中没有找到质心json,导致与此问题无关的错误。