Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在D3 force布局中节点的鼠标上方显示div元素_Javascript_Html_Svg_D3.js_Force Layout - Fatal编程技术网

Javascript 在D3 force布局中节点的鼠标上方显示div元素

Javascript 在D3 force布局中节点的鼠标上方显示div元素,javascript,html,svg,d3.js,force-layout,Javascript,Html,Svg,D3.js,Force Layout,我需要在d3.js生成的图中的节点上的鼠标上显示HTMLdiv元素。主要目的是以结构化的方式显示与悬停节点相关的附加信息。此信息包含链接、图像和文本。下面是我为生成图形而编写的代码。它是静态图形,用户可以在其中移动窗口框架内的节点: <!DOCTYPE html> <meta charset="utf-8"> <style> .node { stroke: #fff; stroke-width: 1.5px; } .node.fixed { b

我需要在d3.js生成的图中的节点上的鼠标上显示HTMLdiv元素。主要目的是以结构化的方式显示与悬停节点相关的附加信息。此信息包含链接、图像和文本。下面是我为生成图形而编写的代码。它是静态图形,用户可以在其中移动窗口框架内的节点:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.node {
  stroke: #fff;
  stroke-width: 1.5px;
}

.node.fixed {
  border: 2px;
  border-color: #foo;
}

.link {
  stroke: #000;
  fill: none;
  stroke-width: 2px;
  cursor: default;
}

.nodeLabel {
  stroke: #000;
  stroke-width: 1px;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 1000,
    height = 850;

var color = d3.scale.category20();
// A map from group ID to image URL.
var imageByGroup = {
  "0": "./images/1.png",
  "1": "./images/2.png",
  "2": "./images/3.png",
  "3": "./images/4.png",
  "4": "./images/5.png",
  "5": "./images/6.png",
  "6": "./images/7.png",
  "7": "./images/8.png",
  "8": "./images/9.png"
};


var force = d3.layout.force()
    .charge(-100)
    .linkDistance(150)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .call(d3.behavior.zoom().on("zoom", redraw));

// Per-type markers, as they don't inherit styles.
svg.append("svg:defs").append("svg:marker")
    .attr("id", 'end-arrow')
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 17)
    .attr("refY", 0)
    .attr("markerWidth", 7)
    .attr("markerHeight", 7)
    .attr("orient", "auto")
  .append("svg:path")
    .attr("d", "M5,0L0,-5L10,0L0,5L5,0")
    .attr('fill', '#000');  

d3.json("input.json", function(error, graph) {
  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style('marker-end', 'url(#end-arrow)');

 var drag = force.drag()
    .on("dragstart", dragstart);      

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(drag);

  node.append("image")
      .attr("xlink:href", function(d) {return imageByGroup[d.group];})
      .attr("x", -24)
      .attr("y", -24)
      .attr("width", 48)
      .attr("height", 48);  

    node.append("text")
        .attr("text-anchor", "middle")
        .attr("dy",35)
        .attr("class", "nodeLabel")
        .text(function(d) { return d.name; });    

  node.append("title")
      .text(function(d) { return d.id+"--"+d.name; });
  node.on("click", function(d){//alert(d.name)
  });

  force.on("tick", function() {
    node.attr("cx", function(d) { return Math.min(width,d.x); })
        .attr("cy", function(d) { return Math.min(height,d.y); })
        .attr("transform", function(d) { return "translate(" + Math.min(width,d.x)+ "," + Math.min(height,d.y) + ")"; });
    link.attr("x1", function(d) { return Math.min(width,d.source.x); })
        .attr("y1", function(d) { return Math.min(height,d.source.y); })
        .attr("x2", function(d) { return Math.min(width,d.target.x); })
        .attr("y2", function(d) { return Math.min(height,d.target.y); });       
  });


});

function redraw() {
  svg.attr("transform", " scale(" + d3.event.scale + ")");
}
  function dragstart(d) {
      d.fixed = true;
      d3.select(this).classed("fixed", true);
  }
</script>
文本工具提示可以很好地处理上述代码,但我需要以如上所述的格式化方式显示更多图像和链接


此外,如果您能让我知道我是否可以更改悬停节点的图像,使其显示为高亮显示,这将非常有用。

您只需使用以下命令绑定操作:

node.on("mouseover", mouseover)
    .on("mouseout", mouseout)
其中mouseover和mouseoutare函数用于在参数中获取悬停节点:

function mouseover(d) {
    // d is the node object
    // You can even get mouse position with this command
    var mousePos = d3.mouse(this);
}

本书还有一章解释了如何显示工具提示。

您只需使用以下命令绑定操作:

node.on("mouseover", mouseover)
    .on("mouseout", mouseout)
其中mouseover和mouseoutare函数用于在参数中获取悬停节点:

function mouseover(d) {
    // d is the node object
    // You can even get mouse position with this command
    var mousePos = d3.mouse(this);
}

本书还有一章解释了如何显示工具提示。

感谢您的建议。它起作用了,书的链接也很有用。我已经接受了这个答案。谢谢你的建议。它起作用了,书的链接也很有用。我接受了这个答案。