1.1.6 • Published 4 years ago

flowchart-core v1.1.6

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

GitHub stars GitHub issues GitHub forks GitHub last commit license

NPM NPM

flowchart-core

A tiny svg javascript lib, Ability to quickly draw flowchart and topology graph.

查看简体中文

Advantage

Two configurable modes: link-mode and render-mode.

  • link-mode: quickly start flowchart config.

  • render-mode: quickly drawing topological relationships.

Install

NPM

 $ npm install flowchart-core

Yarn

 $ yarn flowchart-core

Layout

Topology

Check Demo | Get Usage

// create topology graph, just use one statement.
import { RSGraph } from 'flowchart-core';
const nodes = [
    {
        id: 'root',
        children: ['node1', 'node2'],
        title: 'start',
        desc: '1',
        parent: null,
    },
    {
        id: 'node1',
        children: [],
        title: 'doing',
        desc: '2',
        parent: 'root',
    },
    {
        id: 'node2',
        children: [],
        title: 'end',
        desc: '3',
        parent: 'root',
    },
];

// create node div dom element. this just a case.
nodes.forEach(node => {
    const { title, desc } = node;
    const body = document.querySelector('body');
    const div = document.createElement('div');
    div.setAttribute('data-rsgraph-id', node.id);
    div.setAttribute('class', 'item');
    div.innerHTML = `<div class="desc">${desc}</div><div class="title">${title}</div>`;
    body.appendChild(div);
});

const config = {
    data: nodes,
    zoom: true, // default is false
    direction: 'x-axis', // x-axis || y-axis. default value is 'y-axis'
    coreOptions: {
        style: {
            borderTop: '1px dashed #000',
            overflow: 'scroll',
        },
        line: {
            style: {
                stroke: 'deepskyblue',
            },
            arrow: {
                style: {
                    fill: '#888',
                },
                viewBox: '0 0 18 18',
            },
        },
        linkDot: {
            display: 'none', // default is display: none
        },
        mode: 'link-mode', // set link-mode will not work.
    },
};

const graph = new RSGraph('#svg-container', config);

warning Add data-rsgraph-id attribute to the DOM element of node before using it.

Flowchart

Check Demo

Mount

mount with global
<!-- html -->
<svg id="svg-container"></svg>
import { Core, Node } from 'flowchart-core';
// initial Core.
const core = new Core('#svg-container', {
    style: {
        width: 1000,
        height: 600,
        border: '1px dashed #000',
    },
    mode: 'link-mode',
});
// define node container width & height.
const width = 140;
const height = 70;
// initial Node.
const node = new Node({
    position: {
        x: 100,
        y: 50,
    },
    style: {
        width,
        height,
        strokeWidth: 2,
        stroke: 'black',
        cursor: 'grab',
        rx: 10,
    },
});

// zoom graph
core.zoom();

// add node to container
core.addNode(node);

API Reference

new Core(selectors, options)

proptypedescmust
selectorsCSS selectorsSvg DOM selector name1
optionscoreOptionscore config options1

coreOptions

  • Type: { style: {}, line: {}, linkDot: {}, mode: 'render-mode' }

  • Arguments:

    proptypedescmust
    stylestylesheetcss style1
    linelineObjectlink path config0
    linkDotlinkDotObjectlink dot config0
    modeStringlink-mode. render-mode.1
##### lineObject

link line props.

