Canvas rendering routines for graphology
.
npm install graphology-canvas
If you need to use this package's functions in node, you will also need to install node-canvas
thusly:
npm install canvas
If you experience any issue when installing the libray check that you have the required dependencies as listed here.
Each node's position must be set before rendering a graph. Two attributes called x
and y
must therefore be defined for all the graph nodes. graphology-layout or graphology-layout-forceatlas2, for instance, can be used to give positions to nodes if they don't have one already.
import {render} from 'graphology-canvas';
render(graph, context, settings);
import {renderAsync} from 'graphology-canvas';
renderAsync(graph, context, settings, function () {
console.log('Done!');
});
import {renderToPNG} from 'graphology-canvas/node';
renderToPNG(graph, './graph.png', () => console.log('Done!'));
renderToPNG(graph, './graph.png', settings, () => console.log('Done!'));
-
width ?number [
2048
]: width of the canvas. Will be the same asheight
if not provided. -
height ?number [
2048
]: height of the canvas. Will be the same aswidth
if not provided. -
padding ?number [
20
]: padding to keep around the drawing. -
nodes ?object: node-related settings:
-
defaultColor ?string [
#999
]: default color for nodes. -
reducer ?function: reducer fonction for nodes taking the rendering settings, the node key and its attributes and tasked to return rendering info such as
color
,size
etc.
-
defaultColor ?string [
-
edges ?object: node-related settings:
-
defaultColor ?string [
#ccc
]: default color for edges. -
reducer ?function: reducer fonction for edges taking the rendering settings, the node key and its attributes and tasked to return rendering info such as
color
,size
etc.
-
defaultColor ?string [
-
batchSize ?number [
500
]: number of items to render on canvas on each animation frame, increase or decrease to tweak performance vs. UI availability.