echarts+流程图

前言:

         echarts+流程图

效果图:

实现源码:

var title = [
    // 第零层
    {
        label: '0',
        warn: 5,
        error: 8,
        value: 2
    },
    // 第一层
    {
        label: '1-0',
        warn: 5,
        error: 8,
        value: 5
    },
    {
        label: '1-1',
        warn: 5,
        error: 8,
        value: 7
    },
    {
        label: '1-2',
        warn: 5,
        error: 8,
        value: 7
    },
    {
        label: '1-3',
        warn: 5,
        error: 8,
        value: 7
    },
    // 第二层
    {
        label: '2-0',
        warn: 5,
        error: 8,
        value: 2
    },
    {
        label: '2-1',
        warn: 5,
        error: 8,
        value: 2
    },
    {
        label: '2-2',
        warn: 5,
        error: 8,
        value: 2
    },
    {
        label: '2-3',
        warn: 5,
        error: 8,
        value: 2
    },
    {
        label: '2-4',
        warn: 5,
        error: 8,
        value: 2
    },
    // 第三层
    {
        label: '3-0',
        warn: 5,
        error: 8,
        value: 2
    },
    {
        label: '3-1',
        warn: 5,
        error: 8,
        value: 2
    },
    {
        label: '3-2',
        warn: 5,
        error: 8,
        value: 2
    },
];
var colorMap = {
    normal: '#18a849',
    warn: '#FFC125',
    error: '#FF0000',
};

