Loading...
A Shape in G6 refers to a graphical element, such as a circle, rectangle, or path. Shapes are generally associated with nodes, edges, or combos in G6. 💡 Every node/edge/combo in G6 is composed of one or more shapes. The style configuration of nodes, edges, and combos is reflected on their corresponding shapes.
For example, in the images below: the node on the left contains a single circular shape; the node in the middle contains a circle and a text shape; the node on the right contains five circles (the blue-green main circle and four anchor points at the top, bottom, left, and right) and a text shape. Each node/edge/combo has its unique key shape (keyShape). In the examples below, the keyShape for all three nodes is the blue-green circle. The keyShape is mainly used for interaction detection and automatic style updates with element states, see keyShape.
(Left) A node with only one circular shape, whose keyShape is the circle. (Middle) A node with a circle and a text shape, whose keyShape is the circle. (Right) A node with a main circle, text, and four small circles at the top, bottom, left, and right, whose keyShape is the main circle.
G6 uses different combinations of shapes to design various built-in nodes/edges/combos. Built-in nodes include 'circle', 'rect', 'ellipse', ... (see Built-in Nodes); built-in edges include 'line', 'polyline', 'cubic', ... (see Built-in Edges); built-in combos include 'circle', 'rect', ... (see Built-in Combos).
In addition to using built-in nodes/edges/combos, G6 also allows users to customize nodes/edges/combos by combining shapes as needed. See Custom Node, Custom Edge, and Custom Combo for details.
In G6, each node, edge, or combo consists of one or more shapes, but one of them is called the keyShape, which is the "key graphical element" of the item:
The key graphical element of the node is the colored area in the image above.
Determines the bounding box of a node/combo, which is used to calculate the connection point of related edges (the intersection with the edge). If the keyShape is different, the intersection calculation between the node and the edge will also differ.
In this example, a node consists of a rect shape and a circle shape with a gray stroke and transparent fill.
If you need to customize nodes, customize edges, or customize combos, you need to understand the lifecycle of shapes. If you use built-in nodes/edges/combos, you can skip this section.
The shape lifecycle includes:
When customizing shapes, the most common requirement is "how to efficiently manage the creation, update, and destruction of shapes." For this, G6 provides a very useful method in BaseShape:
upsert is a combination of "update" and "insert", meaning "update if exists, insert if not". Its function can be simply understood as:
upsert
will automatically determine whether the shape already exists. If not, it will create it; if it exists, it will update it; if it needs to be deleted, it will remove it automatically.Type Definition:
/*** Create, update, or delete a shape* @param className Shape name* @param Ctor Shape type* @param style Shape style. Pass false to delete the shape* @param container Container* @param hooks Hooks* @returns Shape instance*/upsert<T extends DisplayObject>(className: string,Ctor: string | { new (...args: any[]): T },style: T['attributes'] | false,container: DisplayObject,hooks?: UpsertHooks,): T | undefined {}
You only need to describe "what kind of shape you want now" without worrying about whether it is being created, updated, or deleted. upsert will handle it for you. This makes customizing and managing complex composite shapes very simple and safe.