vue+echarts實(shí)現(xiàn)動(dòng)態(tài)折線圖的方法與注意
之前公司有個(gè)繪制實(shí)時(shí)盈利率折線圖的需求,實(shí)現(xiàn)的還不錯(cuò),今天來分享下vue+echarts實(shí)現(xiàn)動(dòng)態(tài)折線圖的方法。
實(shí)現(xiàn)代碼
<template> <div id="myChart"></div> </template> <script> import echarts from 'echarts' export default { name: 'DynamicLineChart', data () { return { // 實(shí)時(shí)數(shù)據(jù)數(shù)組 date: [], yieldRate: [], yieldIndex: [], // 折線圖echarts初始化選項(xiàng) echartsOption: { legend: { data: ['實(shí)際收益率', '大盤收益率'], }, xAxis: { name: '時(shí)間', nameTextStyle: { fontWeight: 600, fontSize: 18 }, type: 'category', boundaryGap: false, data: this.date, // 綁定實(shí)時(shí)數(shù)據(jù)數(shù)組 }, yAxis: { name: '實(shí)際收益率', nameTextStyle: { fontWeight: 600, fontSize: 18 }, type: 'value', scale: true, boundaryGap: ['15%', '15%'], axisLabel: { interval: 'auto', formatter: '{value} %' } }, tooltip: { trigger: 'axis', }, series: [ { name:'實(shí)際收益率', type:'line', smooth: true, data: this.yieldRate, // 綁定實(shí)時(shí)數(shù)據(jù)數(shù)組 }, { name:'大盤收益率', type:'line', smooth: true, data: this.yieldIndex, // 綁定實(shí)時(shí)數(shù)據(jù)數(shù)組 } ] } } }, mounted () { this.myChart = echarts.init(document.getElementById('myChart'), 'light'); // 初始化echarts, theme為light this.myChart.setOption(this.echartsOption); // echarts設(shè)置初始化選項(xiàng) setInterval(this.addData, 3000); // 每三秒更新實(shí)時(shí)數(shù)據(jù)到折線圖 }, methods: { // 獲取當(dāng)前時(shí)間 getTime : function() { var ts = arguments[0] || 0; var t, h, i, s; t = ts ? new Date(ts * 1000) : new Date(); h = t.getHours(); i = t.getMinutes(); s = t.getSeconds(); // 定義時(shí)間格式 return (h < 10 ? '0' + h : h) + ':' + (i < 10 ? '0' + i : i) + ':' + (s < 10 ? '0' + s : s); }, // 添加實(shí)時(shí)數(shù)據(jù) addData : function() { // 從接口獲取數(shù)據(jù)并添加到數(shù)組 this.$axios.get('url').then((res) => { this.yieldRate.push((res.data.actualProfitRate * 100).toFixed(3)); this.yieldIndex.push((res.data.benchmarkProfitRate * 100).toFixed(3)); this.date.push(this.getTime(Math.round(new Date().getTime() / 1000))); // 重新將數(shù)組賦值給echarts選項(xiàng) this.echartsOption.xAxis.data = this.date; this.echartsOption.series[0].data = this.yieldRate; this.echartsOption.series[1].data = this.yieldIndex; this.myChart.setOption(this.echartsOption); }); } } } </script> <style> // 設(shè)定寬高,不然超出windows會(huì)顯示不出來 #myChart{ width: 100%; height: 500px; margin: 0 auto; } </style>
要注意的有三點(diǎn):
- mounted中init并setOption初始化echarts
- echartsOption里的data綁定數(shù)組
- setInterval中要更新數(shù)組并重新將數(shù)組賦值給echarts選項(xiàng)
效果圖
總結(jié)
到此這篇關(guān)于vue+echarts實(shí)現(xiàn)動(dòng)態(tài)折線圖的文章就介紹到這了,更多相關(guān)vue+echarts動(dòng)態(tài)折線圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示
這篇文章主要介紹了Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue-cli2 構(gòu)建速度優(yōu)化的實(shí)現(xiàn)方法
這篇文章主要介紹了vue-cli2 構(gòu)建速度優(yōu)化的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01Vue電商網(wǎng)站首頁內(nèi)容吸頂功能實(shí)現(xiàn)過程
電商網(wǎng)站的首頁內(nèi)容會(huì)比較多,頁面比較長(zhǎng),為了能讓用戶在滾動(dòng)瀏覽內(nèi)容的過程中都能夠快速的切換到其它分類。需要分類導(dǎo)航一直可見,所以需要一個(gè)吸頂導(dǎo)航的效果。目標(biāo):完成頭部組件吸頂效果的實(shí)現(xiàn)2023-04-04深入探討Vue計(jì)算屬性與監(jiān)聽器的區(qū)別和用途
在Vue的開發(fā)中,計(jì)算屬性(Computed Properties)和監(jiān)聽器(Watchers)是兩種非常重要的概念,它們都用于響應(yīng)式地處理數(shù)據(jù)變化,本文將帶你深入了解計(jì)算屬性和監(jiān)聽器的區(qū)別,以及在何時(shí)使用它們,感興趣的朋友可以參考下2023-09-09vue實(shí)現(xiàn)百度搜索下拉提示功能實(shí)例
這篇文章主要介紹了vue實(shí)現(xiàn)百度搜索下拉提示功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06