This article will provide a simple drawing and configuration example for the **Tutorial Example**. Through this article, you will learn about some commonly used configuration options and their functions when creating a general graph.
To contain the G6 graph, you need to create a container in HTML, usually a `div` tag. G6 will append a `canvas` tag under this container and draw the graph in it.
The data source for G6 is a JSON-formatted object. The data format of v5 is different from v4. For more information, please refer to the [Data Format Change](https://g6-next.antv.antgroup.com/en/manual/upgrade#1%EF%B8%8F%E2%83%A3-%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F%E5%8F%98%E6%9B%B4) section. v5 also provides a data conversion processor for v4 data, which will be explained in the following graph configuration.
- The `nodes` array contains node objects. Each node object has a unique and necessary `id` to identify different nodes. `x` and `y` specify the position of the node.
- The `edges` array contains edge objects. `source` and `target` are the required properties of each edge, representing the `id` of the starting point and the `id` of the target point, respectively.
When instantiating the graph, at least the container needs to be set for the graph. If you are using the graph data format of G6 v4, you can configure `transforms: ['transform-v4-data']` when instantiating the graph. `'transform-v4-data'` is a built-in data converter in G6 v5, which will format the v4 data into the format required by v5 after reading the data.
In the previous sections, we used data with only two nodes and one edge, and directly defined the data in the code. However, the data in real scenarios is usually loaded from remote interfaces. For convenience, we have prepared a JSON data file for readers. The address is as follows:
> The `fetch` function allows us to make network requests and load data, and its asynchronous process can be better controlled using `async`/`await`. Here, for convenience, we put the main logic inside the `main` function. If you have any questions about fetch and `async`/`await`, you can refer to the <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function' target='_blank'>async function</a>, <a href='https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API' target='_blank'>Fetch API</a>.
At first glance, the image looks a bit strange, but the data has actually been correctly loaded. Due to the large amount of data, there are many nodes and edges, and the grid layout, which is the default, does not show the connection between the nodes. Next, we will use more graph configurations to make the data clearer and more visually appealing.
| transforms | Array | ['transform-v4-data', { type: 'map-node-size', field: 'value' }] | null | Data processors. After the original user data enters the Graph, the processors in the transform are executed in order to obtain the processed data. |
| |
| modes | Object | {<br/> default: [ 'drag-node', 'drag-canvas' ]<br/>} | null | The collection of behavior modes on the graph. |
| node | Object / Function | {<br/> type: 'circle',<br/> keyShape: {<br/> ......<br/> }<br/>} | null | The global attribute mapper of the node, including general attributes and style attributes (style). v5 also supports function mapping. |
| edge | Object / Function | {<br/> type: 'polyline',<br/> keyShape: {<br/> ......<br/> }<br/>} | null | The global attribute mapper of the edge, including general attributes and style attributes (style). v5 also supports function mapping. |
| nodeState | Object | {<br/> hover: {<br/> ......<br/> },<br/> select: {<br/> ......<br/> }<br/>} | null | The style attributes (style) of the node in states other than the default state, such as hover and select. |
| |
| edgeState | Object | {<br/> hover: {<br/> ......<br/> },<br/> select: {<br/> ......<br/> }<br/>} | null | The style attributes (style) of the edge in states other than the default state, such as hover and select. |
**⚠️ Note:**<br/> If you need to replace the data, please replace `'https://raw.githubusercontent.com/antvis/G6/v5/packages/g6/tests/datasets/force-data.json'` with the new data file address.