| prop  | type                                | desc                | must |
| :---- | :---------------------------------- | :------------------ | :--- |
| style | stylesheet                        | _css style_         | 0  |
| arrow | [arrowObject](#arrowObject) | _line arrow config_ | 0  |

##### linkDotObject

> `Only` the following table styles can be used.

| prop        | type           | desc                        | must |
| :---------- | :------------- | :-------------------------- | :--- |
| r           | radius       | \<circle> _attr `r` radius_ | 0  |
| fill        | fill color   | _color_                     | 0  |
| stroke      | stroke color | _color_                     | 0  |
| strokeWidth | stroke width | _px_                        | 0  |
| display     | css display  | _display value_             | 0  |

##### arrowObject

| prop    | type         | desc                                                                                | must |
| :------ | :----------- | :---------------------------------------------------------------------------------- | :--- |
| style   | `stylesheet` | _css style_                                                                         | 0  |
| viewBox | `String`     | _[svg viewBox](https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/viewBox)_ | 0  |
  • Usage:

    const options = {
        style: {
            width: '100vw',
            height: '100vh',
            border: '1px dashed #000',
        },
        line: {
            style: {
                stroke: 'deepskyblue',
            },
            arrow: {
                style: {
                    fill: 'deepskyblue',
                },
                viewBox: '0 0 18 18',
                d: 'M1,2 L8,6 L1,10 Z',
            },
        },
        linkDot: {
            r: 2,
            fill: 'deepskyblue',
            stroke: 'deepskyblue',
            strokeWidth: 2,
        },
    };

new Node(config)

proptypedescmust
confignodeConfignode config1

nodeConfig

  • Type: { style: {}, position: { x, y }, html: { meta } }

  • Arguments:

    proptypedescmust
    stylestylesheetcss style1
    positionpositionObjectnode position in svg1
    htmlhtmlObject11
    positionObject
    proptypedescmust
    xx axisMouseEvent.clientX1
    yy axisMouseEvent.clientY1
    htmlObject
    proptypedescmust
    metaDomInstanceDom element1
  • Usage:

    const config = {
        position: {
            // node position in svg.
            x: 100,
            y: 150,
        },
        style: {
            width: 140,
            height: 70,
        },
        html: {
            meta: '<div>...</div>', // html template.
        },
    };

new Edge(config)

proptypedescmust
configedgeConfigedge config0

edgeConfig

  • Type: { style: {} }

  • Arguments:

    proptypedescmust
    stylestylesheetcss style1
  • Usage:

    const config = {
        style: {
            stroke: 'deepskyblue',
        },
    };

new RSGraph(selectors, config)

proptypedescmust
selectorsCSS selectorscss selectors1
configrsGraphConfigrsgraph config0

rsGraphConfig

  • Type: { data: [], zoom: true, coreOptions: {} }

  • Arguments:

    proptypedescmust
    dataArraynode relation data1
    zoomBooleanzoom in or zoom out0
    directionStringchange the direction of topological0
    coreOptionscoreOptionscore config options0
  • Usage:

    const config = {
        data: nodes,
        zoom: true,
        direction: 'x-axis', // x-axis || y-axis. default value is 'y-axis'
        coreOptions: {
            style: {
                borderTop: '1px dashed #000',
                overflow: 'scroll',
            },
            linkDot: {
                display: 'none', // default is display: none
            },
            mode: 'link-mode', // set link-mode will not work.
        },
    };

Methods

Core Methods

proptypedesc
addNode(node)Functionadd node to svg container
addEdge(edge, config)Functionadd a path to svg container to describe the relationship between nodes, just use in render mode, config: { source, target, dotLink, dotEndLink }
deleteNode(node)Functiondelete node data and remove node from svg container
deleteEdge(edge)Functiondelete edge data and remove edge from svg container
showSvgElement(svgElement, type)Functionshow a svg element. enum value is 'node' or 'edge'
hiddenSvgElement(svgElement, type)Functionhidden a svg element. enum value is 'node' or 'edge'
zoom()Functionmake graph zoom in or zoom out. drag-and-drop are not supported after called zoom()
zoomClose()Functionclose zoom,Core.mode value back to Core.options.mode value

Usage:

// eg. how to appendChild a edge in core instance.
const coreInstance = new Core('#svg-container', { ... });
const edgeInstance = new Edge({ ... });
coreInstance.addEdge(edgeInstance, {
    source: sourceNode.id,
    target: targetNode.id,
    dotLink: 'bottom',
    dotEndLink: 'top'
});

Node Methods

proptypedesc
changePosition(position)Functionchange node position, attribute is a positionObject

Usage:

// eg. how to change the position attribute.
const coreInstance = new Core('#svg-container', { ... });
const nodeInstance = new Node({ ... });
nodeInstance.changePosition({
    x: 130,
    y: 100,
});

Class Attributes

More complex effects can be achieved through these exposure methods.

Core

  • Arguments:

    proptypedesc
    containerSvgElement<svg>Svg Dom
    nodesArray<Object>node dom list
    edgesArray<Object>edge dom list
    nodeGSvgElement<g>\ tag. nodes container
    edgeGSvgElement<g>\ tag. edges container

Node

  • Arguments:

    proptypedesc
    idNumberunique node id
    nodeSvgElement<g>node container \. As real node to use. Accessible to all DOM attribute values on it
    htmlStringhtml element embed in node inside
    stylestylesheetnode style
  • Usage:

    // eg. how to make a node instance visible or hidden.
    const coreInstance = new Core('#svg-container', { ... });
    const nodeInstance = new Node({
        position: {
            x: 100,
            y: 100,
        },
        style: {
            width,
            height,
        },
    });
    const operatorType = 'node';
    // hidden.
    coreInstance.hiddenSvgElement(nodeInstance, operatorType);
    // visible.
    coreInstance.showSvgElement(nodeInstance, operatorType);

Edge

  • Arguments:

    proptypedesc
    idNumberunique edge id
    edgeSvgElement<g>edge container \
    sourceNumbersource node id
    targetNumbertarget node id
    dotLinkStringnode start link dot's position: top | bottom| left | right
    dotEndLinkStringnode end link dot's position: top | bottom | left | right
    lineDataStringlink path data. \ prop d
  • Usage:

    // eg. create edge instance & append child on svg
    const coreInstance = new Core('#svg-container', { ... })
    const edgeInstance = new Edge({
        style: {
            stroke: 'deepskyblue',
        },
    });
    // key step.
    coreInstance.addEdge(edgeInstance, {
        source: sourceNode.id,
        target: targetNode.id,
        dotLink: 'bottom',
        dotEndLink: 'top'
    });

Example

Recommand to check more example (how to embed self define div node)

  • self define html embed node

eg-graph

  • flowchart config.

eg-graph-1

Author

TimRChen

1.1.6

4 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago