pie-node.html 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="mountNode"></div>
  11. <script src="../build/g6.js"></script>
  12. <script>
  13. /**
  14. * 该实例演示如何使用G6自定义类似饼图的节点
  15. * by 十吾
  16. *
  17. */
  18. const lightBlue = 'rgb(119, 243, 252)';
  19. const lightOrange = 'rgb(230, 100, 64)';
  20. // 注册自定义名为 pie-node 的节点类型
  21. G6.registerNode('pie-node', {
  22. drawShape: (cfg, group) => {
  23. const radius = cfg.size / 2; // 节点半径
  24. const inPercentage = cfg.inDegree / cfg.degree; // 入度占总度数的比例
  25. const inAngle = inPercentage * Math.PI * 2; // 入度在饼图中的夹角大小
  26. const outAngle = Math.PI * 2 - inAngle; // 出度在饼图中的夹角大小
  27. const inArcEnd = [radius * Math.cos(inAngle), radius * Math.sin(inAngle)]; //入度饼图弧结束位置
  28. let isInBigArc = 1, isOutBigArc = 0;
  29. if (inAngle > Math.PI) {
  30. isInBigArc = 0;
  31. isOutBigArc = 1;
  32. }
  33. // 定义代表入度的扇形形状
  34. const fanIn = group.addShape('path', {
  35. attrs: {
  36. path: [
  37. [ 'M', radius, 0 ],
  38. [ 'A', radius, radius, 0, isInBigArc, 0, inArcEnd[0], inArcEnd[1] ],
  39. [ 'L', 0, 0 ],
  40. [ 'B' ]
  41. ],
  42. lineWidth: 0,
  43. fill: lightOrange
  44. }
  45. });
  46. // 定义代表出度的扇形形状
  47. const fanOut = group.addShape('path', {
  48. attrs: {
  49. path: [
  50. [ 'M', inArcEnd[0], inArcEnd[1] ],
  51. [ 'A', radius, radius, 0, isOutBigArc, 0, radius, 0 ],
  52. [ 'L', 0, 0 ],
  53. [ 'B' ]
  54. ],
  55. lineWidth: 0,
  56. fill: lightBlue
  57. }
  58. });
  59. // 返回 keyshape
  60. return fanIn;
  61. }
  62. },
  63. "single-shape"
  64. );
  65. const data = {
  66. nodes: [
  67. {
  68. size: 80,
  69. inDegree: 80,
  70. degree: 360,
  71. x: 50,
  72. y: 200
  73. },
  74. {
  75. size: 80,
  76. inDegree: 280,
  77. degree: 360,
  78. x: 200,
  79. y: 200
  80. }
  81. ]
  82. }
  83. const graph = new G6.Graph({
  84. container: 'mountNode',
  85. width: 800,
  86. height: 600,
  87. defaultNode: {
  88. shape: 'pie-node'
  89. }
  90. })
  91. graph.data(data)
  92. graph.render()
  93. </script>
  94. </body>
  95. </html>