zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<template>
  <div class="container" id="container"></div>
</template>
 
<script setup>
 
import {getChineseLen} from '@/utils/tools.js'
 
// import G6 from '@antv/g6';
import {useEntityStore} from '@/store/modules';
 
const loading = ref(false);
 
const entityStore = useEntityStore();
 
const getChildrenIndex = (d) => {
  let index = 0
  if (!d.children) {
    index++;
  } else {
    d.children.forEach(e => {
      index += getChildrenIndex(e);
    });
  }
  return index;
};
 
const getTextWidth = ({desc, name}) => {
  const numEnum = {
    chinese: 16,
    english: 8,
    other: 6
  };
  const w1 = getChineseLen(desc, numEnum);
  const w2 = getChineseLen(name, numEnum);
  const w3 = w1 > w2 ? w1 : w2;
  return w3 < 100 ? 100 : w3;
}
 
const getParentWidth = ({data, parent}) => {
  let left = 0;
  if (parent) {
    left = getTextWidth(parent.data);
    left = left + getParentWidth(parent) + 40;
  }
  return left;
};
 
 
const registerNode = (data) => {
 
  G6.registerNode('card-node', {
    draw: function drawShape(cfg, group) {
      const r = 2;
      const color = '#5B8FF9';
      const w = getTextWidth(cfg);
      const h = cfg.size[1];
      const shape = group.addShape('rect', {
        attrs: {
          x: 20,
          y: -h / 2,
          width: w, //200,
          height: h, // 60
          stroke: color,
          radius: r,
          fill: '#fff',
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: 'main-box',
        draggable: true,
      });
 
      group.addShape('rect', {
        attrs: {
          x: 20,
          y: -h / 2,
          width: w, //200,
          height: h / 2, // 60
          fill: color,
          radius: [r, r, 0, 0],
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: 'title-box',
        draggable: true,
      });
 
      // title text
      group.addShape('text', {
        attrs: {
          textBaseline: 'top',
          x: 30,
          y: -h / 2 + 2,
          lineHeight: 20,
          text: cfg.name,
          fill: '#fff',
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: 'title',
      });
      cfg.children &&
      group.addShape('marker', {
        attrs: {
          x: 20 + w,
          y: 0,
          r: 6,
          cursor: 'pointer',
          symbol: cfg.collapsed ? G6.Marker.expand : G6.Marker.collapse,
          stroke: '#666',
          lineWidth: 1,
          fill: '#fff',
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: 'collapse-icon',
      });
 
      group.addShape('text', {
        attrs: {
          textBaseline: 'top',
          x: 30,
          y: -h / 2 + 24,
          lineHeight: 20,
          text: cfg.desc,
          fill: 'rgba(0,0,0, 1)',
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: `description`,
      });
 
      return shape;
    },
    setState(name, value, item) {
      if (name === 'collapsed') {
        const marker = item.get('group').find((ele) => ele.get('name') === 'collapse-icon');
        const icon = value ? G6.Marker.expand : G6.Marker.collapse;
        marker.attr('symbol', icon);
      }
    },
  });
 
  const container = document.getElementById('container');
  const width = 1000;
  const height = getChildrenIndex(data) * 65;
 
  const graph = new G6.TreeGraph({
    container: 'container',
    width,
    height,
    modes: {
      default: ['drag-canvas'],
    },
    defaultNode: {
      type: 'card-node',
      size: [100, 40],
    },
    defaultEdge: {
      type: 'cubic-horizontal',
      style: {
        endArrow: true,
      },
    },
    layout: {
      type: 'indented',
      direction: 'LR',
      dropCap: false,
      indent: (n) => {
        return getParentWidth(n);
      },
      getHeight: () => {
        return 30;
      },
    },
  });
 
  graph.data(data);
  graph.render();
  graph.fitView();
  graph.on('node:click', (e) => {
    if (e.target.get('name') === 'collapse-icon') {
      e.item.getModel().collapsed = !e.item.getModel().collapsed;
      graph.setItemState(e.item, 'collapsed', e.item.getModel().collapsed);
      graph.layout();
    }
  });
 
  if (typeof window !== 'undefined')
    window.onresize = () => {
      if (!graph || graph.get('destroyed')) return;
      if (!container || !container.scrollWidth || !container.scrollHeight) return;
      graph.changeSize(container.scrollWidth, container.scrollHeight);
    };
}
 
defineExpose({
  registerNode
});
 
</script>
 
 
<style lang="scss" scoped>
.container {
  width: 100%;
  min-height: 300px;
}
</style>