javascript - 条形图和数据之间的共享工具提示未在 D3 堆栈条形图中成功呈现
问题描述
我正在使用 d3-tip.js 向堆叠条形图添加工具提示。我的工具提示的预期输出应该是当鼠标悬停到栏的特定堆栈时,工具提示将显示该特定堆栈的人口。如下图所示,每个条上的每个堆栈代表一个性别,男性或女性。条形上方的数字是男性和女性人口的总和。现在我希望工具提示显示每个性别的个体人口。但是,我的实现中有 3 个问题:
数据似乎没有渲染成功,如下图所示,数据没有成功渲染。
第二个问题是栏之间共享的工具提示。例如,如下图所示,当我的鼠标在未知关系的“女性”(粉红色)堆栈上时,您可以看到“未知”栏和“其他”栏的堆栈都被突出显示,所以看起来像他们跨栏共享相同的工具提示。
- 工具提示显示的位置不正确。如下图,当我的鼠标在Other的Male(蓝色)堆栈上时,tooltip的位置不在“Other”上,而是在“unknown”栏上。
下面是添加工具提示的代码:
var tip = d3.tip()
.attr('class', "d3-tip")
.style("color", "white")
.style("background-color", "black")
.style("padding", "6px")
.style("border-radius", "4px")
.style("font-size", "12px")
.offset([-10, 0])
.html(function(d) { return `<strong>${d3.format(',')(keys, d => z(d.key))}</strong> population`; });
svg.call(tip);
svg
.selectAll('g.layer')
.on('mouseover', function(d) {
// show the tooltip on mouse over
tip.show(d, this);
// when the bar is mouse-overed, we slightly decrease opacity of the bar.
d3.select(this).style('opacity', 0.7);
})
.on('mouseout', function(d) {
// hide the tooltip on mouse out
tip.hide();
d3.select(this).style('opacity', 1)
});
这是我在 Plunker 中的图表的现场演示的链接:
解决方案
推荐阅读
- html - 如何使用 svg 过滤器在图像过渡中添加更多图像?
- javascript - 渲染组件及其状态
- asp.net-core - 偶尔 System.NotSupportedException:没有数据可用于编码 1252
- dynamics-crm - Microsoft Dynamics 365 (on-premises v8.2) 如何为自定义角色启用 ISH 的子区域按钮?
- sql - 在组合行时按多列分组
- typescript - 错误类型错误:changes.forEach 不是函数
- python - 按列值复制熊猫数据框中的行并添加具有重复索引的新列
- python - python flask从Pycharm运行应用程序时如何从终端访问CLI
- ffmpeg - ffmpeg 交叉淡入淡出 2 张 1080p 的图片
- android - 实施 Material Design 入职示例