Commit a788b5d2 by Your Name Committed by zhaochengxiang

add marker

parent 91498e8d
import React from 'react'; import React from 'react';
import G6 from '@antv/g6'; import G6 from '@antv/g6';
import { transform, mat3 } from '@antv/matrix-util';
import circle from './circle.93e1.png' import circle from './circle.93e1.png'
import node from './node-bg-select.171e.png' import node from './node-bg-select.171e.png'
...@@ -65,7 +64,7 @@ function countDepth(node, _depth = 0) { ...@@ -65,7 +64,7 @@ function countDepth(node, _depth = 0) {
function init(container, data) { function init(container, data) {
depthCount = {} depthCount = {}
countDepth(data) countDepth(data)
console.debug(depthCount) // console.debug(depthCount)
// const container = document.getElementById('container'); // const container = document.getElementById('container');
const width = container.scrollWidth; const width = container.scrollWidth;
const height = container.scrollHeight || 500; const height = container.scrollHeight || 500;
...@@ -177,8 +176,7 @@ G6.registerNode( ...@@ -177,8 +176,7 @@ G6.registerNode(
} }
if (depthCount[cfg.depth] > 10) { if (depthCount[cfg.depth] > 10) {
group.addShape('image', {
const image = group.addShape('image', {
attrs: { attrs: {
x: -width1 / 2, x: -width1 / 2,
y: -width1 / 2, y: -width1 / 2,
...@@ -189,7 +187,6 @@ G6.registerNode( ...@@ -189,7 +187,6 @@ G6.registerNode(
name: 'halo-shape', name: 'halo-shape',
visible: false, visible: false,
}); });
} }
if (depthCount[cfg.depth] > 10) { if (depthCount[cfg.depth] > 10) {
...@@ -305,10 +302,10 @@ G6.registerNode( ...@@ -305,10 +302,10 @@ G6.registerNode(
focus && focus.hide(); focus && focus.hide();
} }
// 将相关边也高亮 // 将相关边也高亮
const relatedEdges = item.getEdges(); // const relatedEdges = item.getEdges();
relatedEdges.forEach((edge) => { // relatedEdges.forEach((edge) => {
edge.setState('focus', value); // edge.setState('focus', value);
}); // });
} }
}, },
update: undefined, update: undefined,
...@@ -324,11 +321,12 @@ G6.registerEdge( ...@@ -324,11 +321,12 @@ G6.registerEdge(
const startPoint = cfg.startPoint; const startPoint = cfg.startPoint;
const endPoint = cfg.endPoint; const endPoint = cfg.endPoint;
const dx = endPoint.x - startPoint.x, dy = endPoint.y - startPoint.y, dst = Math.sqrt(dx*dx + dy*dy) const dx = endPoint.x - startPoint.x, dy = endPoint.y - startPoint.y, dst = Math.sqrt(dx * dx + dy * dy)
const px =dst > 30 ? 10 * dx / dst : 0, py = dst > 30 ? 10 * dy / dst : 0 const px = dst > 30 ? 10 * dx / dst : 0, py = dst > 30 ? 10 * dy / dst : 0
const shape = group.addShape('path', { const shape = group.addShape('path', { // 自定义连线
attrs: { attrs: {
stroke: '#aaa', stroke: '#aaa',
lineWidth: .5,
path: [ path: [
['M', startPoint.x + px, startPoint.y + py], ['M', startPoint.x + px, startPoint.y + py],
['L', endPoint.x - px, endPoint.y - py], ['L', endPoint.x - px, endPoint.y - py],
...@@ -339,45 +337,61 @@ G6.registerEdge( ...@@ -339,45 +337,61 @@ G6.registerEdge(
}); });
const midPoint = shape.getPoint(0.5); const midPoint = shape.getPoint(0.5);
const rad = Math.acos(dx/dst) const rad = Math.acos(dx / dst)
const radius = dy < 0 ? -rad : rad
const rw = 20, rh = 6
{ {
const rw = 20, rh = 6
const rect = group.addShape('rect', { // 文字的底框 const rect = group.addShape('rect', { // 文字的底框
attrs: { attrs: {
width: rw, width: rw,
height: rh, height: rh,
fill: '#fff', fill: '#fff',
radius: 2, radius: 2,
// x and y should be minus width / 2 and height / 2 respectively to translate the center of the rect to the midPoint // x and y should be minus width / 2 and height / 2 respectively to translate the center of the rect to the midPoint
// x 和 y 分别减去 width / 2 与 height / 2,使矩形中心在 midPoint 上 // x 和 y 分别减去 width / 2 与 height / 2,使矩形中心在 midPoint 上
}, },
}); });
const toMatrix = G6.Util.transform( const toMatrix = G6.Util.transform(
[1, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0, 0, 1],
[['t', -rw>>1, -rh>>1],['r', dy < 0 ? -rad : rad],['t',midPoint.x,midPoint.y]], [['t', -rw >> 1, -rh >> 1], ['r', dy < 0 ? -rad : rad], ['t', midPoint.x, midPoint.y]],
); );
rect.setMatrix(toMatrix) rect.setMatrix(toMatrix)
} }
{ {
const text = group.addShape('text', { // 文字 const text = group.addShape('text', { // 文字
attrs: { attrs: {
text: '关系', text: '关系',
textAlign: 'center', textAlign: 'center',
fontSize: 4, fontSize: 4,
fill: '#000', fill: '#000',
opacity: 0.85, opacity: 0.85,
} }
}) })
const bbox = text.getBBox() const bbox = text.getBBox()
text.moveTo(midPoint.x, midPoint.y + (bbox.height>>1)) text.moveTo(midPoint.x, midPoint.y + (bbox.height >> 1))
const radius = dy < 0 ? -rad : rad text.rotateAtPoint(midPoint.x, midPoint.y, rad > Math.PI * .5 ? radius + Math.PI : radius)
text.rotateAtPoint(midPoint.x, midPoint.y, rad > Math.PI * .5 ? radius + Math.PI : radius )
} }
{
const arrow = group.addShape('marker', {
attrs: {
x: 0,
y: 0,
r: 2,
fill: '#fff',
symbol: 'triangle',
},
// must be assigned in G6 3.3 and later versions. it can be any value you want
name: 'marker-shape',
});
const x = midPoint.x + ((rw >> 1) + 2.5) * dx / dst, y = midPoint.y + ((rw >> 1) + 2.5) * dy / dst
arrow.moveTo(x, y)
arrow.rotateAtPoint(x, y, radius + Math.PI * .5)
}
return group; return group;
}, },
afterDraw: (cfg, group) => { afterDraw: (cfg, group) => {
}, },
setState: (name, value, item) => { setState: (name, value, item) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment