Polyline

4 min read

A built-in edge Polyline has the default style as below.
img

Usage

As stated in Built-in Edges , there are three methods to configure edges: Configure edges globally when instantiating a Graph; Configure edges in their data; Configure edges by graph.edge(edgeFn). Their priorities are:

graph.edge(edgeFn) > Configure in data > Configure globally

⚠️ Attention: Expect for id, source, target, label which should be assigned to every single edge data, the other configurations in The Common Property and in each edge type (refer to doc of each edge type) support to be assigned by the three ways.

1 Global Configure When Instantiating a Graph

Assign type to 'cubic' in the defaultEdge object when instantiating a Graph:

const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultEdge: {
    type: 'polyline', // The type of the edge
    // ...  Other configuraltions
  },
});

2 Configure in the Data

To configure different edges with different properties, you can write the properties into the edge data.

const data = {
  nodes: [
    // ... // nodes
  ],
  edges: [{
    source: 'node0',
    target: 'node1'
    type: 'polyline',
    //... // Other configurations for edges
    style: {
      //...  // Style properties for edges
    }
  },
    //... // Other edges
  ]
}

Property

Polyline edge has the Common Edge Properties, and some commonly used properties are shown below. The properties with object type will be described in detail after the table

color: '#87e8de',
style: {
  offset: 20,  // The minimum distance between the bend and the source/target node
  radius: 10,  // The border radius of the bend.
  lineWidth: 2,
  stroke: '#87e8de'
},
label: 'Label text',
labelCfg: {
  refX: 10,  // x offset of the label
  refY: 10,  // y offset of the label
  style: {
    fill: '#595959'
  }
}
NameDescriptionTypeRemark
colorThe color of the edgeStringThe priority id lower than stroke in style
styleThe default style of edgeObjectCorrespond to the styles in Canvas
style.radiusThe border radius of the bendNumberIt is a special property for polyline edge
style.offsetThe minimum distance between the bend and the source/target nodeNumber5 by default. It is a special property for polyline edge
labelThe text of the labelString
labelCfgThe configurations of the labelObject
controlPointsThe array of the control points for the polylineArrayIf it is not assigned, G6 will calculate it by A* algorithm If it is assgned, the path of the polyline will be generated according to it. e.g. [{ x: 10, y: 20 }, { x: 20, y: 25 }, ...]
stateStylesThe styles in different statesObjectRefer to Configure Styles for State

style

style is an object. The Common Edge Style Properties are available for polyline edge. There are two special properties in style for polyline edge:

  • radius, he border radius of the bend;
  • offset, The minimum distance between the bend and the source/target node, 5 by default.

The other style properties are the same as the common style property of edge. Refer to [Built-in Edges]. The following code shows how to configure the style globally when instantiating a Graph.
img

const data = {
  nodes: [
    {
      id: 'node0',
      x: 100,
      y: 100,
      size: 20,
    },
    {
      id: 'node1',
      x: 200,
      y: 200,
      size: 20,
    },
  ],
  edges: [
    {
      source: 'node0',
      target: 'node1',
      type: 'polyline',
      label: 'polyline',
    },
  ],
};
const graph = new G6.Graph({
  container: 'mountNode',
  width: 800,
  height: 600,
  defaultEdge: {
    // type: 'polyline',  // The type has been assigned in the data, we do not have to define it any more
    style: {
      radius: 10,
      offset: 10,
      stroke: 'steelblue',
      lineWidth: 5,
    },
  },
});
graph.data(data);
graph.render();

labelCfg

labelCfg is an object which is the same as the Common Edge Label Properties. Base on the code in style section, we add labelCfg to defaultEdge.
img

const data = {
  // ... data
};
const graph = new G6.Graph({
  // ... Other configurations for graph
  defaultEdge: {
    // ... Other properties for edges
    labelCfg: {
      refY: -10,
      refX: 60,
    },
  },
});
// ...