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

时间:2019-10-10 09:16:18

标签: javascript d3.js

我的工具提示在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

enter image description here

1 个答案:

答案 0 :(得分:0)

在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)");
}
相关问题