javascript – d3js使最后一个圆圈成为超链接

前端之家收集整理的这篇文章主要介绍了javascript – d3js使最后一个圆圈成为超链接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的……我正在使用 http://bl.ocks.org/mbostock/7607535的Zoomable Circle Packing
我打开了flare.json文件并开始搞乱它并且能够成功地操作它.它看起来像这样:

flare.json

  1. {
  2. "name": "flare","children": [
  3. {
  4. "name": "Kommunikation und Umwelt","children": [
  5.  
  6. {
  7. "name": "Courses","children": [
  8. {
  9. "name": "AO-Psy.","children": [
  10. {"name": "Prof. A","size": 5731,"url":"google.com"},{"name": "Prof. B","size": 5731},{"name": "Prof. C","size": 5731}
  11. ]
  12. },{
  13. "name": "E&E","children": [
  14. {"name": "Prof. D",{"name": "Prof. E",{"name": "Prof. F",{"name": "Prof. G",{"name": "Prof. H",{
  15. "name": "IBSS","children": [
  16. {"name": "Prof. I",{"name": "Prof. J",{"name": "Prof. K",{"name": "","size": 0},{
  17. "name": "E-Gov","children": [
  18. {"name": "Prof. L",{"name": "Prof. M",{"name": "Prof. N",{
  19. "name": "Muki","children": [
  20. {"name": "Prof. O",{"name": "Prof. P",{"name": "Prof. Q",{"name": "Prof. V",{"name": "Schedule",{"name": "News",{"name": "Events",{"name": "Search","size": 0}
  21. ]
  22. },"size": 0}
  23. ]

},

我现在想做的是尝试添加链接.例如,我希望能够点击“ProfA”并转到我将创建的另一个html页面.我可以对flare.json进行修改吗?

我已经找到了一些帖子Post1 Post2 Post3
但没有什么工作它只是再缩小

这里完整的html文件,flare.json已经发布在这里(短片)
zoom.html:

  1. <html xmlns:xlink="http://www.w3.org/1999/xlink">
  2. <Meta charset="utf-8">
  3. <style>
  4.  
  5. .node {
  6. cursor: pointer;
  7. }
  8.  
  9. .node:hover {
  10. stroke: #000;
  11. stroke-width: 1.5px;
  12. }
  13.  
  14. .node--leaf {
  15. fill: #14DCD2;
  16. }
  17.  
  18. .label {
  19. font: 20px "Helvetica Neue",Helvetica,Arial,sans-serif;
  20. text-anchor: middle;
  21. text-shadow: 0 1px 0 #fff,1px 0 0 #fff,-1px 0 0 #fff,0 -1px 0 #fff;
  22. }
  23.  
  24. .label,.node--root,.node--leaf {
  25. pointer-events: none;
  26. }
  27.  
  28. </style>
  29. <body>
  30. <script src="http://d3js.org/d3.v3.min.js"></script>
  31. <script>
  32.  
  33.  
  34. var margin = 600,diameter = 1920;
  35.  
  36. var color = d3.scale.linear()
  37. .domain([-1,5])
  38. .range(["hsl(152,80%,80%)","hsl(228,30%,40%)"])
  39. .interpolate(d3.interpolateHcl);
  40.  
  41. var pack = d3.layout.pack()
  42. .padding(2)
  43. .size([diameter - margin,diameter - margin])
  44. .value(function(d) { return d.size; })
  45.  
  46. var svg = d3.select("body").append("svg")
  47. .attr("width",diameter)
  48. .attr("height",diameter)
  49. .append("g")
  50. .attr("transform","translate(" + diameter / 2 + "," + diameter / 2 + ")");
  51.  
  52. d3.json("flare.json",function(error,root) {
  53. if (error) return console.error(error);
  54.  
  55. var focus = root,nodes = pack.nodes(root),view;
  56.  
  57. var circle = svg.selectAll("circle")
  58. .data(nodes)
  59. .enter().append("circle")
  60. .attr("class",function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
  61. .style("fill",function(d) { return d.children ? color(d.depth) : null; })
  62. .on("click",function(d) { if (focus !== d) zoom(d),d3.event.stopPropagation(); })
  63.  
  64.  
  65.  
  66. var text = svg.selectAll("text")
  67. .data(nodes)
  68. .enter().append("text")
  69. .attr("class","label")
  70. .style("fill-opacity",function(d) { return d.parent === root ? 1 : 0; })
  71. .style("display",function(d) { return d.parent === root ? null : "none"; })
  72. .text(function(d) { return d.name; })
  73. //.on('click',function(d,i) {window.location.href = d.url;});
  74.  
  75. var node = svg.selectAll("circle,text")
  76.  
  77. node.each(function(d){
  78. var thisNode = d3.select(this);
  79. if (!d.children) {
  80. thisNode.append("a")
  81. .attr("xlink:href",function(d) { return d.url; })
  82. .append("text")
  83. .attr("dx",8)
  84. .attr("dy",3)
  85. .attr("text-anchor","start")
  86. .text(function(d) { return d.name; })
  87. ;
  88. } else {
  89. thisNode.append("text")
  90. .attr("dx",-8)
  91. .attr("dy",3)
  92. .attr("text-anchor","end")
  93. .text(function(d) { return d.name; });
  94. }
  95.  
  96. });
  97.  
  98.  
  99.  
  100.  
  101. d3.select("body")
  102. .style("background",color(-1))
  103. .on("dblclick",function() { zoom(root); });
  104.  
  105. zoomTo([root.x,root.y,root.r * 2 + margin]);
  106.  
  107. function zoom(d) {
  108. var focus0 = focus; focus = d;
  109. //.attr("xlink:href",url);
  110. //.on('click',i) {window.location.href = d.url;});
  111.  
  112. var transition = d3.transition()
  113. .duration(d3.event.altKey ? 7500 : 750)
  114. .tween("zoom",function(d) {
  115. var i = d3.interpolateZoom(view,[focus.x,focus.y,focus.r * 4 + margin]);
  116. return function(t) { zoomTo(i(t)); };
  117. });
  118.  
  119. transition.selectAll("text")
  120. .filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
  121. .style("fill-opacity",function(d) { return d.parent === focus ? 1 : 0; })
  122. .each("start",function(d) { if (d.parent === focus) this.style.display = "inline"; })
  123. .each("end",function(d) { if (d.parent !== focus) this.style.display = "none"; });
  124.  
  125. }
  126.  
  127. function zoomTo(v) {
  128. var k = diameter / v[2]; view = v;
  129. node.attr("transform",function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
  130. circle.attr("r",function(d) { return d.r * k; });
  131.  
  132. }
  133.  
  134. });
  135.  
  136. d3.select(self.frameElement).style("height",diameter + "px");
  137.  
  138. </script>
  139. </html>

解决方法

您的设置中的问题主要源于您有指针事件:某些元素没有,例如叶子(最小的圆圈).

如果您更正并定义click事件以使其指向url而不是触发叶子的zoom事件,则会获得所需的行为.

我为你准备了一个小小提琴,请看这里:
http://jsfiddle.net/chroth/fkxcvtu9/3/

这个想法的核心在这里(点击功能):

  1. function clickFct(d,i) {
  2. if (d3.select(this).classed("node--leaf")) {
  3. alert(d.url); //open URL here
  4. } else {
  5. if (focus !== d)
  6. {
  7. zoom(d);
  8. d3.event.stopPropagation();
  9. }
  10. }
  11. }

并在这种风格的变化:

  1. .node--root,.node--leaf {
  2. pointer-events: all;
  3. }

待办事项

如你所见,目前我只是在发出警报.此外,您可能希望在未放大等时禁用某些点击事件.再加上颜色.

另请注意,您需要修复标签的可见性等.

但我把它留给你:)

希望有所帮助.

猜你在找的JavaScript相关文章