【问题标题】:Tooltip is going out of the box in d3 chart工具提示在 d3 图表中开箱即用
【发布时间】:2020-02-07 16:39:12
【问题描述】:

我的工具提示在 d3 图表中开箱即用。

有人可以指导我做错了什么吗?

css -

div.tooltip {
        position: absolute;
        width: 240px;
        height: 140px;
        font: 14px sans-serif;
        background: white;
        border: 0px;
        box-shadow: 0 0 5px #999999;
        border-radius: 2px;
        pointer-events: none;
        text-align: left;
        padding: 5px 0 5px 15px;
        opacity: 100;
    }

工具提示的 div -

// Define the div for the tooltip
var div = d3
    .select("body")
    .append("div")
    .attr("class", "tooltip")
    .style("opacity", 0);

我觉得mousemove 有问题,mouseover 请指导我。

代码沙箱在这里 - https://codesandbox.io/s/wizardly-butterfly-2nnbw

【问题讨论】:

标签: javascript d3.js


【解决方案1】:

在 TimelineChart.js 行号 513 上。 https://codesandbox.io/s/amazing-waterfall-gz4o8

div
.html(
`<span>The following changes are made to running configuration :</span><br/><br/>`
 <span>Lines Added : ${d.magnitude} % </span><br/>
<span>Lines Removed : ${d.magnitude} % </span><br/><br/>
<span>${parseDate(d.startTime)}</span>`
.style("left", d3.event.pageX + "px")
.style("top", d3.event.pageY - 28 + "px");

用这个替换

div
.html(
`<span>The following changes are made to running configuration :</span><br/><br/>
<span>Lines Added : ${d.magnitude} % </span><br/>
<span>Lines Removed : ${d.magnitude} % </span><br/><br/>
<span>${parseDate(d.startTime)}</span>`
).style("top", d3.event.pageY - 28 + "px");
var out_width = this.getBoundingClientRect().width - 130;
if(d3.event.pageX > out_width){
div.style("left", d3.event.pageX + "px")
div.style("transform", "translateX(-100%)");
}
else{
div.style("left", d3.event.pageX + "px");
div.style("transform", "translateX(0)");
}

【讨论】:

  • 感谢您的回复,为什么您在此行中特别给出了var out_width = this.getBoundingClientRect().width - 130; 130
  • 在我的代码中,我正在使用(window.innerWidth * 66)/100 this 计算图表的宽度,然后重新绘制我的图表,我应该用什么来代替130
  • 这是正常和基本的代码,我计算了图表 SVG 宽度和 130 的简单钩子。请使用 Window.outerWidth 它比 window.innerWIdth 更好,因为它是用填充和其他东西计算出的宽度。
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
相关资源
最近更新 更多