g6/docs/api/Group.en.md

264 lines
7.9 KiB
Markdown
Raw Normal View History

2019-11-20 10:47:11 +08:00
---
2019-12-02 19:58:02 +08:00
title: Graphics Group
order: 10
2019-11-20 10:47:11 +08:00
---
2020-08-31 09:42:21 +08:00
Graphics Group (hereinafter referred to as Group) in G6 is similar to <a href='https://developer.mozilla.org/zh-CN/docs/Web/SVG/Element/g' target='_blank'> `<g>` tag in SVG </a>: Group a container of a group of graphics. The transformations on a Group such as clipping, rotating, zooming, and translating will be applied to all the children of the Group. The properties like color and position will also be inherited by its children. Besides, Group can be nested for complicated objects. For more information about Group, please refer to [Graphics Group](/en/docs/manual/middle/elements/shape/graphics-group) document.
2019-11-20 10:47:11 +08:00
## Get group of item
2019-11-20 10:47:11 +08:00
```javascript
// Find the graphics group of the item
const group = item.getContainer();
// equal to
const group = item.get('group');
2019-11-20 10:47:11 +08:00
```
## Methods
2019-11-20 10:47:11 +08:00
### group.addGroup(cfg)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Add a new group 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
2020-08-17 14:51:02 +08:00
| Name | Type | Description |
| ---- | ------ | --------------------------------------------------- |
| cfg | Object | Not required. It is the configurations of the group |
The `cfg` above is not required, and it contains:
| Name | Type | Description |
| --- | --- | --- |
| id | String | The unique id of this group |
| name | String | The name of the shape which can be not unique. It is required for each shape in G6 3.3. Besides, `name` can be used for searching this shape, e.g. `const shape = group.find(element => element.name === 'shape-name')`. The usage of find can be found at [find(fn)](#findfn) |
| visible | Boolean | Whether the group is visible |
| capture | Boolean | Whether the group is capturable |
| draggable | Boolean | Whether the group is allowed to response `dragstart`, `drag`, and `dragend` events. E.g. when user add a group into a custom node with `draggable: true`, the group will response the dragging events on the node, and the `'drag-node'` in the `modes` of the graph instance will take effect on the group |
| zIndex | Number | The visual index of the shape, similar to z-index of DOM. It is not required. `group.sort()` will sort the visual index of the shapes inside the group according to their zIndex |
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
// No configurations
group.addGroup();
// Configured
2019-11-20 10:47:11 +08:00
group.addGroup({
2020-02-14 10:10:54 +08:00
id: 'groupId',
draggable: true,
// other configurations
2020-02-14 10:10:54 +08:00
});
2019-11-20 10:47:11 +08:00
```
### group.addShape(type, cfgs)
2019-11-20 10:47:11 +08:00
2020-08-31 09:42:21 +08:00
Add a new shape into the group<br /><span style="background-color: rgb(251, 233, 231); color: rgb(139, 53, 56)"><strong>Attention:</strong></span> the clip and transform operations will affect all the shapes in the group. The graphics and their properties are introduced in [Shape Doc](/en/docs/manual/middle/elements/shape/shape-keyshape).
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 | Description |
2019-11-20 10:47:11 +08:00
| --- | --- | --- |
2020-08-31 09:42:21 +08:00
| type | String | The type of the shape. Options: `'rect'`, `'circle'`, `'fan'`, `'ellipse'`, `'marker'`, `'image'`, and so on. Please refer to [Graphics Shape Properties](/en/docs/manual/middle/elements/shape/shape-and-properties) document |
2019-12-02 19:58:02 +08:00
| cfg | Object | The configurations of the shape. |
2019-11-20 10:47:11 +08:00
2020-02-07 22:05:40 +08:00
The `cfg` above contains:
| Name | Type | Description |
| --- | --- | --- |
| attrs | Object | The style configurations for the shape. e.g. `{x: 0, y: 10, fill: '#0f0'}` |
| name | String | The name of the shape which can be not unique. It is required for each shape in G6 3.3. Besides, `name` can be used for searching this shape, e.g. `const shape = group.find(element => element.name === 'shape-name')`. The usage of find can be found at [find(fn)](#findfn) |
| visible | Boolean | Whether the shape is visible |
| capture | Boolean | Whether the shape is capturable by mouse events |
| draggable | Boolean | Whether the shape is allowed to response `dragstart`, `drag`, and `dragend` events. E.g. when user add a shape into a custom node with `draggable: true`, the shape will response the dragging events on the node, and the `'drag-node'` in the `modes` of the graph instance will take effect |
2020-02-07 22:05:40 +08:00
| zIndex | Number | The visual index of the shape, similar to z-index of DOM. It is not required. `group.sort()` will sort the visual index of the shapes inside the group according to their zIndex |
2019-12-02 19:58:02 +08:00
**Usage**
2019-11-20 10:47:11 +08:00
```javascript
group.addShape('rect', {
2020-02-14 10:10:54 +08:00
attrs: {
x: 0, // required
y: 0, // required
2020-02-14 10:10:54 +08:00
fill: 'red',
2019-11-20 10:47:11 +08:00
shadowOffsetX: 10,
shadowOffsetY: 10,
shadowColor: 'blue',
shadowBlur: 10,
2020-02-14 10:10:54 +08:00
opacity: 0.8,
2020-02-07 22:05:40 +08:00
},
// must be assigned in G6 3.3 and later versions. it can be any value you want
name: 'rect-shape',
2020-02-14 10:10:54 +08:00
zIndex: 1,
});
2019-11-20 10:47:11 +08:00
```
### group.contain(child)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Whether the group contains the child.<br />The type of the return value: Boolean.
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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ----- | ------------- | ------------------------------------ |
2019-12-02 19:58:02 +08:00
| child | Group / Shape | A sub group or an instance of shape. |
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
2020-02-14 10:10:54 +08:00
const has = group.contain(child);
2019-11-20 10:47:11 +08:00
```
### group.find(fn)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Find **the first** element that matches 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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ---- | -------- | ----------------------------- |
| fn | Function | Customized callback function. |
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
2020-08-17 14:51:02 +08:00
const child = group.find(function (item) {
2020-02-14 10:10:54 +08:00
return item.attr('fill') === 'red'; // Find the first graphics filled with red
2019-11-20 10:47:11 +08:00
});
```
### group.findById(id)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Find the element by its id. <br />The type of the return value: Object。
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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ---- | ------ | -------------------- |
| id | String | 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
const group1 = group.findById('group1');
```
### group.findAll(fn)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Find all the elements that match the rule.<br />The type of the return value: [ Object ]
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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ---- | -------- | ----------------------------- |
| fn | Function | Customized callback function. |
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
2020-08-17 14:51:02 +08:00
const children = group.findAll(function (item) {
2020-02-14 10:10:54 +08:00
return item.get('id') < 10; // get all the elements with the id smaller than 10
2019-11-20 10:47:11 +08:00
});
```
### group.getShape(x,y)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Get the top shape which is on (x, y). <br />The type of the return value: Object
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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ---- | ------ | ------------ |
| x | number | x coordinate |
| y | number | y coordinate |
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 top element on (10, 30)
2020-02-14 10:10:54 +08:00
const element = group.getShape(10, 30);
2019-11-20 10:47:11 +08:00
```
### group.getFirst()
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Get **the first** element of the group. <br />The type of the return value: Object
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
2020-02-14 10:10:54 +08:00
const child = group.getFirst();
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
// Equal to
2020-02-14 10:10:54 +08:00
const childrens = group.get('children');
const child = childrens[0];
2019-11-20 10:47:11 +08:00
```
### group.getLast()
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Get the last element of the group. <br />The type of the return value: Object
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
2020-02-14 10:10:54 +08:00
const child = group.getLast();
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
// Equal to
2020-02-14 10:10:54 +08:00
const childrens = group.get('children');
const child = childrens[childrens.length - 1];
2019-11-20 10:47:11 +08:00
```
### group.getChildByIndex(index
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Get the `index`-th child of the group started from `0`.<br />The type of the return value: Object
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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ----- | ------ | ------------------------------------- |
2019-12-02 19:58:02 +08:00
| index | number | The index of the child. 0 by default. |
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
2020-02-14 10:10:54 +08:00
const child = group.getChildByIndex(2);
2019-11-20 10:47:11 +08:00
```
### group.removeChild(child)
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Remove a group or a graphics from 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
2020-02-14 10:10:54 +08:00
| Name | Type | Description |
| ----- | ------------- | ------------------------------------ |
2019-12-02 19:58:02 +08:00
| child | Group / Shape | A sub group or an instance of Shape. |
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
2020-02-14 10:10:54 +08:00
group.removeChild(child);
2019-11-20 10:47:11 +08:00
```
### group.sort()
2020-02-14 10:10:54 +08:00
2019-12-02 19:58:02 +08:00
Sort method. <br />In general, it is called for ordering the children of the group.
2019-11-20 10:47:11 +08:00
2019-12-02 19:58:02 +08:00
Typical scenerio: we set index for each `shape` when add `shape` by `group.addShape()`. After adding, sort the shapes by calling `group.sort()`.
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
2020-02-14 10:10:54 +08:00
group.sort();
2019-11-20 10:47:11 +08:00
```
### group.clear()
2019-11-20 10:47:11 +08:00
2020-02-14 10:10:54 +08:00
Clear all the children in 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
2020-02-14 10:10:54 +08:00
group.clear();
2019-11-20 10:47:11 +08:00
```