This package provides Topology View components based on PatternFly from https://github.com/patternfly/patternfly-react
To use React Topology out-of-the-box, you will first need to transform your back-end data into a Model. These model objects contain the information needed to display the nodes and edges. Each node and edge has a set of properties used by PF Topology as well as a data field which can be used to customize the nodes and edges by the application.
This project currently supports Node Active LTS releases. Please stay current with Node Active LTS when developing patternfly-react.
For example, to develop with Node 18, use the following:
nvm install 18
nvm use 18
This project also requires a Yarn version of >=1.6.0. The latest version can be installed here.
yarn add @patternfly/react-topology
or
npm install @patternfly/react-topology --save
- Create a new Controller which can be done using the default
Visualization
class.
It is important to note that three register
methods are accessed by the controller.
The following two must be declared explicitly:
- `registerLayoutFactory`: This method sets the layout of your topology view (e.g. Force, Dagre, Cola, etc.). If your application supports all layouts, use `defaultLayoutFactory` as a parameter. If you only want to support a subset of the available layout options, update `defaultLayout` to a custom implementation .
- `registerComponentFactory`: This method lets you customize the components in your topology view (e.g. nodes, groups, and edges). You can use `defaultComponentFactory` as a parameter.
The register method below is initialized in Visualization.ts
. It doesn't need to be declared unless you support a custom implementation which modifies the types.
- `registerElementFactory`: This method sets the types of the elements being used (e.g. graphs, nodes, edges). `defaultElementFactory` uses types from `ModelKind` and is exported in `index.ts`.
-
The
fromModel
method must be called on the controller to create the nodes.fromModel
will take your data model as a parameter. Your data model should include agraph
object, on which you will need to setid
,type
andlayout
. -
To create your topology view component, add a
VisualizationProvider
, which is a useful context provider. It allows access to the created Controller and is required when using theVisualizationSurface
component. -
You can use the provided
VisualizationSurface
to provide the SVG component required for the topology components. TheVisualizationSurface
can take a state parameter that will allow you to pass your state settings along to the Controller.
import * as React from 'react';
import {
ColaLayout,
ComponentFactory,
DefaultEdge,
DefaultGroup,
DefaultNode,
EdgeStyle,
Graph,
GraphComponent,
Layout,
LayoutFactory,
Model,
ModelKind,
NodeShape,
SELECTION_EVENT,
Visualization,
VisualizationProvider,
VisualizationSurface
} from '@patternfly/react-topology';
const baselineLayoutFactory: LayoutFactory = (type: string, graph: Graph): Layout | undefined => {
switch (type) {
case 'Cola':
return new ColaLayout(graph);
default:
return new ColaLayout(graph, { layoutOnDrag: false });
}
};
const baselineComponentFactory: ComponentFactory = (kind: ModelKind, type: string) => {
switch (type) {
case 'group':
return DefaultGroup;
default:
switch (kind) {
case ModelKind.graph:
return GraphComponent;
case ModelKind.node:
return DefaultNode;
case ModelKind.edge:
return DefaultEdge;
default:
return undefined;
}
}
};
const NODE_SHAPE = NodeShape.ellipse;
const NODE_DIAMETER = 75;
const NODES = [
{
id: 'node-0',
type: 'node',
label: 'Node 0',
width: NODE_DIAMETER,
height: NODE_DIAMETER,
shape: NODE_SHAPE
},
{
id: 'node-1',
type: 'node',
label: 'Node 1',
width: NODE_DIAMETER,
height: NODE_DIAMETER,
shape: NODE_SHAPE
},
{
id: 'node-2',
type: 'node',
label: 'Node 2',
width: NODE_DIAMETER,
height: NODE_DIAMETER,
shape: NODE_SHAPE
},
{
id: 'node-3',
type: 'node',
label: 'Node 3',
width: NODE_DIAMETER,
height: NODE_DIAMETER,
shape: NODE_SHAPE
},
{
id: 'node-4',
type: 'node',
label: 'Node 4',
width: NODE_DIAMETER,
height: NODE_DIAMETER,
shape: NODE_SHAPE
},
{
id: 'node-5',
type: 'node',
label: 'Node 5',
width: NODE_DIAMETER,
height: NODE_DIAMETER,
shape: NODE_SHAPE
},
{
id: 'Group-1',
children: ['node-0', 'node-1', 'node-2'],
type: 'group',
group: true,
label: 'Group-1',
style: {
padding: 40
}
}
];
const EDGES = [
{
id: 'edge-node-4-node-5',
type: 'edge',
source: 'node-4',
target: 'node-5',
edgeStyle: EdgeStyle.default
},
{
id: 'edge-node-0-node-2',
type: 'edge',
source: 'node-0',
target: 'node-2',
edgeStyle: EdgeStyle.default
}
];
export const TopologyGettingStartedDemo: React.FC = () => {
const [selectedIds, setSelectedIds] = React.useState<string[]>([]);
const controller = React.useMemo(() => {
const model: Model = {
nodes: NODES,
edges: EDGES,
graph: {
id: 'g1',
type: 'graph',
layout: 'Cola'
}
};
const newController = new Visualization();
newController.registerLayoutFactory(baselineLayoutFactory);
newController.registerComponentFactory(baselineComponentFactory);
newController.addEventListener(SELECTION_EVENT, setSelectedIds);
newController.fromModel(model, false);
return newController;
}, []);
return (
<VisualizationProvider controller={controller}>
<VisualizationSurface state={{ selectedIds }} />
</VisualizationProvider>
);
};
Live docs available on patternfly.org