小编Bog*_*scu的帖子

如何使用 d3.js 在显示数据时在鼠标上绘制垂直线

如何将垂直线附加到图表并在工具提示上显示关注的数据?

像这样的东西:

带有垂直线的图形

任务:

  • 添加线条指示器和工具提示
  • 将不可见的div附加到vis容器,将其类设置为“tooltip”并使用index.css定义必要的样式(例如位置)
  • 将指示线附加到视口
  • 附加一个矩形并将其类设置为“interaction-rect”(请参阅​​index.css)。我们将使用这个矩形来捕获鼠标事件
  • 每当鼠标移动时,更新工具提示以显示正确的日期和值
  • 每当鼠标离开视口时,使指示器和工具提示消失

代码:

/* Retrieve the node of the div element declared within the index.html by its identifier */

var visContainerNode = d3.select("#vis-container");

// Specify margins such that the visualization is clearly visible and no elements are invisible due to the svg border
var margins = {
    top: 20,
    right: 25,
    bottom: 20,
    left: 50
};

// Specify the width and height of the svg as well as the width height of the …
Run Code Online (Sandbox Code Playgroud)

d3.js

5
推荐指数
0
解决办法
1727
查看次数

标签 统计

d3.js ×1