How to use Graphin

The most basic role of Graphin is the React component. The following table is the interface description of <Graphin />.

Property nametypeDefault valueDescription
dataGraphinProps.data-Data Structure
layoutLayout{type:"grid"}Set Layout
themeThemeType{mode:"#light",...}Set theme
refReactRef-Graphin component Ref
widthnumber-Graphin canvas width, it is recommended to set the width through the parent element container, the default width is 100%
heightnumber-Graphin canvas height, it is recommended to set the width through the parent element container, the default minHeight is 500px
styleReact.CSSProperties{height: '100%',width: '100%',background: '#fff'}Graphin container stle
containerIdstringgraphin-containerGraphin container ID, It is used in multi-instance management scenarios
containerStyleReact.CSSProperties{height: '100%',width: '100%',position: 'relative'}Graphin container style
modesG6.Modes-Recommended use Behaviors
pluginsG6.Plugins-Recommended Components
defaultNodeDefault style configuration of node
defaultEdgeDefault edge style configuration
defaultComboCombo's default style configuration
nodeStateStylesNode style configuration in different states
edgeStateStylesEdge style configuration in different states
comboStateStylesCombo style configuration in different states
fitViewbooleanfalseWhether to enable canvas adaptation. The image automatically adapts to the canvas size after opening
fitCenterbooleanfalseAfter opening, the graph will be translated, the center of the graph will be aligned to the center of the canvas, but not zoomed. Priority is lower than fitView
fitViewPaddingnumber[][0,0]Take effect when fitView is true. When the figure adapts to the canvas, specify the space around it
minZoomnumber0.2Minimum zoom ratio
maxZoomnumber10Maximum zoom ratio
enabledStackbooleanfalseWhether to enable stack, that is, whether to enable redo & undo function
maxStepnumber10redo & undo maximum number of steps, only works when enabledStack is true

How to use the graph instance in Graphin

01. Get through Ref

import React from 'react';
import Graphin, { Utils } from '@antv/graphin';
const data = Utils.mock(10)
.circle()
.graphin();
const Demo = () => {
const graphinRef = React.createRef();
React.useEffect(() => {
const {
graph, // Graph instance of g6
apis, // API interface provided by Graphin
} = graphinRef.current;
console.log('ref', graphinRef, graph, apis);
}, []);
return (
<div className="App">
<Graphin data={data} ref={graphinRef}></Graphin>
</div>
);
};
export default Demo;

02. Get through GraphinContext

import React from 'react';
import Graphin, { Utils, GraphinContext } from '@antv/graphin';
const data = Utils.mock(10)
.circle()
.graphin();
const CustomComponents = () => {
// As long as the components wrapped in Graphin, you can obtain graph instances and apis provided by Graphin through Context
const { graph, apis } = React.useContext(GraphinContext);
return null;
};
const Demo = () => {
return (
<div className="App">
<Graphin data={data} ref={graphinRef}>
<CustomComponents />
</Graphin>
</div>
);
};
export default Demo;

the instance of G6 graph

Refer to the instance of G6 graph: https://g6.antv.vision/en/docs/api/graphFunc/get_set

The commonly used methods are as follows:

Graphin built-in interface: apis

In the future iterations of Graphin versions, commonly used interfaces will be aggregated on the upper layer of Graphin.

API

NameDescriptionTypeDefault
handleAutoZoom--() => { text: string; ratio: number; }(required)
handleRealZoom--() => { text: string; ratio: number; }(required)
handleChangeZoom--() => { text: string; ratio: number; }(required)
handleZoomIn--() => { text: string; ratio: number; }(required)
handleZoomOut--() => { text: string; ratio: number; }(required)
focusNodeById--(nodeId: string) => void(required)
highlightNodeById--(nodeId: string[]) => void(required)