【D3.js数据可视化系列教程】(二十四)--力导向图

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/zhang__tianxu/article/details/14106937

力导向图(可拖动)

  1. 数据格式

定义节点和连接对象数组。

【司机提示】

  • nodes和edges是必备的两个属性。
  • nodes中的数据是一个表示节点的对象数组;
  • edges是一个表示边的对象数组,其中source和target子属性是必备的,其值代表了nodes数组中的索引。
  • 节点和边数组都可以有额外的属性。

    
    
  1. var dataset={
  2. nodes:[ //节点
  3. { name: "Adam"},
  4. { name: "Bob"},
  5. { name: "Carride"},
  6. { name: "Donovan"},
  7. { name: "Edward"},
  8. { name: "Felicity"},
  9. { name: "George"},
  10. { name: "Hannah"},
  11. { name: "Iris"},
  12. { name: "Jerry"}
  13. ],
  14. edges:[ //边
  15. { source: 0, target: 1, weight: 1, color: 1},
  16. { source: 0, target: 2, weight: 3, color: 4},
  17. { source: 0, target: 3, weight: 4, color: 6},
  18. { source: 0, target: 4, weight: 6, color: 65},
  19. { source: 1, target: 5, weight: 3, color: 76},
  20. { source: 2, target: 5, weight: 8, color: 879},
  21. { source: 2, target: 5, weight: 7, color: 989},
  22. { source: 3, target: 4, weight: 9, color: 643},
  23. { source: 5, target: 8, weight: 1, color: 54},
  24. { source: 5, target: 9, weight: 3, color: 54},
  25. { source: 6, target: 7, weight: 4, color: 45},
  26. { source: 7, target: 8, weight: 0, color: 43},
  27. { source: 2, target: 8, weight: 8, color: 243},
  28. { source: 3, target: 8, weight: 1, color: 43},
  29. { source: 5, target: 8, weight: 5, color: 13},
  30. { source: 6, target: 8, weight: 3, color: 351},
  31. { source: 8, target: 9, weight: 4, color: 1}
  32. ]
  33. };

  2. 力布局

转化数据为适合生成力导向图的对象数组


    
    
  1. var force=d3.layout.force()
  2. .nodes(dataset.nodes) // 加载节点数据
  3. .links(dataset.edges) // 加载边数据
  4. .size([w,h]) // 设置有效空间的大小
  5. .linkDistance( 50) // 连线的长度
  6. .charge( - 200) // 负电荷量,相互排斥设置的负值越大越排斥
  7. .start(); // 启用力布局

  3. 画线

创建作为连线的svg直线


    
    
  1. var edges=svg.selectAll( "line")
  2. .data(dataset.edges)
  3. .enter()
  4. .append( "line")
  5. .style( "stroke", function(d){ // 设置线的颜色
  6. return colors(d.color);
  7. })
  8. .style( "stroke-width", function(d,i){ // 设置线的宽度
  9. return d.weight;
  10. });

  4. 画点

创建作为连线的svg圆形


    
    
  1. var nodes=svg.selectAll( "circle")
  2. .data(dataset.nodes)
  3. .enter()
  4. .append( "circle")
  5. .attr( "r", function(d){ // 设置圆点的半径,圆点的度越大weight属性值越大,可以对其做一点数学变换
  6. return Math.log(d.weight)* 10;
  7. })
  8. .style( "fill", function(d){
  9. return colors(d.weight*d.weight*d.weight);
  10. })
  11. .call(force.drag); //可以拖动

  5. 节拍

