【问题标题】:d3.js opening a new tab by dbclick eventd3.js 通过 dbclick 事件打开一个新选项卡
【发布时间】:2016-06-09 09:35:39
【问题描述】:

我对 d3 很陌生。我正在使用下面的代码根据 d3 网站中的示例对我的投资组合项目进行网络类型分析。 现在我正在寻找一种在打开新​​选项卡并转到相应网址的圆圈上添加双击事件的方法。 我已经尝试过这些,但到目前为止都做不到。

double click event on node

open new tab after doubleclick on element

非常感谢任何帮助。

var links = [
  {source: "Not Another one", target: "Respond", type: "solid", url:'https://www.wikipedia.org/'},
  {source: "Not Another one", target: "Pause", type: "link", url:'https://facebook.com/'},
  {source: "Who Steals Bikes?", target: "Respond", type: "solid", url:'https://www.yahoo.com/'},
  {source: "Who Steals Bikes?", target:  "Pause", type: "link", url:'https://www.google.com/'},
  {source: "Toward a Moving Structure", target: "Respond", type: "link", url:'https://www.github.com/'},
  {source: "Why Tall?", target: "Respond", type: "link", url:'http://www.mahanmehrvarz.name/'}
  ];

var nodes = {};

// Compute the distinct nodes from the links.
links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});

var width = 1000,
    height = 600;

var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([width, height])
    .linkDistance(150)
    .charge(-300)
    .on("tick", tick)
    .start();

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);


// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
    .data(["suit", "solid", "link"])
  .append("path")
     .attr("d", "M0,-5L10,0L0,5");

var path = svg.append("g").selectAll("path")
    .data(force.links())
  .enter().append("path")
    .attr("class", function(d) { return "link " + d.type; })
    .attr("marker-end", function(d) { return "url(#" + d.type + ")"; });

var circle = svg.append("g").selectAll("circle")
    .data(force.nodes())
  .enter().append("circle")

    .attr("r", 6)
    .call(force.drag);

var text = svg.append("g").selectAll("text")
    .data(force.nodes())
  .enter().append("text")
    .attr("x", 26)
    .attr("y", ".31em")
    .text(function(d) { return d.name; });

// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
  path.attr("d", linkArc);
  circle.attr("transform", transform);
  text.attr("transform", transform);
}


function linkArc(d) {
  var dx = dx = d.target.x - d.source.x,
      dy = d.target.y - d.source.y,
      dr = 0
  return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}

function transform(d) {
  return "translate(" + d.x + "," + d.y + ")";
}

【问题讨论】:

标签: javascript d3.js svg


【解决方案1】:

我找到了像你说的这个例子:D3js: how to open new tab after doubleclick on element?

但帮助的是这个:JavaScript: location.href to open in new window/tab?

关键代码:

window.open(
      htmlLinkHere,
      '_blank' // <- This is what makes it open in a new window.
    );

所以 onclick 就这样做:

.on('click', function(d) {
    console.log('open tab')
    window.open(
      'http://en.wikipedia.org',
      '_blank' // <- This is what makes it open in a new window.
    );
  });

我刚刚放了一个 wiki 页面,但在您的示例中,该行将使用 d.url

编辑

简单来说:

 .on('click', function(d) {
        console.log('open tab')
        window.open(
          d.url,
          '_blank' // <- This is what makes it open in a new window.
        );
      });

检查实现的小提琴(单击节点以在新选项卡中打开):http://jsfiddle.net/thatOneGuy/0nv4ck58/5/

编辑

你说你不想它在节点上工作,那么你必须将数据传递给节点。即节点必须有 URL 数据。你可以这样做:

links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, url:link.url});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, url : link.url});
});

但是,如果节点有多个链接,则 url 可能会发生冲突。现在您有了数据,只需将点击侦听器添加到您的节点:

var circle = svg.append("g").selectAll("circle")
    .data(force.nodes())
  .enter().append("circle")
 .on('click', function(d) {
            console.log('open tab')
            window.open(
              d.url,
              '_blank' // <- This is what makes it open in a new window.
            );
          });

【讨论】:

  • 比你好多了。这对我来说很完美而且很清楚。现在通过单击它打开另一个选项卡。但我希望它查看“链接”字典并基于此打开与每个圈子相关的“url”。换句话说,每个圈子都有不同的网址,它们并不相同。 (我还编辑了代码并将不同的网址放在更清晰的位置)。谢谢你的帮助
  • 我已经把它放在了答案中。只需在window.open的第一个参数中返回d.url
  • 好吧,我以为我错过了什么。 d.url 只是返回一个新的空白选项卡。 :(
  • 我认为我的数据结构存在问题,当我调用 url 时会导致空白。但我不知道在哪里以及如何解决它。感谢您的帮助。
  • 'ReferenceError: d is not defined'
猜你喜欢
  • 1970-01-01
  • 2019-11-02
  • 2011-04-02
  • 2023-03-21
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多