g6/docs/api/Graph.en.md

1325 lines
29 KiB
Markdown
Raw Normal View History

2019-11-20 10:47:11 +08:00
---
title: Graph
order: 1
2019-12-02 19:58:02 +08:00
redirect_from:
- /en/docs/api
2019-11-20 10:47:11 +08:00
---
2019-12-02 19:58:02 +08:00
Graph is the carrier of G6. All the operations about events, behaviors, items are mounted on the instance of Graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
The life cycle of an instance of Graph is: Initialize -> Load data -> Render -> Update -> Destroy.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
## Initialize
2019-11-20 10:47:11 +08:00
### G6.Graph
2019-12-02 19:58:02 +08:00
**Configurations**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Default | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| container | String|HTMLElement | The DOM container of graph, it can be the id of a DOM element or the an HTML node. |
| width | Number | undefined | The width of the canvas for graph with the unit 'px'. |
| height | Number | undefined | The height of the canvas for graph with the unit 'px'. |
| renderer | String | canvas | The engine for rendering. Options: 'canvas' or 'svg'. |
| fitView | Boolean | false | Whether fit the canvas to the view port. |
| fitViewPadding | Array | Number | Takes effect only when `fitView: true`. It is the padding between canvas and the border of view port.<br />- It can be a value, e.g. `fitViewPadding: 20`, which means the padding to the top, left, right, bottom are the same.<br />- Or an array, e.g. `fitViewPadding: [ 20, 40, 50, 20 ]`, the four values in the array indicate the padding to the top, right, bottom, left respectively. |
| groupByTypes | Boolean | true | Whether group the nodes and edges separately. When it is false, all the items (including nodes and edges) are in the same group, and the order/zindex of them are determined according to the order of their generation. |
| autoPaint | Boolean | true | Whether paint the graph automatically while item updated or view port changed. In order to enhance the performance, we recommend to turn off `antoPaint` when you are doing bulk operation on nodes or edges. This can be refered to [`setAutoPaint()`](#setautopaintauto). |
| modes | Object | | The interaction modes of this graph. Please refer to [Interaction Mode](/en/docs/manual/middle/states/mode) for detail。 |
| nodeStateStyles | Object | {} | The node styles on different states, e.g. hover, selected. It is a new feature of G6 3.1. |
| edgeStateStyles | Object | {} | The edge styles on different states, e.g. hover, selected. It is a new feature of G6 3.1. |
| defaultNode | Object | {} | Default node configurations in global, including shape, size, color and so on. Its priority is lower than the configurations in data. |
| defaultEdge | Object | {} | Default edge configurations in global, including shape, size, color and so on. Its priority is lower than the configurations in data. |
| plugins | Array | [] | Plugins for graph. Please refer to [plugin](/en/docs/manual/tutorial/plugins#插件) for detail.|
| animate | Boolean | false | Wheter activate the global animation. Which will take effect while changing layouts, changing data, and other global operations. |
| animateCfg | Object | | The configurations for global animation. Takes effect only when `animate: true`. |
| animateCfg.<br />onFrame | Function | null | The callback function for every frame of animation. The path of custom animation for node can be defined here. The nodes will move linearly when `onFrame` is null. |
| animateCfg.<br />duration | Number | 500 | Duration of animation with unit millisecond. |
| animateCfg.<br />easing | String | easeLinear | The easing function name of animation. Please refer to ease in d3. |
| minZoom | Number | 0.2 | The minimum zoom ratio. |
| maxZoom | Number | 10 | The maximum zoom ratio. |
| pixelRatio | Number | 1.0 | Pixel ratio. |
| groupType | String | circle | Group type for nodes. Options: 'circle' or 'rect' |
| groupStyle | Object | | Group style for nodes, please refer to [Node Group](/en/docs/manual/middle/nodeGroup) for detail. |
| layout | Object | | Configurations for layout. The `type` in it is the name of layout method with the options: 'random', 'radial', 'mds', 'circular', 'fruchterman', 'force', 'dagre', 'concentric', 'grid'. For more configurations for different layout methods, please refer to  [Layout API](/en/docs/api/layout/Layout) |
⚠️**Attention:** In G6 3.1, we added two new configurations for graph: `nodeStateStyles` and `edgeStateStyles`. In the same time, we deleted `nodeStyle` and `edgeStyle` . To upgrate, replace `nodeStyle` with `nodeStateStyles`, and replace `edgeStyle` with `edgeStateStyles`, and keep the sub-configuration inside them.
**Usage**
Place the configurations in the paramter as below to instantiate a graph:
2019-11-20 10:47:11 +08:00
```javascript
const graph = new G6.Graph({
container: '',
width: 500,
height: 500,
modes: {
default: ['drag-canvas']
},
layout: {
type: 'radial',
unitRadius: 50,
center: [500, 300]
}
})
```
2019-12-02 19:58:02 +08:00
## Load Data
2019-11-20 10:47:11 +08:00
### data(data)
2019-12-02 19:58:02 +08:00
Load the data for graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| data | Object | true | Graph data, it should be an object containing an array of nodes and an array of edges. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const data = {
nodes: [
{
id: 'node1',
label: 'node1'
},
{
id: 'node2',
label: 'node2'
}
],
edges: [
{
source: 'node1',
target: 'node2'
}
]
}
2019-12-02 19:58:02 +08:00
// graph is an instance of Graph
2019-11-20 10:47:11 +08:00
graph.data(data)
```
2019-12-02 19:58:02 +08:00
## Render
2019-11-20 10:47:11 +08:00
### render()
2019-12-02 19:58:02 +08:00
Render the graph with data onto the canvas.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.render()
```
### renderCustomGroup(data, groupType)
2019-12-02 19:58:02 +08:00
Render a node group according to the data.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| data | Object | true | The data to be rendered |
| groupType | string | true | Type of node group. Options: 'circle' or 'rect' |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const data = {
nodes: [
{
id: "node1",
groupId: "group1",
label: "node1"
},
{
id: "node2",
groupId: "group1",
label: "node2"
}
],
edges: [
{
source: "node1",
target: "node2"
}
],
groups: [
{
id: "group1",
title: {
2019-12-02 19:58:02 +08:00
text: "My Group 1",
2019-11-20 10:47:11 +08:00
stroke: "#444",
offsetX: -20,
offsetY: 30
}
}
]
};
2019-12-02 19:58:02 +08:00
// graph is an instance of Graph
2019-11-20 10:47:11 +08:00
graph.renderCustomGroup(data, "circle");
```
2019-12-02 19:58:02 +08:00
2019-11-20 10:47:11 +08:00
### read(data)
2019-12-02 19:58:02 +08:00
Read the data and render the graph. It is equal to combining graph.data(data) and graph.render().
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| data | Object | true | Graph data, it should be an object containing an array of nodes and an array of edges. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const data = {
nodes: [
{
id: 'node1',
label: 'node1'
},
{
id: 'node2',
label: 'node2'
}
],
edges: [
{
source: 'node1',
target: 'node2'
}
]
}
2019-12-02 19:58:02 +08:00
// graph is an instance of Graph
2019-11-20 10:47:11 +08:00
graph.read(data)
```
### changeData(data)
2019-12-02 19:58:02 +08:00
Change the data source, and render the graph according to the new data.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| data | Object | true | Graph data, it should be an object containing an array of nodes and an array of edges. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const data = {
nodes: [
{
id: 'node1',
label: 'node1'
},
{
id: 'node2',
label: 'node2'
}
],
edges: [
{
source: 'node1',
target: 'node2'
}
]
}
2019-12-02 19:58:02 +08:00
// graph is an instance of Graph
2019-11-20 10:47:11 +08:00
graph.changeData(data)
```
### collapseGroup(groupId)
2019-12-02 19:58:02 +08:00
Collapse the group with groupId. After collapsing, the nodes and edges inside the group will be hided, the edges linking inside nodes and outside nodes will be linked to the group.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| groupId | String | true | The id of the group. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.collapseGroup('groupId')
```
### expandGroup(groupId)
2019-12-02 19:58:02 +08:00
Expand the group to show the inside nodes and edges, and the edges linking inside nodes and outside nodes will be restored.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| groupId | String | true | The id of the group. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.expandGroup('groupId')
```
2019-12-02 19:58:02 +08:00
## Update
2019-11-20 10:47:11 +08:00
### addItem(type, model)
2019-12-02 19:58:02 +08:00
Add item(node, edge, or group) to the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| type | String | true | The type of the item. Options: 'node', 'edge', and 'group'. |
| model | Object | true | The data model of the item. When type = 'group', refer to [Create Node Group](/en/docs/manual/advanced/create-node-group) |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const model = {
id: 'node',
label: 'node',
address: 'cq',
x: 200,
y: 150,
style: {
fill: 'blue'
}
}
graph.addItem('node', model)
2019-12-02 19:58:02 +08:00
// Here is the model example for type = 'group'
2019-11-20 10:47:11 +08:00
const model = {
groupId: 'xxx000999',
nodes: ['123', '23'],
type: 'rect',
zIndex: 0,
title: {
2019-12-02 19:58:02 +08:00
text: 'group name'
2019-11-20 10:47:11 +08:00
}
}
graph.addItem('group', model)
```
### add(type, model)
2019-12-02 19:58:02 +08:00
The same as addItem(type, model).
2019-11-20 10:47:11 +08:00
### updateItem(item, model)
2019-12-02 19:58:02 +08:00
Update the item with new data model.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
| cfg | Object | false | New data model. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const model = {
id: 'node',
label: 'node',
address: 'cq',
x: 200,
y: 150,
style: {
fill: 'blue'
}
}
2019-12-02 19:58:02 +08:00
// Find the item instance by id
2019-11-20 10:47:11 +08:00
const item = graph.findById('node')
graph.updateItem(item, model)
```
### update(item, model)
2019-12-02 19:58:02 +08:00
The same as updateItem(item, model).
2019-11-20 10:47:11 +08:00
### removeItem(item)
2019-12-02 19:58:02 +08:00
Remove the item. When the item is the id of a group, this operation will delete the corresponding group.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Find the item instance by id
2019-11-20 10:47:11 +08:00
const item = graph.findById('node')
graph.removeItem(item)
```
### remove(item)
2019-12-02 19:58:02 +08:00
The same as removeItem(item)。
2019-11-20 10:47:11 +08:00
### refresh()
2019-12-02 19:58:02 +08:00
Refresh the canvas when the data is changed.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.refresh()
```
### refreshItem(item)
2019-12-02 19:58:02 +08:00
Refresh the item.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Find the item instance by id
2019-11-20 10:47:11 +08:00
const item = graph.findById('node')
graph.refreshItem(item)
```
### refreshPositions()
2019-12-02 19:58:02 +08:00
When the positions of nodes in their data models are changed, refresh the canvas to paint the nodes with new positions. It will update the edges in the same time.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.refreshPositions()
```
### paint()
2019-12-02 19:58:02 +08:00
Repaint the canvas. Use it after changing the item's style or state.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const item = e.item;
const graph = this.graph;
const autoPaint = graph.get('autoPaint');
graph.setAutoPaint(false);
graph.setItemState(item, 'selected', true)
graph.paint();
graph.setAutoPaint(autoPaint);
```
### setAutoPaint(auto)
2019-12-02 19:58:02 +08:00
Whether repaint the canvas automatically after updating or deleting items.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| auto | Boolean | true | Whether repaint the canvas automatically. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const item = e.item;
const graph = this.graph;
const autoPaint = graph.get('autoPaint');
graph.setAutoPaint(false);
graph.setItemState(item, 'selected', true)
graph.paint();
graph.setAutoPaint(autoPaint);
```
2019-12-02 19:58:02 +08:00
## Layout
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
There are several basic layout algorithms in G6 3.1. For more information, please refer to [Layout API](/en/docs/api/layout/Layout)。
2019-11-20 10:47:11 +08:00
### layout()
2019-12-02 19:58:02 +08:00
Re-layout the graph with current layout configurations in graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const graph = new G6.Graph({
container: 'mountNode',
width: 1000,
height: 600,
layout: {
type: 'force'
},
modes: {
default: ['drag-node']
}
});
graph.data({
nodes: data.nodes,
edges: data.edges.map((edge, i) => {
edge.id = 'edge' + i;
return Object.assign({}, edge);
})
});
graph.render();
function refreshDragedNodePosition(e) {
const model = e.item.get('model');
model.fx = e.x;
model.fy = e.y;
}
graph.on('node:dragstart', e => {
2019-12-02 19:58:02 +08:00
// Relayout when dragging the node
2019-11-20 10:47:11 +08:00
graph.layout();
refreshDragedNodePosition(e);
});
graph.on('node:drag', e => {
refreshDragedNodePosition(e);
});
graph.on('node:dragend', e => {
e.item.get('model').fx = null;
e.item.get('model').fy = null;
});
```
### updateLayout(cfg)
2019-12-02 19:58:02 +08:00
Update the layout configurations.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
1. If there is `type` in `cfg`, `type` is a String and it is different from current layout method, `updateLayout(cfg)` will change the layout method and relayout;
1. If there is no `type` in `cfg`, `updateLayout(cfg)` will relayout with current layout method and new layout configurations.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| cfg | Object | true | Configurations of new layout. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const graph = new G6.Graph({
container: 'mountNode',
width: 1000,
height: 600,
modes: {
default: ['drag-canvas', 'drag-node'],
},
layout: {
type: 'circular',
center: [500, 300]
},
animate: true
});
graph.data(data);
graph.render();
2019-12-02 19:58:02 +08:00
// configure the layout while instantializing the graph, and update the layout in somewhere you want.
2019-11-20 10:47:11 +08:00
graph.updateLayout({
radius: 200,
startAngle: Math.PI / 4,
endAngle: Math.PI,
divisions: 5,
ordering: 'degree'
});
```
2019-12-02 19:58:02 +08:00
## Destroy
2019-11-20 10:47:11 +08:00
### clear()
2019-12-02 19:58:02 +08:00
Clear all the items in the canvas. This function is used for reseting the data source and re-rendering the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.clear()
```
### destroy()
2019-12-02 19:58:02 +08:00
Destroy the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.destroy()
```
2019-12-02 19:58:02 +08:00
## State
2019-11-20 10:47:11 +08:00
### showItem(item)
2019-12-02 19:58:02 +08:00
Show the item.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Find the item instance by id
2019-11-20 10:47:11 +08:00
const item = graph.findById('nodeId')
graph.showItem(item)
2019-12-02 19:58:02 +08:00
// equal to
2019-11-20 10:47:11 +08:00
graph.showItem('nodeId')
```
### hideItem(item)
2019-12-02 19:58:02 +08:00
Hide the item.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Find the item instance by id
2019-11-20 10:47:11 +08:00
const item = graph.findById('nodeId')
graph.hideItem(item)
2019-12-02 19:58:02 +08:00
// Equal to
2019-11-20 10:47:11 +08:00
graph.hideItem('nodeId')
```
### setItemState(item, state, enabled)
2019-12-02 19:58:02 +08:00
Set the item's state.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
This function will emit events `beforitemstatechange` and `afteritemstatechange`.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
| state | String | true | The value of state. State can be comstomized as selected, hover, actived, and so on. |
| enabled | Boolean | true | Whether activate the state. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.setItemState('node1', 'selected', true);
```
### clearItemStates(item, states)
2019-12-02 19:58:02 +08:00
Clear the states of the item. This function could clear multiple states in the same time.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
| states | String / Array | null  | false | It can be a single state value, an array, or null. When it is null, this operation will clear the **first** state of the item. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Clear single state 'a' of the node
2019-11-20 10:47:11 +08:00
graph.clearItemStates(node, 'a');
2019-12-02 19:58:02 +08:00
// Clear multiple states of the node
2019-11-20 10:47:11 +08:00
graph.clearItemStates(node, ['a', 'b']);
2019-12-02 19:58:02 +08:00
// Clear all the states of the node
2019-11-20 10:47:11 +08:00
graph.clearItemStates(node);
```
### node(nodeFn)
2019-12-02 19:58:02 +08:00
Set the style and other configurations for each node.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
⚠️**Attention:** this funcion must **be called before graph.render()**. It does not take effect otherwise.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| nodeFn | Function | true | Return the configurations for each node. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.node((node) => {
return {
id: node.id,
shape: 'rect',
style: {
fill: 'blue'
}
}
})
graph.data(data)
graph.render()
```
### edge(edgeFn)
2019-12-02 19:58:02 +08:00
Set the style and other configurations for each edge.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
⚠️**Attention:** this funcion must **be called before graph.render()**. It does not take effect otherwise.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| edgeFn | Function | true | Return the configurations for each edge. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.edge((edge) => {
return {
id: edge.id,
shape: 'cubic-horizontal',
style: {
stroke: 'green'
}
}
})
graph.data(data)
graph.render()
```
2019-12-02 19:58:02 +08:00
## Interaction
2019-11-20 10:47:11 +08:00
### addBehaviors(behaviors, modes)
2019-12-02 19:58:02 +08:00
Add interaction behaviors to a mode or multiple modes.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| behaviors | String / Array | true | The name(s) of behavior(s) to be added. |
| modes | String / Array | true | The name(s) of mode(s) |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Add single behavior 'click-select' to a single mode 'default'
2019-11-20 10:47:11 +08:00
graph.addBehaviors('click-select', 'default');
2019-12-02 19:58:02 +08:00
// Add multiple behaviors to single mode 'default'
2019-11-20 10:47:11 +08:00
graph.addBehaviors([ 'brush-select', 'click-select' ], 'default');
2019-12-02 19:58:02 +08:00
// Add single behavior 'brush-select' to multiple modes
2019-11-20 10:47:11 +08:00
graph.addBehaviors('brush-select', ['default', 'select']);
2019-12-02 19:58:02 +08:00
// Add multiple behaviors to multiple modes
2019-11-20 10:47:11 +08:00
graph.addBehaviors([ 'brush-select', 'click-select' ], ['default', 'select']);
```
### removeBehaviors(behaviors, modes)
2019-12-02 19:58:02 +08:00
Remove behavior(s) from mode(s).
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| behaviors | String / Array | true | The name(s) of behavior(s) to be removed. |
| modes | String / Array | true | The name(s) of mode(s). |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// remove single behavior 'click-select' from single mode 'default'
2019-11-20 10:47:11 +08:00
graph.removeBehaviors('click-select', 'default');
2019-12-02 19:58:02 +08:00
// remove multiple behaviors from single mode 'default'
2019-11-20 10:47:11 +08:00
graph.removeBehaviors([ 'brush-select', 'click-select' ], 'default');
2019-12-02 19:58:02 +08:00
// remove single behavior 'brush-select' from multiple modes
2019-11-20 10:47:11 +08:00
graph.removeBehaviors('brush-select', ['default', 'select']);
2019-12-02 19:58:02 +08:00
// remove multiple behaviors from multiple modes
2019-11-20 10:47:11 +08:00
graph.removeBehaviors([ 'brush-select', 'click-select' ], ['default', 'select']);
```
### setMode(mode)
2019-12-02 19:58:02 +08:00
Switch the interaction mode of graph. For example, switch from edit mode to read-only mode.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| mode | String | true | The name of the mode. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const graph = new G6.Graph({
container: div,
width: 500,
height: 500,
pixelRatio: 2,
modes: {
default: [...],
custom: [...]
}
})
graph.setMode('custom')
```
### getCurrentMode()
2019-12-02 19:58:02 +08:00
Get the current mode.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of return value: String;
- The return value indicates the current mode.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// The return value is the current interaction mode
2019-11-20 10:47:11 +08:00
const mode = graph.getCurrentMode()
```
### getZoom()
2019-12-02 19:58:02 +08:00
Get the current zoom ratio.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of return value: Number;
- The return value indicates the current zoom ratio of view port. The default value is 1.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// The return value indicates the current zoom ratio
2019-11-20 10:47:11 +08:00
const zoom = graph.getZoom()
```
### zoom(ratio, center)
2019-12-02 19:58:02 +08:00
Change the zoom ratio. The parameter ratio is the related ratio about the current canvas.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| ratio | Number | true | Zoom ratio.|
| center | Object | false | Zoom at the center with x and y. If the center is ignored, this operation will zoom the graph with the current graph center. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Zoom at center (100, 100) with ratio 3
2019-11-20 10:47:11 +08:00
graph.zoom(3, { x: 100, y: 100 });
2019-12-02 19:58:02 +08:00
// Zoom at graph center with ratio 0.5
2019-11-20 10:47:11 +08:00
graph.zoom(0.5)
```
### zoomTo(toRatio, center)
2019-12-02 19:58:02 +08:00
Zoom the canvas at the center to a fixed ratio.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| toRatio | Number | true | Fixed zoom ratio. |
| center | Object | false | Zoom at the center with x and y. If the center is ignored, this operation will zoom the graph with the current graph center. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Zoom at center (100, 100) with ratio 3
2019-11-20 10:47:11 +08:00
graph.zoomTo(3, { x: 100, y: 100 });
2019-12-02 19:58:02 +08:00
// Zoom at graph center with ratio 0.5
2019-11-20 10:47:11 +08:00
graph.zoomTo(0.5)
```
### focusItem(item)
2019-12-02 19:58:02 +08:00
Move the graph to center at the item. This operation can be used as easing animation after searching a node.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| item | String / Object | true | The id or the instance of the item. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.focusItem(item)
```
### changeSize(width, height)
2019-12-02 19:58:02 +08:00
Change the size of the canvas.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| width | Number | true | The width of the canvas. |
| height | Number | true | The height of the canvas. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.changeSize(600, 350)
```
### translate(dx, dy)
2019-12-02 19:58:02 +08:00
Move the canvas with **relative displacement**.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| dx | Number | true | Displacement in the horizontal direction. |
| dy | Number | true | Displacement in the vertical direction. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.translate(100, 100);
```
### moveTo(x, y)
2019-12-02 19:58:02 +08:00
Move the canvas to a **fixed position**.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| x | Number | true | Displacement in the horizontal direction. |
| y | Number | true | Displacement in the vertical direction. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.moveTo(200, 300)
```
### fitView(padding)
2019-12-02 19:58:02 +08:00
Fit the graph to the view port.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| padding | Number / Array | false | The padding of [top, right, bottom, left]. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// When padding is a number, top = right = bottom = left = 20
2019-11-20 10:47:11 +08:00
graph.fitView(20)
2019-12-02 19:58:02 +08:00
// Equal to graph.fitView(20)
2019-11-20 10:47:11 +08:00
graph.fitView([20])
2019-12-02 19:58:02 +08:00
// When padding is an array with 2 values, top = bottom = 20, right = left = 10
2019-11-20 10:47:11 +08:00
graph.fitView([20, 10])
2019-12-02 19:58:02 +08:00
// When padding is an array with four values
2019-11-20 10:47:11 +08:00
graph.fitView([20, 10, 20, 15])
```
2019-12-02 19:58:02 +08:00
## Search
2019-11-20 10:47:11 +08:00
### find(type, fn)
2019-12-02 19:58:02 +08:00
Find single item according to a rule.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| type | String | true | Type of the item. Options: 'node', 'edge'. |
| fn | Function | true | Rule for searching. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- If there are items that match the rule, return the first one. Return `undefined` otherwise.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const findNode = graph.find('node', node => {
return node.get('model').x === 100;
});
```
### findById(id)
2019-12-02 19:58:02 +08:00
Find an item by id.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
| id | String | true | 元素 ID |
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- If there are items that match the rule, return the first one. Return `undefined` otherwise.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const node = graph.findById('node')
```
### findAll(type, fn)
2019-12-02 19:58:02 +08:00
Find all the items that match the rule.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| type | String | true | The type of the item. Options: 'node', 'edge'. |
| fn | Function | true | Rule for searching. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Array;
- If there are items that match the rule, return all of them. Return `undefined` otherwise.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const nodes = graph.findAll('node', node => {
return node.get('model').x;
});
```
### findAllByState(type, state)
2019-12-02 19:58:02 +08:00
Find all the items whose value of state is true.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| type | String | true | The type of the item. Options: 'node', 'edge'. |
| state | String | true | State for searching. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Array;
- Return all the items that match the state.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Find all the items whose 'selected' state is true
2019-11-20 10:47:11 +08:00
const nodes = graph.findAllByState('node', 'selected');
```
2019-12-02 19:58:02 +08:00
## Data
2019-11-20 10:47:11 +08:00
### save()
2019-12-02 19:58:02 +08:00
Get the graph data.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- The return value has all the nodes and edges as below:
2019-11-20 10:47:11 +08:00
```javascript
{
nodes: [],
edges: [],
groups: [],
}
```
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.save()
```
### getNodes()
2019-12-02 19:58:02 +08:00
Get all the node items in the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
⚠️**Attention:** it returns the items but not data models.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Array;
- Return the node items in the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const nodes = graph.getNodes()
```
### getEdges()
2019-12-02 19:58:02 +08:00
Get all the edge items in the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
<<<<<<< HEAD
⚠️**Attention:** it returns the items but not data models.
=======
Attention: it returns the items but not data models.
>>>>>>> feat: english version of API
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Array;
- Return the edge items in the graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const edges = graph.getEdges()
```
2019-12-02 19:58:02 +08:00
## Coordinate Transformation
In this part, we will describe the methods about transformation between view port, canvas, and client coordinates. The relationships between them are shown below:
2019-11-20 10:47:11 +08:00
<img src='https://gw.alipayobjects.com/mdn/rms_f8c6a0/afts/img/A*loahSq940hMAAAAAAAAAAABkARQnAQ' width=565 />
### getPointByClient(clientX, clientY)
2019-12-02 19:58:02 +08:00
Transform client/screen coordinates into view port coordinates.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| clientX | Number | true | x coordinate of client/screen. |
| clientY | Number | true | y coordinate of client/screen. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- Includes x and y.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const point = graph.getPointByClient(e.clientX, e.clientY);
2019-12-02 19:58:02 +08:00
console.log('The x and y of view port are: ', point.x, point.y)
2019-11-20 10:47:11 +08:00
```
### getClientByPoint(x, y)
2019-12-02 19:58:02 +08:00
Transform view port coordinates into client/screen coordinates.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| x | Number | true | x coordinate of view port. |
| y | Number | true | y coordinate of view port. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- Includes x and y.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const point = graph.getClientByPoint(100, 200);
2019-12-02 19:58:02 +08:00
console.log('The x and y of client/screen are: ', point.x, point.y)
2019-11-20 10:47:11 +08:00
```
### getPointByCanvas(canvasX, canvasY)
2019-12-02 19:58:02 +08:00
Transform canvas coordinates into view port coordinates.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| canvasX | Number | true | The x coordinate of canvas. |
| canvasY | Number | true | The y coordinate of canvas. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- Include x and y.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const point = graph.getPointByCanvas(100, 200);
2019-12-02 19:58:02 +08:00
console.log('The x and y of view port: ', point.x, point.y)
2019-11-20 10:47:11 +08:00
```
### getCanvasByPoint(x, y)
2019-12-02 19:58:02 +08:00
Transform view port coordinates into canvas coordinates.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| x | Number | true | The x coordinate of view port. |
| y | Number | true | The y coordinate of view port. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: Object;
- Includes x and y.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const point = graph.getCanvasByPoint(100, 200);
2019-12-02 19:58:02 +08:00
console.log('The x and y coordinates of canvas: ', point.x, point.y)
2019-11-20 10:47:11 +08:00
```
2019-12-02 19:58:02 +08:00
## Animation
2019-11-20 10:47:11 +08:00
### positionsAnimate()
2019-12-02 19:58:02 +08:00
Update the node positions according to the data model animatively.
2019-11-20 10:47:11 +08:00
### stopAnimate()
2019-12-02 19:58:02 +08:00
Stop the animation on the canvas.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.stopAnimate()
```
### isAnimating()
2019-12-02 19:58:02 +08:00
Return if the graph is animating.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
## Others
2019-11-20 10:47:11 +08:00
### addPlugin(plugin)
2019-12-02 19:58:02 +08:00
Add plugin to graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| plugin | Object | true | Instance of the plugin. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
import miniMap from '@antv/g6/build/minimap'
graph.Plugin(miniMap)
```
### removePlugin(plugin)
2019-12-02 19:58:02 +08:00
Remove the plugin from graph.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| plugin | Object | true | The Instance of the plugin. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
import miniMap from '@antv/g6/build/minimap'
graph.removePlugin(miniMap)
```
### get(key)
2019-12-02 19:58:02 +08:00
Get ant attribute of graph by key.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| key | String | true | Key of the attribute. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// get the group
2019-11-20 10:47:11 +08:00
const group = graph.get('group')
2019-12-02 19:58:02 +08:00
// get the canvas instance
2019-11-20 10:47:11 +08:00
const canvas = graph.get('canvas')
2019-12-02 19:58:02 +08:00
// get the value of autoPaint
2019-11-20 10:47:11 +08:00
const autoPaint = graph.get('autoPaint')
```
### set(key, val)
2019-12-02 19:58:02 +08:00
Set the value to an attribute.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| key | String | true | The key of the attribute. |
| val | String / Object | Array | true | The value of the attribute. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
2019-12-02 19:58:02 +08:00
// Set capture to false
2019-11-20 10:47:11 +08:00
graph.set('capture', false)
2019-12-02 19:58:02 +08:00
// Set customGroup to group
2019-11-20 10:47:11 +08:00
graph.set('customGroup', group)
2019-12-02 19:58:02 +08:00
// Set nodeIdList to [1, 3, 5]
2019-11-20 10:47:11 +08:00
graph.set('nodeIdList', [1, 3, 5])
```
### downloadImage(name)
2019-12-02 19:58:02 +08:00
Export the canvas as an image.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Parameters**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
| Name | Type | Required | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- | --- |
2019-12-02 19:58:02 +08:00
| name | String | true | The name of the image. |
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
graph.downloadImage()
```
### toDataURL()
2019-12-02 19:58:02 +08:00
Generate url of a image of the canvas.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Return**
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
- Type of the return value: String;
- The return value is the image url.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
const dataURL = graph.toDataURL()
```