每个节拍力布局会重新模拟一次,直到自动收敛。


    
    
  1. force.on( "tick", function(){
  2. // 更新连线坐标
  3. edges.attr( "x1", function(d){
  4. return d.source.x;
  5. })
  6. .attr( "y1", function(d){
  7. return d.source.y;
  8. })
  9. .attr( "x2", function(d){
  10. return d.target.x;
  11. })
  12. .attr( "y2", function(d){
  13. return d.target.y;
  14. });
  15. // 更新节点坐标
  16. nodes.attr( "cx", function(d){
  17. return d.x;
  18. })
  19. .attr( "cy", function(d){
  20. return d.y;
  21. });
  22. })

  6. 完整代码


    
    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset= "utf-8" >
  5. <title>testD3- 22-force.html </title>
  6. <script type= "text/javascript" src= "d3.js" > </script>
  7. <style type= "text/css" >
  8. </style>
  9. </head>
  10. <body>
  11. <script type= "text/javascript" >
  12. var h= 300 ;
  13. var w= 300 ;
  14. // 颜色函数
  15. var colors=d3.scale.category20() //创建序数比例尺和包括20中颜色的输出范围
  16. //(1)定义节点和联系对象数组
  17. var dataset={
  18. nodes: [ //节点
  19. { name: "Adam" },
  20. { name: "Bob" },
  21. { name: "Carride" },
  22. { name: "Donovan" },
  23. { name: "Edward" },
  24. { name: "Felicity" },
  25. { name: "George" },
  26. { name: "Hannah" },
  27. { name: "Iris" },
  28. { name: "Jerry" }
  29. ],
  30. edges: [ //边
  31. { source: 0 , target: 1 , weight: 1 , color: 1 },
  32. { source: 0 , target: 2 , weight: 3 , color: 4 },
  33. { source: 0 , target: 3 , weight: 4 , color: 6 },
  34. { source: 0 , target: 4 , weight: 6 , color: 65 },
  35. { source: 1 , target: 5 , weight: 3 , color: 76 },
  36. { source: 2 , target: 5 , weight: 8 , color: 879 },
  37. { source: 2 , target: 5 , weight: 7 , color: 989 },
  38. { source: 3 , target: 4 , weight: 9 , color: 643 },
  39. { source: 5 , target: 8 , weight: 1 , color: 54 },
  40. { source: 5 , target: 9 , weight: 3 , color: 54 },
  41. { source: 6 , target: 7 , weight: 4 , color: 45 },
  42. { source: 7 , target: 8 , weight: 0 , color: 43 },
  43. { source: 2 , target: 8 , weight: 8 , color: 243 },
  44. { source: 3 , target: 8 , weight: 1 , color: 43 },
  45. { source: 5 , target: 8 , weight: 5 , color: 13 },
  46. { source: 6 , target: 8 , weight: 3 , color: 351 },
  47. { source: 8 , target: 9 , weight: 4 , color: 1 }
  48. ]
  49. };
  50. //(2)转化数据为适合生成力导向图的对象数组
  51. var force=d3.layout.force()
  52. .nodes(dataset.nodes) //加载节点数据
  53. .links(dataset.edges) //加载边数据
  54. .size([w,h]) //设置有效空间的大小
  55. .linkDistance( 50 ) //连线的长度
  56. .charge( - 200 ) //负电荷量,相互排斥设置的负值越大越排斥
  57. .start(); //设置生效
  58. var svg=d3.select( "body" )
  59. .append( "svg" )
  60. .attr( "width" ,w)
  61. .attr( "height" ,h);
  62. //(3)创建作为连线的svg直线
  63. var edges=svg.selectAll( "line" )
  64. .data(dataset.edges)
  65. .enter()
  66. .append( "line" )
  67. .style( "stroke" ,function(d){ // 设置线的颜色
  68. return colors(d.color);
  69. })
  70. .style( "stroke-width" ,function(d,i){ //设置线的宽度
  71. return d.weight;
  72. });
  73. //(4) 创建作为连线的svg圆形
  74. var nodes=svg.selectAll( "circle" )
  75. .data(dataset.nodes)
  76. .enter()
  77. .append( "circle" )
  78. .attr( "r" ,function(d){ //设置圆点的半径,圆点的度越大weight属性值越大,可以对其做一点数学变换
  79. return Math.log(d.weight)* 10 ;
  80. })
  81. .style( "fill" ,function(d){
  82. return colors(d.weight*d.weight*d.weight);
  83. })
  84. .call(force.drag); //可以拖动
  85. //(5)打点更新,没有的话就显示不出来了
  86. force.on( "tick" ,function(){
  87. //边
  88. edges.attr( "x1" ,function(d){
  89. return d.source.x;
  90. })
  91. .attr( "y1" ,function(d){
  92. return d.source.y;
  93. })
  94. .attr( "x2" ,function(d){
  95. return d.target.x;
  96. })
  97. .attr( "y2" ,function(d){
  98. return d.target.y;
  99. });
  100. //节点
  101. nodes.attr( "cx" ,function(d){
  102. return d.x;
  103. })
  104. .attr( "cy" ,function(d){
  105. return d.y;
  106. });
  107. })
  108. </script>
  109. </body>
  110. </html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值