AntV X6流程图绘制程序(官方示例纯javascript+html+css)

使用LogicFlow 编写的流程设计 logicflow: logicflow流程图

直接拷贝可用,需要引入官方的x6.js(AntV X6)  antvx6-flow: antv x6实现流程画图 (gitee.com)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="./js/x6.js"></script>

    <style type="text/css">
        #container {
            width: 100%;
            height: 600px;
        display: flex;
        border: 1px solid #dfe3e8;
        }
        #stencil {
        width: 180px;
        height: 100%;
        position: relative;
        border-right: 1px solid #dfe3e8;
        }
        #graph-container {
        width: calc(100% - 180px);
        height: 100%;
        }
        .x6-widget-stencil  {
        background-color: #fff;
        }
        .x6-widget-stencil-title {
        background-color: #fff;
        }
        .x6-widget-stencil-group-title {
        background-color: #fff !important;
        }
        .x6-widget-transform {
        margin: -1px 0 0 -1px;
        padding: 0px;
        border: 1px solid #239edd;
        }
        .x6-widget-transform > div {
        border: 1px solid #239edd;
        }
        .x6-widget-transform > div:hover {
        background-color: #3dafe4;
        }
        .x6-widget-transform-active-handle {
        background-color: #3dafe4;
        }
        .x6-widget-transform-resize {
        border-radius: 0;
        }
        .x6-widget-selection-inner {
        border: 1px solid #239edd;
        }
        .x6-widget-selection-box {
        opacity: 0;
        }
    </style>
