【发布时间】:2019-02-22 08:29:13
【问题描述】:
我正在制作与其绩效评级相关的企业世界地图:因此每个企业都将由一个点表示,该点有一个带有绩效(和其他信息)的工具提示。我正在使用地图示例 here
地图数据:
pointData = {
"businessName": businessName,
"location": location,
"performance": currperformance
}
pointsData.push(pointData);
因此 pointsData JSON 对象的形式为
[{"business":"b1","location":[long1, lat1]},{"businessName":"b2","location":[long2, lat2]}...]
工具提示问题:
我可以完美地显示带有点和相同工具提示的地图,直到我尝试使用不同的工具提示。我使用动态工具提示研究过的许多 D3 示例仅适用于图表 - 我的困难是在地图上的每个 SVG 圆圈上附加工具提示的 JSON 数据。
这是我迄今为止的尝试,它显示 no points 并且没有显示控制台错误(添加 .each(function (d, i) {..}doesn't 不再绘制部件,但有必要将每个位置链接到它的后续业务和绩效评级。)
d3.json("https://raw.githubusercontent.com/d3/d3.github.com/master/world-110m.v1.json", function (error, topo) {
if (error) throw error;
gBackground.append("g")
.attr("id", "country")
.selectAll("path")
.data(topojson.feature(topo, topo.objects.countries).features)
.enter().append("path")
.attr("d", path);
gBackground.append("path")
.datum(topojson.mesh(topo, topo.objects.countries, function (a, b) { return a !== b; }))
.attr("id", "country-borders")
.attr("d", path);
//Tooltip Implementation
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style('opacity', 0)
.style('position', 'absolute')
.style('padding', '0 10px');
gPoints.selectAll("circle")
.each(function (d, i) {
this.data(pointsData.location).enter()
.append("circle")
.attr("cx", function (d, i) { return projection(d)[0]; })
.attr("cy", function (d, i) { return projection(d)[1]; })
.attr("r", "10px")
.style("fill", "yellow")
.on('mouseover', function (d) {
tooltip.transition()
.style('opacity', .9)
.style('background', 'black')
.text("Business" + pointsData.businessName + "performance" + pointsData.performance)
.style('left', (d3.event.pageX - 35) + 'px')
.style('top', (d3.event.pageY - 30) + 'px')
})
.on('mouseout', function (d) {
tooltip.transition()
.style("visibility", "hidden");
})
});
});
【问题讨论】:
-
你不需要
each。只需使用常规输入选择。 -
@GerardoFurtado 是的
.data(points).enter()确实在我拥有所有地图纬度和经度的数组时绘制了所有点 - 但我试图循环通过 pointsData 对象,因为链接 each 纬度/经度圈到它的对应数据,以便每个工具提示可以不同 -
每个工具提示中的数据会有所不同,只需简单、常规的回车选择即可。您是否在“鼠标悬停”回调中看到您从未使用过的
d参数?使用它。 -
我不确定,但是您似乎误解了d3的数据连接原理。 d 将是每个数据点在 3 种状态之一的迭代(输入、删除、更新)bost.ocks.org/mike/join
标签: javascript d3.js svg tooltip