之前公司有个绘制实时盈利率折线图的需求,实现的还不错,今天来分享下vue+echarts实现动态折线图的方法。
实现代码
<template> <div id="myChart"></div> </template> <script> import echarts from 'echarts' export default { name: 'DynamicLineChart', data () { return { // 实时数据数组 date: [], yieldRate: [], yieldIndex: [], // 折线图echarts初始化选项 echartsOption: { legend: { data: ['实际收益率', '大盘收益率'], }, xAxis: { name: '时间', nameTextStyle: { fontWeight: 600, fontSize: 18 }, type: 'category', boundaryGap: false, data: this.date, // 绑定实时数据数组 }, yAxis: { name: '实际收益率', nameTextStyle: { fontWeight: 600, fontSize: 18 }, type: 'value', scale: true, boundaryGap: ['15%', '15%'], axisLabel: { interval: 'auto', formatter: '{value} %' } }, tooltip: { trigger: 'axis', }, series: [ { name:'实际收益率', type:'line', smooth: true, data: this.yieldRate, // 绑定实时数据数组 }, { name:'大盘收益率', type:'line', smooth: true, data: this.yieldIndex, // 绑定实时数据数组 } ] } } }, mounted () { this.myChart = echarts.init(document.getElementById('myChart'), 'light'); // 初始化echarts, theme为light this.myChart.setOption(this.echartsOption); // echarts设置初始化选项 setInterval(this.addData, 3000); // 每三秒更新实时数据到折线图 }, methods: { // 获取当前时间 getTime : function() { var ts = arguments[0] || 0; var t, h, i, s; t = ts "" src="/UploadFiles/2021-04-02/202091145415118.gif">总结