customInherit.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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>自定义-继承</title>
  8. </head>
  9. <body>
  10. <div id="mountNode"></div>
  11. <script src="../build/g6.js"></script>
  12. <script>
  13. const data = {
  14. nodes: [{
  15. id: 'node1',
  16. x: 50,
  17. y: 50,
  18. shape: 'rect'
  19. }, {
  20. id: 'node2',
  21. x: 100,
  22. y: 200,
  23. shape: 'custom'
  24. }]
  25. };
  26. G6.registerNode('rect', {
  27. draw(item){
  28. const group = item.getGraphicGroup();
  29. this.drawText(item);
  30. return group.addShape('rect', {
  31. attrs: {
  32. x: 100,
  33. y: 100,
  34. width: 100,
  35. height: 100,
  36. stroke: 'red'
  37. }
  38. });
  39. },
  40. drawText(item) {
  41. const group = item.getGraphicGroup();
  42. const text = this.getText();
  43. group.addShape('text', {
  44. attrs: {
  45. x: 100,
  46. y: 100,
  47. fill: '#333',
  48. text
  49. }
  50. });
  51. },
  52. getText() {
  53. return '我是一个节点 rect';
  54. }
  55. });
  56. G6.registerNode('custom', {
  57. getText(){
  58. return '我是一个自定义节点,\n继承于 rect';
  59. }
  60. }, 'rect');
  61. const graph = new G6.Graph({
  62. container: 'mountNode', // 容器ID
  63. fitView: 'cc',
  64. height: window.innerHeight
  65. });
  66. graph.read(data);
  67. </script>
  68. </body>
  69. </html>