g6/stories/Shape/component/default-shape.tsx

188 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-02-14 10:10:54 +08:00
import React, { useEffect } from 'react';
import G6 from '../../../src';
import { IGraph } from '../../../src/interface/graph';
2020-03-31 10:34:43 +08:00
import { NodeConfig, EdgeConfig } from '../../../src/types';
2020-02-14 10:10:54 +08:00
let graph: IGraph = null;
2020-02-06 16:20:14 +08:00
2020-02-14 10:10:54 +08:00
G6.registerNode(
'file-node',
{
2020-03-31 10:34:43 +08:00
draw(cfg: NodeConfig, group) {
2020-02-14 10:10:54 +08:00
const keyShape = group.addShape('rect', {
2020-02-06 16:20:14 +08:00
attrs: {
2020-02-14 10:10:54 +08:00
x: cfg.x - 4,
y: cfg.y - 14,
fill: '#fff',
stroke: '#666',
},
className: 'node-rect',
2020-02-06 16:20:14 +08:00
});
2020-02-14 10:10:54 +08:00
if (cfg.children && cfg.children.length > 0) {
group.addShape('marker', {
attrs: {
symbol: 'triangle-down',
x: cfg.x + 4,
y: cfg.y - 2,
r: 4,
fill: '#666',
},
});
2020-01-13 21:05:56 +08:00
}
2020-02-14 10:10:54 +08:00
const shape = group.addShape('text', {
attrs: {
x: cfg.x + 15,
y: cfg.y + 4,
text: cfg.label,
fill: '#666',
fontSize: 16,
textAlign: 'left',
},
});
2020-02-06 16:20:14 +08:00
2020-02-14 10:10:54 +08:00
const bbox = shape.getBBox();
group.addShape('text', {
attrs: {
x: cfg.x + bbox.width + 20,
y: cfg.y + 40,
text: '详情',
fill: '#651466',
fontSize: 16,
textAlign: 'right',
},
className: 'detail-text',
});
2020-02-06 16:20:14 +08:00
2020-02-14 10:10:54 +08:00
keyShape.attr({
width: bbox.width + 40,
height: bbox.height + 45,
});
return keyShape;
},
},
'single-node',
);
G6.registerEdge(
'step-line',
{
2020-03-31 10:34:43 +08:00
getControlPoints: function getControlPoints(cfg: EdgeConfig) {
2020-02-14 10:10:54 +08:00
const startPoint = cfg.startPoint;
const endPoint = cfg.endPoint;
return [
{
x: startPoint.x,
y: endPoint.y,
},
];
},
},
'polyline',
);
2020-01-13 21:05:56 +08:00
const DefaultShape = () => {
2020-02-14 10:10:54 +08:00
const container = React.useRef();
2020-01-13 21:05:56 +08:00
useEffect(() => {
2020-02-14 10:10:54 +08:00
if (!graph) {
2020-02-06 16:20:14 +08:00
graph = new G6.TreeGraph({
2019-12-31 12:56:28 +08:00
container: container.current as string | HTMLElement,
width: 500,
2020-01-13 21:05:56 +08:00
height: 500,
2020-02-06 16:20:14 +08:00
linkCenter: true,
modes: {
2020-02-14 10:10:54 +08:00
default: ['collapse-expand', 'zoom-canvas', 'drag-canvas'],
2020-02-06 16:20:14 +08:00
},
defaultNode: {
2020-02-14 10:10:54 +08:00
shape: 'file-node',
2020-02-06 16:20:14 +08:00
},
2020-01-13 21:05:56 +08:00
defaultEdge: {
2020-02-14 10:10:54 +08:00
shape: 'step-line',
2020-01-13 21:05:56 +08:00
},
2020-02-06 16:20:14 +08:00
nodeStateStyles: {
hover: {
fill: '#83AFFD',
stroke: '#5B8FF9',
},
select: {
fill: '#4ab334',
lineWidth: 2,
2020-02-14 10:10:54 +08:00
stroke: '#5B8FF9',
},
2020-02-06 16:20:14 +08:00
},
layout: {
type: 'indented',
isHorizontal: true,
direction: 'LR',
indent: 70,
getHeight: function getHeight() {
return 20;
},
2020-02-14 10:10:54 +08:00
2020-02-06 16:20:14 +08:00
getWidth: function getWidth() {
return 20;
2020-02-14 10:10:54 +08:00
},
},
2020-02-06 16:20:14 +08:00
});
}
const data = {
2020-02-06 16:20:14 +08:00
id: '1',
label: '域服务-创建计划',
2020-02-14 10:10:54 +08:00
children: [
{
id: '1-3',
label: '计划信息初始化',
children: [],
},
{
id: '1-2',
label: '计划信息校验',
children: [],
},
{
id: '1-1',
label: '计划信息保存',
children: [
{
id: '1-1-1',
label: '基本信息初始化',
},
{
id: '1-1-2',
label: '投放时间信息初始化',
},
],
},
],
2020-01-13 21:05:56 +08:00
};
2020-02-14 10:10:54 +08:00
graph.data(data);
graph.render();
2020-02-06 16:20:14 +08:00
graph.on('node:click', ev => {
//const clickNodes = graph.findAllByState('node', 'click');
2020-02-14 10:10:54 +08:00
2020-02-06 16:20:14 +08:00
//clickNodes.forEach(cn => {
2020-02-14 10:10:54 +08:00
//graph.setItemState(cn, 'click', false);
// });
2020-02-06 16:20:14 +08:00
const nodeItem = ev.item;
graph.setItemState(nodeItem, 'select', true);
});
graph.on('node:mouseenter', evt => {
2020-02-14 10:10:54 +08:00
const { item } = evt;
graph.setItemState(item, 'hover', true);
});
});
2020-01-13 21:05:56 +08:00
2020-02-14 10:10:54 +08:00
return <div ref={container}></div>;
};
2020-02-14 10:10:54 +08:00
export default DefaultShape;