javascript - 在 D3 中添加 Node 链接

标签 javascript node.js d3.js

因此,我正在处理 d3 视觉效果中的一个问题,其中我试图创建具有可点击链接的 Node 。我刚刚学习 d3,所以在创建这些链接时遇到了困难。现在到目前为止,我已经在所有 Node 上获得了链接,但我想要实现的本质是为每个 Node 拥有不同的链接。目前,我只是为每个 Node 输入一个网址,但我如何选择一个单独的 Node 并附加一个链接到它。我希望拥有它,以便当用户单击对象 Node 时它会转到另一个页面。

var onode = svg.append('g').selectAll(".outer_node")
        .data(data.outer)
      .enter().append("g")
        .attr("class", "outer_node")
        .on("mouseover", mouseover)
        .on("mouseout", mouseout)
        .on("click", click);

function click (d){
    window.open("https://www.google.com", "_self");
    }

这使得每个 Node 都有相同的链接,我不知道如何分配不同的链接。有人可以帮忙吗?

最佳答案

假设数据集中存储了 URL,您可以在函数中访问该 URL:

function click (d){
    let url = "http://" + d.url; //or whatever your URL field is called in the data
    window.open(url, "_self");
}

您提到“数组中的位置”,因此如果 URL 位于单独的数组中,但与数据集对齐,那么您可以使用

function click (d, i){ //is this 0 based index of each item in the selection
    let url = urlData[i]
    window.open(url, "_self");
 }

关于javascript - 在 D3 中添加 Node 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46412369/

相关文章:

javascript - 我如何在AngularJS中创建类似的内容

javascript - 如何通过回调访问nodejs中的mongodb计数结果?

javascript - D3 TreeMap - 使用直线而不是对 Angular 线时如何转换链接

javascript - 如何格式化值数组以形成 google-charts 直方图的分布?

javascript - AngularJS Google App Engine API 加载错误

javascript - React-Native — 全局变量不变

node.js - 在 nodejs 项目 VSCode 中禁用 DOM 库类型定义

javascript - 如何链接条件异步回调?

javascript - X 轴上的 Nivo Line Formatting 时间刻度

javascript - 如何使用 React 应用程序将 ASP.NET Core Controller 操作 View 之一访问到 iframe 中?