--- title: Getting Started order: 1 --- ## The First Example ## Installation & Import There are two ways to import G6: by NPM; by CDN. ### 1 Import G6 by NPM **Step 1:** Run the following command under the your project's directory in terminal: ```bash npm install --save @antv/g6 ``` **Step 2:** Import the JS file to the file where G6 is going to be used: ```javascript import G6 from '@antv/g6'; ``` ### 2 Import by CDN in HTML ```html // version <= 3.2 // version >= 3.3 // version >= 4.0 ``` ⚠️Attention: - Replace `{$version}` by the version number. e.g. `3.7.1`; - The last version and its number can be found on NPM; - Please refer to the branch in Github: https://github.com/antvis/g6/tree/master for more detail. ## Getting Started The following steps lead to a Graph of G6: 1. Create an HTML container for graph; 2. Prepare the data; 3. Instancialize the Graph; 4. Load the data and render. ### Step 1 Create a HTML Container Create an HTML container for graph canvas, `div` tag in general. G6 will append a `canvas` tag to it and draw graph on the `canvas`. ```html
``` ### Step 2 Data Preparation The data for G6 should be JSON format, includes arrays `nodes` and `edges`: ```javascript const data = { // The array of nodes nodes: [ { id: 'node1', // String, unique and required x: 100, // Number, the x coordinate y: 200, // Number, the y coordinate }, { id: 'node2', // String, unique and required x: 300, // Number, the x coordinate y: 200, // Number, the y coordinate }, ], // The array of edges edges: [ { source: 'node1', // String, required, the id of the source node target: 'node2', // String, required, the id of the target node }, ], }; ``` ⚠️Attention: - `nodes` is an array of nodes, the `id` is unique and required property; the `x` and `y` are coordinates of the node; - `edges` is an array of edges, `source` and `target` are required, represent the `id` of the source node and the `id` of the target node respectively; - The properties of node and edge are described in [Built-in Nodes](/en/docs/manual/middle/elements/nodes/defaultNode) and [Built-in Edges](/en/docs/manual/middle/elements/edges/defaultEdge). ### Step 3 Instantiate the Graph The container, width, and height are required configurations when instantiating a Graph: ```javascript const graph = new G6.Graph({ container: 'mountNode', // String | HTMLElement, required, the id of DOM element or an HTML node width: 800, // Number, required, the width of the graph height: 500, // Number, required, the height of the graph }); ``` ### Step 4 Load the Data and Render ```javascript graph.data(data); // Load the data defined in Step 2 graph.render(); // Render the graph ``` ### The Final Result ## The Complete Code ```html