【发布时间】:2017-11-22 00:02:20
【问题描述】:
我正在构建一个 (d3 v4) 制图可视化,它允许用户在许多数据集(json 文件)和两个不同的区域(一个国家和较小的行政单位并入其首都)。实际上,通过按钮和 jquery 在初始国家/地区级别从一个数据集切换到另一个数据集效果很好。
问题:切换到关于首都城市的地图/数据集时说服力稍差,因为投影最初是为整个国家/地区设置的,因此用户必须多次缩放才能可视化正确的首都地图。我想在调用投影时更改 .scale 和 .center 的值,但经过几次尝试我还没有找到方法。
由于我只有两个不同的区域要显示,我的直觉是设置 scale 和 center 的第一个值并将它们更改为其他值(我知道 .scale 和 .center 的值我想在这两种情况下使用) 当用户通过功能切换到首都的地图时。有没有可能轻松切换这些值?你对解决这个问题有什么建议吗?
当用户单击按钮切换到另一个数据集时,当我将 json 文件路径加载到函数中时,我试图以相同的方式加载 scale 的值,但我可能做错了。好像关于投影的部分代码不能放在函数中?
感谢您的帮助!
我的代码的一小部分:
var width = 1100, height = 770;
var projection = d3.geoConicConformal()
.scale(19000) // value I would like to which when the region changes
.center([4.45, 50.53]) // value I would like to which when the region changes
.translate([width/2,height/2]);
var svg = d3.select( "#mapcontainer" )
.append( "svg" )
.attr("width", width)
.attr("height", height)
.style("border", "solid 1px black");
var path = d3.geoPath()
.projection(projection);
var color, jsonfile, legendtext;
function load (jsonfile, legendtext, color) {
d3.selectAll(".currentmap").remove() ;
d3.json(jsonfile, function(error, belgique) {
g.selectAll("path")
.data(belgique.features)
.enter()
.append("path")
.attr("d", path)
.style("stroke", "#fff")
.attr( "class", "currentmap")
.style("fill", function(d) {
var value = d.properties.DATA;
if (value) {return color(value);}
else {return "rgb(250,110,110)"}
});
})
};
//one of the following function for each map
function BGQprovinces() {
jsonfile = "ATLAS/NewGeoJson/bgq-data1-provinces.json";
legendText [= …];
color = d3.scaleOrdinal()
.domain( […])
.range([…]);
load(jsonfile, legendtext, color) ;
};
;
【问题讨论】:
标签: d3.js geojson map-projections