带链接的 D3 树形图

2023-12-04

我是新来的d3.js图书馆。

我正在尝试制作一棵树this one,但带有一个转到每个节点上的外部页面的链接。

是否可以?

我尝试向每个节点添加一个“svg:a”,但使所有树都消失。

Update:

我从上面链接的页面的 html 中获取此代码。

链接的库是:

  • d3.js
  • d3.layout.js

这是全部代码:

<style type="text/css">
.node circle {
  cursor: pointer;
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node text {
  font-size: 11px;
}

path.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

    </style>
<script>
var m = [20, 120, 20, 120],
    w = 1280 - m[1] - m[3],
    h = 800 - m[0] - m[2],
    i = 0,
    root;

var tree = d3.layout.tree()
    .size([h, w]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var vis = d3.select("#body").append("svg:svg")
    .attr("width", w + m[1] + m[3])
    .attr("height", h + m[0] + m[2])
  .append("svg:g")
    .attr("transform", "translate(" + m[3] + "," + m[0] + ")");

d3.json("flare.json", function(json) {
  root = json;
  root.x0 = h / 2;
  root.y0 = 0;

  function toggleAll(d) {
    if (d.children) {
      d.children.forEach(toggleAll);
      toggle(d);
    }
  }

  // Initialize the display to show a few nodes.
  root.children.forEach(toggleAll);
  toggle(root.children[1]);
  toggle(root.children[1].children[2]);
  toggle(root.children[9]);
  toggle(root.children[9].children[0]);

  update(root);
});

function update(source) {
  var duration = d3.event && d3.event.altKey ? 5000 : 500;

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse();

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 180; });

  // Update the nodes…
  var node = vis.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("svg:g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
      .on("click", function(d) { toggle(d); update(d); });

  nodeEnter.append("svg:circle")
      .attr("r", 1e-6)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

  nodeEnter.append("svg:text")
      .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
      .attr("dy", ".35em")
      .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
      .text(function(d) { return d.name; })
      .style("fill-opacity", 1e-6);

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

  nodeUpdate.select("circle")
      .attr("r", 4.5)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

  nodeUpdate.select("text")
      .style("fill-opacity", 1);

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("circle")
      .attr("r", 1e-6);

  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

  // Update the links…
  var link = vis.selectAll("path.link")
      .data(tree.links(nodes), function(d) { return d.target.id; });

  // Enter any new links at the parent's previous position.
  link.enter().insert("svg:path", "g")
      .attr("class", "link")
      .attr("d", function(d) {
        var o = {x: source.x0, y: source.y0};
        return diagonal({source: o, target: o});
      })
    .transition()
      .duration(duration)
      .attr("d", diagonal);

  // Transition links to their new position.
  link.transition()
      .duration(duration)
      .attr("d", diagonal);

  // Transition exiting nodes to the parent's new position.
  link.exit().transition()
      .duration(duration)
      .attr("d", function(d) {
        var o = {x: source.x, y: source.y};
        return diagonal({source: o, target: o});
      })
      .remove();

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
  });
}

// Toggle children.
function toggle(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
}

    </script>

基本上我尝试过的是在之前添加这段代码nodeEnter.append("svg:text")

  nodeEnter.append("svg:a")
      .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
      .attr("dy", ".35em")
      .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
      .text("http://example.com")
      .style("fill-opacity", 1e-6);

尝试在节点本身上添加操作,如下所示,并将光标更改为指针以向用户提供提示。

  var node = vis.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); })
      .style("cursor", "pointer")
      .on("click", function() {
          window.open("http://www.stackoverflow.com", '_blank').focus();
      });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带链接的 D3 树形图 的相关文章