</head>
<body>
    <div class="header">
        <button onclick="getJson()">点击</button>
    </div>
    <div id="container">
        <div id="stencil"></div>
        <div id="graph-container"></div>
    </div>
    
    <script>
        // #region 初始化画布
        const graph = new X6.Graph({
        container: document.getElementById('graph-container'),
        grid: true,
        mousewheel: {
            enabled: true,
            zoomAtMousePosition: true,
            modifiers: 'ctrl',
            minScale: 0.5,
            maxScale: 3,
        },
        connecting: {
            router: {
            name: 'manhattan',
            args: {
                padding: 1,
            },
            },
            connector: {
            name: 'rounded',
            args: {
                radius: 8,
            },
            },
            anchor: 'center',
            connectionPoint: 'anchor',
            allowBlank: false,
            snap: {
            radius: 20,
            },
            createEdge() {
            return new X6.Shape.Edge({
                attrs: {
                line: {
                    stroke: '#A2B1C3',
                    strokeWidth: 2,
                    targetMarker: {
                    name: 'block',
                    width: 12,
                    height: 8,
                    },
                },
                },
                zIndex: 0,
            })
            },
            validateConnection({ targetMagnet }) {
            return !!targetMagnet
            },
        },
        highlighting: {
            magnetAdsorbed: {
            name: 'stroke',
            args: {
                attrs: {
                fill: '#5F95FF',
                stroke: '#5F95FF',
                },
            },
            },
        },
        resizing: true,
        rotating: true,
        selecting: {
            enabled: true,
            rubberband: true,
            showNodeSelectionBox: true,
        },
        snapline: true,
        keyboard: true,
        clipboard: true,
        })
        // #endregion

        // #region 初始化 stencil
        const stencil = new X6.Addon.Stencil({
        title: '流程图',
        target: graph,
        stencilGraphWidth: 200,
        stencilGraphHeight: 180,
        collapsable: true,
        groups: [
            {
            title: '基础流程图',
            name: 'group1',
            },
            {
            title: '系统设计图',
            name: 'group2',
            graphHeight: 250,
            layoutOptions: {
                rowHeight: 70,
            },
            },
        ],
        layoutOptions: {
            columns: 2,
            columnWidth: 80,
            rowHeight: 55,
        },
        })
        document.getElementById('stencil').appendChild(stencil.container)
        // #endregion

        // #region 快捷键与事件
        // copy cut paste
        graph.bindKey(['meta+c', 'ctrl+c'], () => {
        const cells = graph.getSelectedCells()
        if (cells.length) {
            graph.copy(cells)
        }
        return false
        })
        graph.bindKey(['meta+x', 'ctrl+x'], () => {
        const cells = graph.getSelectedCells()
        if (cells.length) {
            graph.cut(cells)
        }
        return false
        })
        graph.bindKey(['meta+v', 'ctrl+v'], () => {
        if (!graph.isClipboardEmpty()) {
            const cells = graph.paste({ offset: 32 })
            graph.cleanSelection()
            graph.select(cells)
        }
        return false
        })

        //undo redo
        graph.bindKey(['meta+z', 'ctrl+z'], () => {
        if (graph.history.canUndo()) {
            graph.history.undo()
        }
        return false
        })
        graph.bindKey(['meta+shift+z', 'ctrl+shift+z'], () => {
        if (graph.history.canRedo()) {
            graph.history.redo()
        }
        return false
        })

        // select all
        graph.bindKey(['meta+a', 'ctrl+a'], () => {
        const nodes = graph.getNodes()
        if (nodes) {
            graph.select(nodes)
        }
        })

        //delete
        graph.bindKey('backspace', () => {
        const cells = graph.getSelectedCells()
        if (cells.length) {
            graph.removeCells(cells)
        }
        })

        // zoom
        graph.bindKey(['ctrl+1', 'meta+1'], () => {
        const zoom = graph.zoom()
        if (zoom < 1.5) {
            graph.zoom(0.1)
        }
        })
        graph.bindKey(['ctrl+2', 'meta+2'], () => {
        const zoom = graph.zoom()
        if (zoom > 0.5) {
            graph.zoom(-0.1)
        }
        })

        
        // 控制连接桩显示/隐藏
        const showPorts = (ports, show) => {
        for (let i = 0, len = ports.length; i < len; i = i + 1) {
            ports[i].style.visibility = show ? 'visible' : 'hidden'
        }
        }
        graph.on('node:mouseenter', () => {
        const container = document.getElementById('graph-container')
        const ports = container.querySelectorAll(
            '.x6-port-body',
        )
        showPorts(ports, true)
        })
        graph.on('node:mouseleave', () => {
        const container = document.getElementById('graph-container')
        const ports = container.querySelectorAll(
            '.x6-port-body',
        )
        showPorts(ports, false)
        })
        
        // #endregion

        // #region 初始化图形
        const ports = {
        groups: {
            top: {
            position: 'top',
            attrs: {
                circle: {
                r: 4,
                magnet: true,
                stroke: '#5F95FF',
                strokeWidth: 1,
                fill: '#fff',
                style: {
                    visibility: 'hidden',
                },
                },
            },
            },
            right: {
            position: 'right',
            attrs: {
                circle: {
                r: 4,
                magnet: true,
                stroke: '#5F95FF',
                strokeWidth: 1,
                fill: '#fff',
                style: {
                    visibility: 'hidden',
                },
                },
            },
            },
            bottom: {
            position: 'bottom',
            attrs: {
                circle: {
                r: 4,
                magnet: true,
                stroke: '#5F95FF',
                strokeWidth: 1,
                fill: '#fff',
                style: {
                    visibility: 'hidden',
                },
                },
            },
            },
            left: {
            position: 'left',
            attrs: {
                circle: {
                r: 4,
                magnet: true,
                stroke: '#5F95FF',
                strokeWidth: 1,
                fill: '#fff',
                style: {
                    visibility: 'hidden',
                },
                },
            },
            },
        },
        items: [
            {
            group: 'top',
            },
            {
            group: 'right',
            },
            {
            group: 'bottom',
            },
            {
            group: 'left',
            },
        ],
        }

        X6.Graph.registerNode(
        'custom-rect',
        {
            inherit: 'rect',
            width: 66,
            height: 36,
            attrs: {
            body: {
                strokeWidth: 1,
                stroke: '#5F95FF',
                fill: '#EFF4FF',
            },
            text: {
                fontSize: 12,
                fill: '#262626',
            },
            },
            ports: { ...ports },
        },
        true,
        )

        X6.Graph.registerNode(
        'custom-polygon',
        {
            inherit: 'polygon',
            width: 66,
            height: 36,
            attrs: {
            body: {
                strokeWidth: 1,
                stroke: '#5F95FF',
                fill: '#EFF4FF',
            },
            text: {
                fontSize: 12,
                fill: '#262626',
            },
            },
            ports: {
            ...ports,
            items: [
                {
                group: 'top',
                },
                {
                group: 'bottom',
                },
            ],
            },
        },
        true,
        )

        X6.Graph.registerNode(
        'custom-circle',
        {
            inherit: 'circle',
            width: 45,
            height: 45,
            attrs: {
            body: {
                strokeWidth: 1,
                stroke: '#5F95FF',
                fill: '#EFF4FF',
            },
            text: {
                fontSize: 12,
                fill: '#262626',
            },
            },
            ports: { ...ports },
        },
        true,
        )

        X6.Graph.registerNode(
        'custom-image',
        {
            inherit: 'rect',
            width: 52,
            height: 52,
            markup: [
            {
                tagName: 'rect',
                selector: 'body',
            },
            {
                tagName: 'image',
            },
            {
                tagName: 'text',
                selector: 'label',
            },
            ],
            attrs: {
            body: {
                stroke: '#5F95FF',
                fill: '#5F95FF',
            },
            image: {
                width: 26,
                height: 26,
                refX: 13,
                refY: 16,
            },
            label: {
                refX: 3,
                refY: 2,
                textAnchor: 'left',
                textVerticalAnchor: 'top',
                fontSize: 12,
                fill: '#fff',
            },
            },
            ports: { ...ports },
        },
        true,
        )

        const r1 = graph.createNode({
        shape: 'custom-rect',
        label: '开始',
        attrs: {
            body: {
            rx: 20,
            ry: 26,
            },
        },
        })
        const r2 = graph.createNode({
        shape: 'custom-rect',
        label: '过程',
        })
        const r3 = graph.createNode({
        shape: 'custom-rect',
        attrs: {
            body: {
            rx: 6,
            ry: 6,
            },
        },
        label: '可选过程',
        })
        const r4 = graph.createNode({
        shape: 'custom-polygon',
        attrs: {
            body: {
            refPoints: '0,10 10,0 20,10 10,20',
            },
        },
        label: '决策',
        })
        const r5 = graph.createNode({
        shape: 'custom-polygon',
        attrs: {
            body: {
            refPoints: '10,0 40,0 30,20 0,20',
            },
        },
        label: '数据',
        })
        const r6 = graph.createNode({
        shape: 'custom-circle',
        label: '连接',
        })
        stencil.load([r1, r2, r3, r4, r5, r6], 'group1')

        const imageShapes = [
        {
            label: 'Client',
            image:
            'https://gw.alipayobjects.com/zos/bmw-prod/687b6cb9-4b97-42a6-96d0-34b3099133ac.svg',
        },
        {
            label: 'Http',
            image:
            'https://gw.alipayobjects.com/zos/bmw-prod/dc1ced06-417d-466f-927b-b4a4d3265791.svg',
        },
        {
            label: 'Api',
            image:
            'https://gw.alipayobjects.com/zos/bmw-prod/c55d7ae1-8d20-4585-bd8f-ca23653a4489.svg',
        },
        {
            label: 'Sql',
            image:
            'https://gw.alipayobjects.com/zos/bmw-prod/6eb71764-18ed-4149-b868-53ad1542c405.svg',
        },
        {
            label: 'Clound',
            image:
            'https://gw.alipayobjects.com/zos/bmw-prod/c36fe7cb-dc24-4854-aeb5-88d8dc36d52e.svg',
        },
        {
            label: 'Mq',
            image:
            'https://gw.alipayobjects.com/zos/bmw-prod/2010ac9f-40e7-49d4-8c4a-4fcf2f83033b.svg',
        },
        ]
        const imageNodes = imageShapes.map((item) =>
        graph.createNode({
            shape: 'custom-image',
            label: item.label,
            attrs: {
            image: {
                'xlink:href': item.image,
            },
            },
        }),
        )
        stencil.load(imageNodes, 'group2')

        function getJson()
        {
            var json = graph.toJSON();
            console.log(json);
        }
      </script>
