Loading...
The options of a G6 graph instance control various aspects of the graph, including canvas settings, viewport properties, data, layout, styles, interaction behaviors, plugins, and more. By configuring these options appropriately, you can flexibly customize the appearance and behavior of the graph.
Options can be specified when creating a graph instance or dynamically modified at runtime through the API. Some basic configurations (such as devicePixelRatio, container) require destroying and recreating the graph instance to take effect after modification.
Retrieve all configuration options of the current graph.
getOptions(): GraphOptions;
Return Value
Example
// Retrieve the current graph's optionsconst options = graph.getOptions();console.log('Current graph options:', options);// Retrieve specific optionsconsole.log('Current canvas width:', options.width);console.log('Current layout options:', options.layout);
Update the graph's configuration options.
setOptions(options: GraphOptions): void;
Parameters
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
options | New configuration options | GraphOptions | - | ✓ |
Note
⚠️ Attention: To update basic properties like devicePixelRatio, container, etc., you need to destroy the current graph instance and recreate it. Most other configurations can be dynamically updated.
Example 1: Basic Usage
// Update graph configurationgraph.setOptions({width: 1000, // Update widthheight: 800, // Update heightautoFit: true, // Enable auto-fitanimation: true, // Enable animation});
Example 2: Update Theme
// Update graph theme configurationgraph.setOptions({theme: {type: 'dark', // Switch to dark theme// Custom theme configurationnode: {palette: ['#1AAF8B', '#F8E71C', '#8B572A', '#7ED321'],},edge: {palette: ['#F5A623', '#F8E71C', '#8B572A', '#7ED321'],},},});
Example 3: Update Layout Configuration
// Update layout configurationgraph.setOptions({layout: {type: 'force', // Switch to force-directed layoutpreventOverlap: true,nodeStrength: -50,edgeStrength: 0.7,},});
Example 4: Update Default Node and Edge Configuration
// Update default style configuration for nodes and edgesgraph.setOptions({node: {style: {fill: '#91d5ff',stroke: '#40a9ff',lineWidth: 1,radius: 10,},},edge: {style: {stroke: '#91d5ff',lineWidth: 2,endArrow: true,},},});
type GraphOptions = {// Whether to enable zoomingenableZoom?: boolean;// Whether to enable draggingenableDrag?: boolean;// Default style for nodesdefaultNodeStyle?: {fill: string;stroke: string;};// Additional configuration options for the graph[configKey: string]: any;};