随机推荐

  • 如何从Android的内部和外部存储中获取所有.mp3文件

    我想做一个音乐播放器 但我无法从内部和外部存储中获取所有 mp3 文件 谁能帮助我吗 提前致谢 这是我的代码 public void getListOfSong1 Context context SongData cart clear Cu
  • 最大应力节点

    我正在尝试生成脚本以在最大应力位置自动创建模型视图 我知道我可以读取每个节点并使用 python 检查每个节点的压力 但是由于模型的大小 这将需要一段时间 由于此功能出现在 CAE 中 是否有一种简单的方法来编写脚本 一旦我知道了我感兴趣的
  • 在 GWT 中以编程方式调用 click() 函数

    我想在GWT中调用按钮的点击事件函数 我尝试了这段代码 但它不起作用 Button btnAddField new Button btnAddField setText Add btnAddField setWidth 225px btnA
  • 通过 XSD 防止 XML 中的空元素

    我正在处理一个 XSD 文件 当用它验证 XML 文件时 我想限制空元素 例如
  • 在 opencart 中显示子类别中的图像

    最近我第一次安装了Opencart 1 5 6 一切都很好 除了我试图在子类别中显示图像和文本 其中是 优化搜索文本 到目前为止我已经把这个放进去了catalog controller module category php childre
  • 模糊图像的卷积产生粗略的输出图像

    这是我的代码 void Blur NOT Sketch IplImage img int rows img gt height cols img gt width row col i j ki kj float sum 0 k 2 0 2
  • Go 提供 REPL 吗?

    交互式环境对程序员非常有帮助 然而 Go 似乎没有提供它 我的理解正确吗 不 Go 不提供REPL 读取 评估 打印循环 然而 正如已经提到的 去游乐场非常方便 Go 作者也在考虑为其添加一个功能丰富的编辑器 如果您想要本地的东西 请考虑安
  • Rake db:迁移错误“不知道如何构建任务”

    我有一个表 其中在需要小数位的字段上使用整数 因此我尝试创建一个迁移 将字段类型从整数更改为浮点 实数 我的数据库是sqllite3 我使用的是rails3 I ran rails generate migration ChangeMeas
  • 我在哪里可以获取文件 libstdc++.so.6.0.15 [关闭]

    Closed 这个问题是无关 目前不接受答案 我正在尝试解决我的问题 GLIBCXX 3 4 15 未找到 该问题有相当详细的记录可以修复 但它需要从某个地方获取文件 libstdc so 6 0 15 大多数解决方案告诉我从编译 C 库的
  • 图库一次滚动一张图像

    如何使图库控件一次滚动一张图像 另外 制作这些图像的连续循环的好方法是什么 我尝试重写 onFling 根本不起作用 这将图像移动一定的距离 但并没有真正实现 真正的分页 Override public boolean onFling Mo
  • sqlite3 按 max 查询并按第二个因素过滤

    I have TABLE MESSAGES message id conversation id from user timestamp message I want 1 SELECT WHERE from user lt gt id 2
  • 如果表单未提交则触发 onbeforeunload

    我有一个通过 PHP 提交的表单 有 3 个提交操作 保存并继续 保存并退出 退出而不保存 如果用户没有单击任何表单操作来通知他们要离开页面 并且他们的更改可能不会保存 我想触发 OnBeforeUnload 警报来显示 我已经尝试了以下代
  • .Net数组大小的限制

    我听说 Net 的大小有硬性限制Array 据说可以分配给单个实例的最大内存量Array对象 无论它是否int double 或您自己的阵列 是 2GB 不 如果您有 64 位计算机 则 2GB 限制仍然存在 我不确定我的印象是否正确 任何
  • 如何从递归函数返回数组

    我找不到解决方案 而且我没有太多时间 所以我想要的是创建一个函数 我给出类别 ID 它返回类别的所有 ID 这是它的子类别 function getID var categories array function getChildren i
  • 如何“转储”使用 mpld3 的 LinkedBrush 插件选择的点?

    我正在尝试实现一个插件 允许用户转储有关 LinkedBrush 插件选择的点的相关信息 我认为我的问题与这个例子 我通过 HTMLTooltip 插件将元信息与每个点相关联 理想情况下 我也能以某种方式抛弃它 在我链接到的示例中 信息是通
  • Rails:关闭错误显示

    当我通过 URL 访问我的 Rails 项目中不存在的帖子时 posts 13 浏览器显示一个详细错误 ActiveRecord RecordNotFound in PostsController show Couldn t find Po
  • Wildfly 和 JAAS 登录模块

    我正在玩Wildfly 9 0 1 Final and JAAS但我没有那么多乐趣 我实现了我的自定义登录模块 public class MongoLoginModule implements LoginModule Inject prot
  • --add-modules 仅在编译时添加[重复]

    这个问题在这里已经有答案了 我正在用 Maven 构建我的项目java 9 我已经添加到我的pom xml file
  • R基于checkboxGroupInput闪亮选择变量

    我正在使用 Rshiny 开发一个交互式分析工具 现在我想根据 checkboxGroupInput 中的变量检查进行分类树 我如何选择该数据子集 谢谢 UI dateInput date Enter date value date che
  • 带链接的 D3 树形图

    我是新来的d3 js图书馆 我正在尝试制作一棵树this one 但带有一个转到每个节点上的外部页面的链接 是否可以 我尝试向每个节点添加一个 svg a 但使所有树都消失 Update 我从上面链接的页面的 html 中获取此代码 链接的