Loading...
Plugins are an important mechanism in G6 for extending functionality and enhancing the interactive experience of graphs. Plugins typically provide independent functional modules, such as thumbnails, toolbars, context menus, etc. They integrate well with the main graph while maintaining modular and maintainable code.
The plugin system is designed to follow the "plug and play" principle, allowing dynamic addition or removal as needed.
Retrieve the plugin instance specified by the key, used to access and operate the methods provided by the plugin.
getPluginInstance<T extends Plugin>(key: string): T;
Parameters
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
key | Unique identifier of the plugin | string | - | ✓ |
Return Value
Note
Many plugins provide specific API methods, which can be directly called by obtaining the plugin instance. For example, the fullscreen plugin provides request()
and exit()
methods to control fullscreen status.
Example: Operate the fullscreen plugin
// Get the fullscreen plugin instanceconst fullscreen = graph.getPluginInstance('fullscreen');// Request to enter fullscreenfullscreen.request();// Exit fullscreen latersetTimeout(() => {fullscreen.exit();}, 5000);
Retrieve all configured plugins in the current graph.
getPlugins(): PluginOptions;
Return Value
Example
// Get all plugin configurationsconst plugins = graph.getPlugins();// View currently active pluginsconsole.log('Current graph plugin configurations:', plugins);
Set the graph's plugins, replacing all existing plugin configurations.
setPlugins(plugins: PluginOptions | ((prev: PluginOptions) => PluginOptions)): void;
Parameters
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
plugins | New plugin configurations, or a function returning new configurations based on the current ones | PluginOptions | ((prev: PluginOptions) => PluginOptions) | - | ✓ |
Note
The set plugins will completely replace the original plugin configurations. To add new plugins based on existing ones, you can use functional updates:
graph.setPlugins((plugins) => [...plugins, { type: 'grid', key: 'grid-line' }]);
Example 1: Set basic plugins
// Set multiple basic pluginsgraph.setPlugins([// String form (using default configuration)'minimap',// Object form (custom configuration){type: 'grid',key: 'grid-line',},{type: 'toolbar',key: 'graph-toolbar',position: 'top-right',},]);
Example 2: Use functional updates
// Add new plugins to existing configurationsgraph.setPlugins((currentPlugins) => [...currentPlugins,{type: 'grid',key: 'grid-line',},]);// Replace specific pluginsgraph.setPlugins((currentPlugins) => {// Filter out existing grid pluginsconst filteredPlugins = currentPlugins.filter((plugin) => {if (typeof plugin === 'string') return plugin !== 'grid';if (typeof plugin === 'function') return true;return plugin.type !== 'grid';});// Add new grid plugin configurationreturn [...filteredPlugins,{type: 'grid',key: 'new-grid',follow: true,},];});
Update the configuration of a specified plugin, identified by the key
of the plugin to be updated.
updatePlugin(plugin: UpdatePluginOption): void;
Parameters
Parameter | Description | Type | Default | Required |
---|---|---|---|---|
plugin | Configuration of the plugin to be updated | UpdatePluginOption | - | ✓ |
Note
To update a plugin, the key
field must be specified in the original plugin configuration to accurately locate and update the plugin.
Example 1: Update plugin configuration
// Specify key when initially setting pluginsgraph.setPlugins([{type: 'grid',key: 'main-grid',follow: true,},]);// Update grid plugin configurationgraph.updatePlugin({key: 'main-grid',follow: false,});
Plugin configuration type, representing an array of plugin configurations.
type PluginOptions = (string | CustomPluginOption | ((this: Graph) => CustomPluginOption))[];
Custom plugin configuration interface, used to configure plugin parameters.
type CustomPluginOption = {// Plugin typetype: string;// Plugin key, i.e., unique identifier// Used to identify the plugin for further operationskey?: string;// Other configuration items for different types of plugins[configKey: string]: any;};
Configuration interface for updating plugins, used to dynamically modify plugin parameters.
type UpdatePluginOption = {// Unique identifier of the plugin to be updatedkey: string;// Other configuration items to be updated[configKey: string]: unknown;};