2
我想連接我的兩個json文件以顯示我的等值線圖上的信息。我有地圖渲染,但當我將第二個json文件連接到地圖文件時,我被卡住了。這兩個文件都包含所有項目的ID,但ID不在對象之前,它在裏面,所以我不確定如何抓住它。缺少一個步驟爲d3連接數據在等值線圖
這是我目前有:
var data; // loaded asynchronously
var width = 600;
var height = 600;
var projection = d3.geo.albers()
//.center([-84.36322, 32.397649]);
.translate([-1000,-200])
.scale([6000]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#chart")
.append("svg:svg");
var counties = svg.append("svg:g")
.attr("id", "counties")
.attr("class", "Blues");
d3.json("data/myData/simpleGA.json", function(json) {
counties.selectAll("path")
.data(json.features)
.enter().append("svg:path")
.attr("class", data ? quantize : null)
.attr("d", path);
});
d3.json("data/myData/lotteryMapNum.json", function(json) {
data = json;
counties.selectAll("path")
.attr("class", quantize);
});
function quantize(d) {
return "q" + Math.min(8, ~~(data[d.id].hopeDollars * 9/12)) + "-9";
}
simpleGA.json
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "id": 0, "properties": { "NAMELSAD10": "Lanier County"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -83.04292, 30.947296 ], [ -83.05332, 30.94753 ],] ] } }
]
}
lotteryMapNum.json
[
{"COUNTY":"Lanier",
"hopeDollars":12921240,
"id":"0"}
]
新的代碼提示:
var newDict = {};
d3.json("data/myData/lotteryMapNum.json", function(data) {
data.forEach(function(d) { newDict[d.id] = +d.hopeDollars;})
data.forEach(function(d) { newDict[d.COUNTY] = +d.COUNTY;});
d3.json("data/myData/simpleGA.json", function(json) {
counties.selectAll("path")
.data(json.features)
.enter().append("svg:path")
.attr("class", function(d) { return quantize(newDict[d.id]);})
.attr("d", path)
.call(d3.helper.tooltip()
//.attr({class: function(d, i) { return d + ' ' + i + ' A'; }})
.text(function(d){ return 'value: '+newDict[d.id]+newDict[d.COUNTY]; })
)
.on('mouseover', function(d){ d3.select(this).style({fill: 'green', stroke: 'red'}); })
.on('mouseout', function(d){ d3.select(this).style({fill: '', stroke: ''}); });
});
});
var quantize = d3.scale.quantize()
//.domain([611850, 627698760])
.domain([0, 700000000])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
};
得到它的工作。謝謝。 – user1855009
你介意看看我發佈的更新代碼嗎?您的解決方案效果很好,現在我已經包含了工具提示,但我無法讓他們提取縣數據。我顯然沒有正確地稱呼他們。你能告訴我我做錯了什麼嗎? – user1855009