colorFunction = function(obj) {
    if (obj.value < obj.warn) {
        return colorMap.normal;
    } else if (obj.warn <= obj.value && obj.value < obj.error) {
        return colorMap.warn;
    } else if (obj.error <= obj.value) {
        return colorMap.error;
    }
};
//具体的绘制流程图的方法
option = {
    tooltip: {
        trigger: 'item',
        formatter: function(para) {
            if (para.name != 'x' || para.name != 'y') {
                return para.name;
            } else {
                //其他的都正式显示,自己是什么就显示什么。
                return '';
            }
        }
    },
    animationDurationUpdate: 1500,
    animationEasingUpdate: 'quinticInOut',
    textStyle: {
        color: '#000'
    },
    series: [{
        type: 'graph',
        tooltip: {
            backgroundColor: 'skyblue',
            //formatter: "{b} <br/>{a} : {c} h "
        },
        layout: 'none',
        symbolSize: 10,
        roam: false,
        label: {
            normal: {
                show: true,
                position: 'inside',
                //offset: [0,-60],//居上 20
                textStyle: {
                    fontSize: 12,
                    color: '#fff',
                    fontWeight: 'BOLD',
                },
            }
        },
        edgeSymbol: ['circle', 'arrow'],
        edgeSymbolSize: [4, 10],
        edgeLabel: {
            normal: {
                textStyle: {
                    fontSize: 18
                }
            }
        },
        //注意,所有节点的位置都是根据自己设置的x, y坐标来设置的
        data: [
            // 零层
            {
                name: title[0].label,
                x: 300,
                y: 0,
                value: title[0],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[0]),
                    }
                }
            },
            // 一层
            {
                name: title[1].label,
                x: 0,
                y: 200,
                value: title[1],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[1]),
                    }
                }
            },
            {
                name: title[2].label,
                x: 150,
                y: 200,
                value: title[2],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[2]),
                    }
                }
            },
            {
                name: title[3].label,
                x: 350,
                y: 200,
                value: title[3],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[3]),
                    }
                }
            },
            {
                name: title[4].label,
                x: 600,
                y: 200,
                value: title[4],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[4]),
                    }
                }
            },
            // 二层
            {
                name: title[5].label,
                x: 0,
                y: 400,
                value: title[5],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[5]),
                    }
                }
            },
            {
                name: title[6].label,
                x: 150,
                y: 400,
                value: title[6],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[6]),
                    }
                }
            },
            {
                name: title[7].label,
                x: 300,
                y: 400,
                value: title[7],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[7]),
                    }
                }
            },
            {
                name: title[8].label,
                x: 450,
                y: 400,
                value: title[8],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[8]),
                    }
                }
            },
            {
                name: title[9].label,
                x: 600,
                y: 400,
                value: title[9],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[9]),
                    }
                }
            },
            // 三层
            {
                name: title[10].label,
                x: 0,
                y: 600,
                value: title[10],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[10]),
                    }
                }
            },
            {
                name: title[11].label,
                x: 150,
                y: 600,
                value: title[11],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[11]),
                    }
                }
            },
            {
                name: title[12].label,
                x: 450,
                y: 600,
                value: title[12],
                symbolSize: 50,
                itemStyle: {
                    normal: {
                        color: colorFunction(title[12]),
                    }
                }
            },

            // 一层
            {
                name: '1',
                x: 300,
                y: 100,
                symbolSize: 0,
            },
            {
                name: '2',
                x: 150,
                y: 100,
                symbolSize: 0,
            },
            {
                name: '3',
                x: 0,
                y: 100,
                symbolSize: 0,
            },
            {
                name: '4',
                x: 350,
                y: 100,
                symbolSize: 0,
            },
            {
                name: '5',
                x: 600,
                y: 100,
                symbolSize: 0,
            },
            // 二层
            {
                name: '21',
                x: 0,
                y: 300,
                symbolSize: 0,
            },
            {
                name: '22',
                x: 150,
                y: 300,
                symbolSize: 0,
            },
            {
                name: '23',
                x: 300,
                y: 300,
                symbolSize: 0,
            },
            {
                name: '24',
                x: 350,
                y: 300,
                symbolSize: 0,
            },
            {
                name: '25',
                x: 450,
                y: 300,
                symbolSize: 0,
            },
            {
                name: '26',
                x: 600,
                y: 300,
                symbolSize: 0,
            },
            // 三层
            {
                name: '31',
                x: 0,
                y: 500,
                symbolSize: 0,
            },
            {
                name: '32',
                x: 150,
                y: 500,
                symbolSize: 0,
            },
            {
                name: '33',
                x: 300,
                y: 500,
                symbolSize: 0,
            },
            {
                name: '34',
                x: 450,
                y: 500,
                symbolSize: 0,
            },
        ],
        // links: [],
        //这是点与点之间的连接关系
        links: [
            // 零层
            {
                source: title[0].label,
                target: '1',
            },
            // 一层
            {
                source: '3',
                target: title[1].label,
            },
            {
                source: '2',
                target: title[2].label,
            },
            {
                source: '4',
                target: title[3].label,
            },
            {
                source: '5',
                target: title[4].label,
            },
            // 二层
            {
                source: title[3].label,
                target: '24',
            },
            {
                source: '21',
                target: title[5].label,
            },
            {
                source: '22',
                target: title[6].label,
            },
            {
                source: '23',
                target: title[7].label,
            },
            {
                source: '25',
                target: title[8].label,
            },
            {
                source: '26',
                target: title[9].label,
            },
            // 三层
            {
                source: title[7].label,
                target: '33',
            },
            {
                source: '31',
                target: title[10].label,
            },
            {
                source: '32',
                target: title[11].label,
            },
            {
                source: '34',
                target: title[12].label,
            },

            // 一层
            {
                source: '1',
                target: '2',
                symbol: 'none',
            },
            {
                source: '2',
                target: '3',
                symbol: 'none',
            },
            {
                source: '1',
                target: '4',
                symbol: 'none',
            },
            {
                source: '4',
                target: '5',
                symbol: 'none',
            },
            // 二层
            {
                source: '21',
                target: '22',
                symbol: 'none',
            },
            {
                source: '22',
                target: '23',
                symbol: 'none',
            },
            {
                source: '23',
                target: '24',
                symbol: 'none',
            },
            {
                source: '24',
                target: '25',
                symbol: 'none',
            },
            {
                source: '25',
                target: '26',
                symbol: 'none',
            },
            // 三层
            {
                source: '31',
                target: '32',
                symbol: 'none',
            },
            {
                source: '32',
                target: '33',
                symbol: 'none',
            },
            {
                source: '33',
                target: '34',
                symbol: 'none',
            },
        ],
        //线条的颜色
        lineStyle: {
            normal: {
                opacity: 0.9,
                color: '#53B5EA',
                type: 'dashed',
                width: 1
            }
        }
    }]
};

