File size: 3,648 Bytes
6f0c45a 0e7a78d 4635160 8d4e686 4635160 6f0c45a 4635160 8d4e686 4635160 6f0c45a 4635160 0e7a78d 8d4e686 4635160 51db3f9 4635160 6f0c45a 4635160 6f0c45a 3f568cf 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 6f0c45a 4635160 0e7a78d 4635160 6f0c45a 8d4e686 51db3f9 8d4e686 6f0c45a 4635160 |
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 |
import { ElementDatum, Graph, IElementEvent } from '@antv/g6';
import isEmpty from 'lodash/isEmpty';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { buildNodesAndCombos } from './util';
import styles from './index.less';
const TooltipColorMap = {
combo: 'red',
node: 'black',
edge: 'blue',
};
interface IProps {
data: any;
show: boolean;
}
const ForceGraph = ({ data, show }: IProps) => {
const containerRef = useRef<HTMLDivElement>(null);
const graphRef = useRef<Graph | null>(null);
const nextData = useMemo(() => {
if (!isEmpty(data)) {
const graphData = data;
const mi = buildNodesAndCombos(graphData.nodes);
return { edges: graphData.edges, ...mi };
}
return { nodes: [], edges: [] };
}, [data]);
const render = useCallback(() => {
const graph = new Graph({
container: containerRef.current!,
autoFit: 'view',
autoResize: true,
behaviors: [
'drag-element',
'drag-canvas',
'zoom-canvas',
'collapse-expand',
{
type: 'hover-activate',
degree: 1, // 👈🏻 Activate relations.
},
],
plugins: [
{
type: 'tooltip',
enterable: true,
getContent: (e: IElementEvent, items: ElementDatum) => {
if (Array.isArray(items)) {
if (items.some((x) => x?.isCombo)) {
return `<p style="font-weight:600;color:red">${items?.[0]?.data?.label}</p>`;
}
let result = ``;
items.forEach((item) => {
result += `<section style="color:${TooltipColorMap[e['targetType'] as keyof typeof TooltipColorMap]};"><h3>${item?.id}</h3>`;
if (item?.entity_type) {
result += `<div style="padding-bottom: 6px;"><b>Entity type: </b>${item?.entity_type}</div>`;
}
if (item?.weight) {
result += `<div><b>Weight: </b>${item?.weight}</div>`;
}
if (item?.description) {
result += `<p>${item?.description}</p>`;
}
});
return result + '</section>';
}
return undefined;
},
},
],
layout: {
type: 'combo-combined',
preventOverlap: true,
comboPadding: 1,
spacing: 100,
},
node: {
style: {
size: 150,
labelText: (d) => d.id,
// labelPadding: 30,
labelFontSize: 40,
// labelOffsetX: 20,
labelOffsetY: 20,
labelPlacement: 'center',
labelWordWrap: true,
},
palette: {
type: 'group',
field: (d) => {
return d?.entity_type as string;
},
},
},
edge: {
style: (model) => {
const weight: number = Number(model?.weight) || 2;
const lineWeight = weight * 4;
return {
stroke: '#99ADD1',
lineWidth: lineWeight > 10 ? 10 : lineWeight,
};
},
},
});
if (graphRef.current) {
graphRef.current.destroy();
}
graphRef.current = graph;
graph.setData(nextData);
graph.render();
}, [nextData]);
useEffect(() => {
if (!isEmpty(data)) {
render();
}
}, [data, render]);
return (
<div
ref={containerRef}
className={styles.forceContainer}
style={{
width: '100%',
height: '100%',
display: show ? 'block' : 'none',
}}
/>
);
};
export default ForceGraph;
|