index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. arrange: {
  24. type: Array,
  25. observer: function (val) {
  26. this.changeData()
  27. }
  28. }
  29. },
  30. /**
  31. * 组件的初始数据
  32. */
  33. data: {
  34. canvasWidth: 375,
  35. canvasHeight: 600,
  36. pixelRatio: 1,
  37. forceMini: false,
  38. },
  39. onLoad() {
  40. const obj = {};
  41. if (this.properties.width) obj.canvasWidth = this.properties.width
  42. if (this.properties.height) obj.canvasHeight = this.properties.height;
  43. const { pixelRatio } = wx.getSystemInfoSync();
  44. if (pixelRatio) obj.pixelRatio = pixelRatio
  45. },
  46. /**
  47. * 组件的方法列表
  48. */
  49. methods: {
  50. init(event) {
  51. const { ctx, canvas, renderer } = event.detail;
  52. this.isCanvasInit = true;
  53. this.ctx = wrapContext(ctx);
  54. this.renderer = renderer;
  55. this.canvas = canvas;
  56. this.updateChart();
  57. },
  58. /**
  59. * canvas派发的事件,转派给graph实例
  60. */
  61. handleTouch(e) {
  62. this.graph && this.graph.emitEvent(e.detail);
  63. },
  64. updateChart() {
  65. const data = this.properties?.list;
  66. if (!data) return
  67. const { canvasWidth, canvasHeight, pixelRatio } = this.data;
  68. // 创建F6实例
  69. this.graph = new F6.TreeGraph({
  70. container: this.canvas,
  71. context: this.ctx,
  72. renderer: this.renderer,
  73. width: canvasWidth,
  74. height: canvasHeight,
  75. pixelRatio,
  76. fitView: true,
  77. linkCenter: true,
  78. modes: {
  79. default: [
  80. 'drag-canvas',
  81. 'zoom-canvas',
  82. {
  83. type: 'click-select',
  84. shouldUpdate: e => {
  85. return false;
  86. }
  87. }
  88. ],
  89. },
  90. defaultNode: {
  91. size: [150, 30],
  92. anchorPoints: [
  93. [0.5, 1],
  94. [0.5, 0]
  95. ],
  96. type: "rect",
  97. label: '冠军',
  98. labelCfg: {
  99. position: 'center',
  100. offset: 10,
  101. style: {
  102. fill: '#666',
  103. },
  104. },
  105. },
  106. defaultEdge: {
  107. type: 'polyline',
  108. endArrow: true
  109. },
  110. layout: {
  111. type: 'dendrogram',
  112. nodeSep: 50, // 节点之间间距
  113. direction: 'RL',
  114. },
  115. });
  116. this.graph.on('nodeselectchange', (e) => {
  117. if (!e.target) return;
  118. // 当前操作的 item
  119. const node = e.target?._cfg?.model;
  120. this.triggerEvent('tapNode', node)
  121. });
  122. this.graph.node((node) => {
  123. const r = this.properties.arrange.find(f => f.node_id === node.id);
  124. if (r) {
  125. const { label } = r
  126. node.label = r[label]
  127. } else {
  128. if (node.pos)
  129. node.label = `${node.pos[0] + 1}-${node.pos[1]}`
  130. }
  131. return node;
  132. })
  133. // 注册数据
  134. this.graph.data(data);
  135. this.graph.render();
  136. this.graph.fitView();
  137. },
  138. changeData() {
  139. if (!this.graph) return;
  140. const data = this.properties?.list;
  141. this.graph.data(data)
  142. this.graph.render();
  143. this.graph.fitView();
  144. },
  145. },
  146. onUnload() {
  147. this.graph && this.graph.destroy();
  148. },
  149. })