index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // commpents/f6tree/index.js
  2. import F6 from '@antv/f6-wx';
  3. import TreeGraph from '@antv/f6-wx/extends/graph/treeGraph'
  4. import { wrapContext } from '../../utils/f6/context';
  5. F6.registerGraph('TreeGraph', TreeGraph);
  6. Component({
  7. canvas: null,
  8. ctx: null, // 延迟获取的2d context
  9. renderer: '', // mini、mini-native等,F6需要,标记环境
  10. isCanvasInit: false, // canvas是否准备好了
  11. graph: null,
  12. /**
  13. * 组件的属性列表
  14. */
  15. properties: {
  16. width: { type: Number },
  17. height: { type: Number },
  18. list: {
  19. type: Object, observer: function (val) {
  20. this.changeData()
  21. }
  22. }
  23. },
  24. /**
  25. * 组件的初始数据
  26. */
  27. data: {
  28. canvasWidth: 375,
  29. canvasHeight: 600,
  30. pixelRatio: 1,
  31. forceMini: false,
  32. },
  33. onLoad() {
  34. const obj = {};
  35. if (this.properties.width) obj.canvasWidth = this.properties.width
  36. if (this.properties.height) obj.canvasHeight = this.properties.height;
  37. const { pixelRatio } = wx.getSystemInfoSync();
  38. if (pixelRatio) obj.pixelRatio = pixelRatio
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. init(event) {
  45. const { ctx, canvas, renderer } = event.detail;
  46. this.isCanvasInit = true;
  47. this.ctx = wrapContext(ctx);
  48. this.renderer = renderer;
  49. this.canvas = canvas;
  50. this.updateChart();
  51. },
  52. /**
  53. * canvas派发的事件,转派给graph实例
  54. */
  55. handleTouch(e) {
  56. this.graph && this.graph.emitEvent(e.detail);
  57. },
  58. updateChart() {
  59. const data = this.properties?.list;
  60. const { canvasWidth, canvasHeight, pixelRatio } = this.data;
  61. // 创建F6实例
  62. this.graph = new F6.TreeGraph({
  63. container: this.canvas,
  64. context: this.ctx,
  65. renderer: this.renderer,
  66. width: canvasWidth,
  67. height: canvasHeight,
  68. pixelRatio,
  69. fitView: true,
  70. linkCenter: true,
  71. modes: {
  72. default: [
  73. 'drag-canvas',
  74. 'zoom-canvas',
  75. {
  76. type: 'click-select',
  77. shouldUpdate: e => {
  78. return false;
  79. }
  80. }
  81. ],
  82. },
  83. defaultNode: {
  84. size: [150, 30],
  85. anchorPoints: [
  86. [0.5, 1],
  87. [0.5, 0]
  88. ],
  89. type: "rect",
  90. label: '冠军',
  91. labelCfg: {
  92. position: 'center',
  93. offset: 10,
  94. style: {
  95. fill: '#666',
  96. },
  97. },
  98. },
  99. defaultEdge: {
  100. type: 'polyline',
  101. endArrow: true
  102. },
  103. layout: {
  104. type: 'dendrogram',
  105. nodeSep: 50, // 节点之间间距
  106. direction: 'RL',
  107. },
  108. });
  109. this.graph.on('nodeselectchange', (e) => {
  110. if (!e.target) return;
  111. // 当前操作的 item
  112. const node = e.target?._cfg?.model;
  113. this.triggerEvent('tapNode', node)
  114. });
  115. // 注册数据
  116. this.graph.data(data);
  117. this.graph.render();
  118. this.graph.fitView();
  119. },
  120. changeData() {
  121. if (!this.graph) return;
  122. const data = this.properties?.list;
  123. this.graph.data(data)
  124. this.graph.render();
  125. this.graph.fitView();
  126. }
  127. },
  128. onUnload() {
  129. this.graph && this.graph.destroy();
  130. },
  131. })