【发布时间】:2017-02-15 04:13:28
【问题描述】:
我正在尝试将俄克拉荷马州与德克萨斯州的一个县合并。
我有两个变量,一个使用整个俄克拉荷马州的州 ID,另一个包含德克萨斯州县的县 ID。如何组合 state.geometries.filter 和 County.geometries.filter?德克萨斯州实际上有许多县将被合并,但为了举例,这个县是随机选择的。
我的代码,只返回最底部的数据,如下:
.county-borders {
fill: none;
stroke: #fff;
stroke-width: 0.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
.state-borders {
fill: none;
stroke: #fff;
stroke-width: 0.7px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
.state.southcentral {
fill: steelblue;
stroke: #fff;
}
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
var svg = d3.select("svg");
var path = d3.geoPath();
var southcentral = {
"40": 1
};
var southcentral_c = {
"48043": 1
};
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("d", path);
svg.append("path")
.attr("class", "state-borders")
.attr("d", path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })));
//multiple datum, only shows the bottom most one. Need to combine into one merge but they use different objects in the JSON.
svg.append("path")
.datum(topojson.merge(us, us.objects.states.geometries.filter(function(d) { return d.id in southcentral; })))
.datum(topojson.merge(us, us.objects.counties.geometries.filter(function(d) { return d.id in southcentral_c; })))
.attr("class", "state southcentral")
.attr("d", path);
});
【问题讨论】:
标签: javascript d3.js topojson