react 您所在的位置:网站首页 react-diagram react

react

2024-07-15 14:50| 来源: 网络整理| 查看: 265

Using the library Using Typescript

If you are using typescript, then you are in luck! The library is built in typescript, and includes advanced types for everything you need right out of the box.

Lets start by including the things we are going to need:

import createEngine, { DefaultLinkModel, DefaultNodeModel, DiagramModel } from '@projectstorm/react-diagrams'; import { CanvasWidget } from '@projectstorm/react-canvas-core';

Now we call createEngine which will bootstrap a DiagramEngine for us that contains all the defaults setup.

// create an instance of the engine with all the defaults const engine = createEngine();

Next, we create two nodes:

// node 1 const node1 = new DefaultNodeModel({ name: 'Node 1', color: 'rgb(0,192,255)', }); node1.setPosition(100, 100); let port1 = node1.addOutPort('Out'); // node 2 const node2 = new DefaultNodeModel({ name: 'Node 1', color: 'rgb(0,192,255)', }); node2.setPosition(100, 100); let port2 = node2.addOutPort('Out');

Now we link the two ports of both of the nodes:

// link them and add a label to the link const link = port1.link(port2); link.addLabel('Hello World!');

Great! Now we have setup a simple diagram. All thats left to do, is create a DiagramModel to contain everything, add all the elements to it, and then add it to the engine.

const model = new DiagramModel(); model.addAll(node1, node2, link); engine.setModel(model);

And then we render with React!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有