javascript - 在 d3.js 中使用 translate() 将工具提示放置在表格单元格上

标签 javascript d3.js

我正在从 json 数据和 d3 创建一个表,如下所示:https://jsfiddle.net/rpardee/ku3q6txw/41/

我使用 mouseenter 和 d3.mouse() 来获取鼠标指针的 x,y 坐标,但需要将从 .mouse() 返回的坐标转换为可以输入到 translate() 的像素位置以便正确放置工具提示。到目前为止,我的胡乱导致了:

    // Move the tooltip over to where the mouse pointer is
    // There has *got* to be a better way of doing this.
    const coords = d3.mouse(this);
    const xpos = table._groups[0][0].clientWidth - coords[0] ;
    const ypos = table._groups[0][0].clientHeight - coords[1] ; ;
    imp_tooltip.style("transform", `translate(${xpos}px, ${ypos}px)`) ;

很接近,但还不够接近。

我猜我可以设置一个 d3.scaleLinear() 来进行此翻译,但我在尝试找出要输入到该家伙的 .domain() 和 range() 方法中的内容时遇到了困难。

有人可以给我线索吗?

最佳答案

可以使用 d3.event.pageXd3.event.pageY ( reference ) 检索鼠标位置。这些坐标可以直接用于翻译:

// Move the tooltip over to where the mouse pointer is
const xpos = d3.event.pageX ;
const ypos = d3.event.pageY ;
imp_tooltip.style("transform", `translate(${xpos}px, ${ypos}px)`) ;

更新了jsfiddle:https://jsfiddle.net/mohgpr2c/1/

关于javascript - 在 d3.js 中使用 translate() 将工具提示放置在表格单元格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60918665/

相关文章:

javascript - 从 iframe 中的 d3 引用一个 div

javascript - Mouseevent div 覆盖图表上的 div

datetime - 如何解析以微秒为单位的日期字符串

javascript - 为什么我的排列计算器返回小数?

Javascript:从变量本身引用变量名

javascript - 从名称 + 变量中随时写入数组名称

d3.js - D3v4 - 在 ("end"上)在 transition.duration 结束之前调用

javascript - 你能用 this.queryText.replace 替换两个不同的文本吗?

javascript - jQuery 如何为append() 或clone() 存储元素

javascript - 使用 d3.js 单击时的爆炸饼图