更多入口

### 在 Vue 中结合 ECharts 绘制流程图 要在 Vue 中使用 ECharts 实现流程图,可以按照以下方式操作: #### 1. 安装依赖 通过 `npm` 或 `yarn` 安装 ECharts 库: ```bash npm install echarts --save ``` 或者使用 Yarn: ```bash yarn add echarts ``` #### 2. 引入 ECharts 可以选择全局引入或局部引入。 ##### 全局引入(适用于整个项目都需要使用 ECharts) 在项目的入口文件 `main.js` 中进行如下设置: ```javascript import * as echarts from 'echarts'; Vue.prototype.$echarts = echarts; ``` 这样可以在任何组件中通过 `this.$echarts` 访问到 ECharts[^4]。 ##### 局部引入(仅限于特定组件) 如果只需要在一个组件中使用 ECharts,则可以直接在该组件中导入: ```javascript const echarts = require('echarts'); // 或者 ES6 模块语法 import * as echarts from 'echarts'; ``` #### 3. 准备 DOM 容器 在模板中定义一个具有固定宽度和高度的容器用于渲染图表。例如: ```html <template> <div> <h3>流程图</h3> <div ref="flowChart" style="width: 100%; height: 500px;"></div> </div> </template> ``` #### 4. 初始化并配置 ECharts 流程图 以下是完整的代码示例,展示如何在 Vue 中绘制流程图: ```javascript <script> export default { data() { return { chartInstance: null, }; }, mounted() { this.initFlowChart(); }, beforeDestroy() { if (this.chartInstance) { this.chartInstance.dispose(); // 销毁实例以释放资源 } }, methods: { initFlowChart() { const domElement = this.$refs.flowChart; if (!domElement) { console.error('DOM element not found!'); return; } this.chartInstance = echarts.init(domElement); const option = { title: { text: '流程图', left: 'center', }, tooltip: {}, series: [ { type: 'graph', // 设置图形类型为 graph 表示关系图/流程图 layout: 'force', // 力引导布局 symbolSize: 50, // 节点大小 roam: true, // 是否开启鼠标缩放和平移漫游 label: { show: true, position: 'inside', }, edgeSymbol: ['circle', 'arrow'], // 边缘箭头样式 edgeLabel: { normal: { show: true, textStyle: { fontSize: 10, }, }, }, force: { repulsion: 100, // 节点之间的斥力因子 }, data: [ { name: '节点A', category: 0 }, { name: '节点B', category: 1 }, { name: '节点C', category: 2 }, { name: '节点D', category: 3 }, ], links: [ { source: '节点A', target: '节点B' }, { source: '节点B', target: '节点C' }, { source: '节点C', target: '节点D' }, ], categories: [ { name: '类别1' }, { name: '类别2' }, { name: '类别3' }, { name: '类别4' }, ], }, ], }; this.chartInstance.setOption(option); }, }, }; </script> <style scoped> /* 可选样式 */ </style> ``` 以上代码展示了如何创建一个简单的流程图,并设置了基本属性如节点名称、连接线以及分类信息[^1]。 --- ### 注意事项 - **性能优化**:对于复杂的大规模数据集,建议调整 `repulsion` 和其他参数以获得更好的显示效果。 - **动态更新**:可以通过监听事件或其他机制实时修改 `option` 数据源,调用 `setOption()` 方法刷新视图[^3]。 - **清理工作**:当组件被销毁时记得调用 `dispose()` 来释放内存占用[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值