G6 is a data-driven charting library, where data is one of the most important concepts. In G6, data is the core of the chart, and both display and interaction are based on data.
Common graph data formats include:CSV, DOT, GDF, GML, GraphML, GEXF etc。
G6 uses JSON format to describe the graph structure, which includes information about nodes and edges. Here is a simple JSON data example:
{"nodes": [{ "id": "node1" }, { "id": "node2" }],"edges": [{ "source": "node1", "target": "node2" }]}
Compared to the other formats mentioned above, the JSON format has a more intuitive and understandable data structure. It is also more flexible, allowing for easy expansion of node and edge attributes.
It is a data exchange format widely supported by computers, so you do not have to worry about data format compatibility issues.
In G6, graph data consists of three parts: nodes
(node data), edges
(edge data), and combos
(combo data). Each part corresponds to different elements in the graph, and their types and data determine how the graph is displayed.
interface GraphData {nodes: NodeData[]; // Node dataedges?: EdgeData[]; // Edge data (optional)combos?: ComboData[]; // Combo data (optional)}
A node is the basic building block of a graph and represents an entity within the graph. Each node has a unique id
used to identify it, and nodes can also have data, styles, and states.
Attribute | Type | Description |
---|---|---|
Required id | string | Unique identifier for the node, used to distinguish different nodes |
type | string | Node type. It can be the type of built-in Node, or the custom Node |
data | Object | Custom data for the node, such as name, description, etc. Can be accessed in style mappings via callback functions |
style | Object | Node style, including position, size, color, and other visual properties |
states | string[] | Initial states for the node, such as selected, active, hover, etc. |
combo | string | null | ID of the combo the node belongs to. Used to organize hierarchical relationships. If none, it is null |
children | string[] | Collection of child node IDs, used only in tree diagrams |
Example:
{"id": "node-1","type": "circle","data": { "name": "alice", "role": "Admin" },"style": { "x": 100, "y": 200, "size": 32, "fill": "violet" },"states": ["selected"],"combo": null}
An edge connects nodes and represents the relationship between them. Each edge is associated with two nodes (source and target), and edges themselves can have data, styles, and states. Edge data is often used to represent logical relationships, such as user connections in social networks or step flows in flowcharts.
Attribute | Type | Description |
---|---|---|
Required source | string | Source node ID |
Required target | string | Target node ID |
id | string | Unique identifier for the edge. If not specified, id is automatically generated with the format ${source}-${target} |
type | string | Edge type.It can be the type of built-in Edge, or the custom Edge |
data | Object | Custom data for the edge, accessible in style mappings via callback functions |
style | Object | Edge style, including stroke color, line width, arrowhead, etc. |
states | string[] | Initial states for the edge |
Example:
{"source": "alice","target": "bob","type": "line","data": { "relationship": "friend", "strength": 5 },"style": { "stroke": "green", "lineWidth": 2 },"states": ["hover"]}
Combos allow you to create a logical unit for multiple nodes, used for layering, grouping, or other structural purposes. A combo can contain child nodes or other combos, forming a nested structure.
Attribute | Type | Description |
---|---|---|
Required id | string | Unique identifier for the combo |
type | string | Combo type.It can be the type of built-in Combo, or the custom Combo |
data | Object | Custom data for the combo, accessible in style mappings via callback functions |
style | Object | Combo style |
states | string[] | Initial states for the combo |
combo | string | null | Parent combo ID. If there is no parent combo, it is null |
Example:
{"id": "combo1","type": "circle","data": { "groupName": "Group A" },"style": { "fill": "lightblue", "stroke": "blue", "collapsed": true },"states": [],"combo": null}
To ensure correct rendering and interaction of the graph, it is recommended to organize the data according to G6's standard data structure. Each element (node, edge, combo) should contain a data
field to store business data and custom properties.
id
, type
, style
, etc., to prevent naming conflicts.data
field. This ensures flexibility and scalability of the data.Example:
{"nodes": [{"id": "node1","data": { "name": "Alice", "role": "Admin" }},{"id": "node2","data": { "name": "Bob", "role": "User" }}],"edges": [{"source": "node1","target": "node2","data": { "relationship": "friend" }}]}
G6 provides a series of APIs to access and manipulate data, including:
Through different APIs, you can conveniently access and manipulate graph data, performing operations such as adding, deleting, modifying, and querying the graph.
G6 does not provide functionality for data retrieval and parsing. For local JSON data, you can directly import and use it as follows:
import data from './path/to/data.json' assert { type: 'json' };
For remote data, you can use fetch
or other networking libraries to retrieve the data:
fetch('https://path/to/data.json').then((res) => res.json()).then((data) => {// Use data});