【发布时间】:2014-04-13 11:34:54
【问题描述】:
我有一个包含状态路径的 topojson。我希望用户能够将鼠标悬停在一个状态上,并且该状态出现在不同的 svg 中。到目前为止,我已经尝试从 topojson 中提取几何(d.geometry、d.geometry.coordinates 等)但我无法做到。 也许我需要从中绘制一个多边形,但有些状态是“Polygon”类型的,其中一些是“MultiPolgyon”类型的。
有什么想法/建议吗?
编辑:这是我的代码
var svg = d3.select("#india-map")
.append("svg")
.attr("width",width).attr("preserveAspectRatio", "xMidYMid")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("height", height)
var stateSvg = d3.select("#state-map")
.append("svg")
.append("g")
.attr("height", height)
.attr("width", width);
var g = svg.append("g");
var projection = d3.geo.mercator()
.center([86, 27])
.scale(1200);
var path = d3.geo.path().projection(projection);
var pc_geojson = topojson.feature(pc, pc.objects.india_pc_2014);
var st_geojson = topojson.feature(state_json, state_json.objects.india_state_2014);
g.selectAll(".pc")
.data(pc_geojson.features)
.enter().append("path")
.attr("class", "pc")
.attr("d", path)
.attr("id", function(d){ return d.properties.Constituency;})
.attr("fill", "orange")
.on("click", function(d){
drawCons(d);
});
function drawCons(d){
stateSvg.selectAll(".pc2")
.data(d)
.enter().append("path")
.attr("class","pc2")
.attr("d", path)
}
【问题讨论】:
-
你试过用
topojson.feature()提取国家吗? -
是的,我通过执行
var g = topojson.feature(country, country.objects.states)提取geojson(这有效,我看到了地图)然后我尝试在悬停时执行此操作,var state = topojson.feature(g, d)但它不起作用 -
好的,在这种情况下,您不再需要
topojson.feature。g应该是 GeoJSON 格式的国家/地区列表,您可以从中提取所选国家/地区,例如通过检查名称或属于数据一部分的类似内容。 -
当我选择该对象时,我得到一个对象以键入“功能”。为了画这个,我正在做
svg.selectAll("path").data(feature).enter().append("path")但我没有看到在 html 中绘制 svg。 -
这段代码出现在什么上下文中? SVG 中已经有
paths 了吗?
标签: javascript d3.js topojson