</body>
</html>
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue是一种流行的JavaScript框架,Vue 2是其第二个版本。AntV X6是一个基于Vue 2的图表绘制库,可以用于绘制各种类型的图表,包括树形流程图。 要在Vue 2项目中使用AntV X6绘制树形流程图,首先需要安装和引入AntV X6库。可以通过npm或yarn进行安装: npm install @antv/x6 或者 yarn add @antv/x6 然后,可以在Vue的组件中使用AntV X6。首先,引入`Graph`和`Node`组件: import { Graph, Node } from '@antv/x6-vue' 接下来,在Vue的模板中添加一个`Graph`组件,并设置一些必要的属性: ``` <template> <div> <Graph :initConfig="graphConfig"> <Node :shape="nodeShape" :x="nodeX" :y="nodeY" :width="nodeWidth" :height="nodeHeight" /> </Graph> </div> </template> <script> export default { data() { return { graphConfig: { width: 800, height: 600, gridSize: 10, background: { color: '#f8f8f8' }, scroller: { enabled: true, pannable: true } }, nodeShape: 'rect', nodeX: 100, nodeY: 100, nodeWidth: 80, nodeHeight: 40 } } } </script> ``` 在上面的代码中,创建了一个Graph组件和一个Node组件。Graph组件定义了整个绘图区域的配置,例如大小、网格和背景颜色等。Node组件定义了一个矩形节点的形状、位置和尺寸。 通过设置Node组件的属性,可以绘制更多的节点,并使用适当的形状、位置和尺寸。 以上是一个简单的示例,展示了如何在Vue 2项目中使用AntV X6绘制树形流程图。通过修改组件属性,可以创建更复杂的图表,并通过AntV X6的其他功能来增强图表的交互性和可视化效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值