您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用Vue+Echarts實(shí)現(xiàn)繪制動(dòng)態(tài)折線圖的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
使用如下命令通過(guò) npm 安裝 ECharts
npm install echarts --save
注:本文安裝Echarts版本為:“echarts”: “5.2.1”
安裝完成以后,可以將echarts全部引入,這樣一來(lái),我們可以在該頁(yè)面使用echarts所有組件;引入代碼如下:
import * as echarts from "echarts";
vue+Echarts基本使用請(qǐng)見(jiàn):在Vue項(xiàng)目中引入 ECharts
折線圖得基本引入使用見(jiàn):vue引入Echarts畫(huà)折線圖
動(dòng)態(tài)折線圖分兩種,一種為動(dòng)渲染靜態(tài)數(shù)據(jù),產(chǎn)生動(dòng)態(tài)變化得動(dòng)畫(huà)效果的折線圖,另一種為動(dòng)態(tài)渲染動(dòng)態(tài)數(shù)據(jù)產(chǎn)生折線圖;一下給出我國(guó)人口總數(shù)20年變化示例。如圖所示:
實(shí)現(xiàn)以上效果最重要的就是利用Echarts中的動(dòng)畫(huà)屬性animation;并使用animationDuration控制動(dòng)畫(huà)時(shí)間;配置項(xiàng)代碼如下:
const optionFree = { xAxis: { data: this.xData }, yAxis: { name: "人口(萬(wàn))", min: "120000", max: "150000" }, animation: true, animationDuration: 20000, series: [ { data: this.populationData, type: "line", smooth: true, endLabel: { show: true } } ] };
以上動(dòng)圖效果中還是用了endLabel屬性控制在折線最后展示數(shù)值。
除了上文的實(shí)現(xiàn)方法,小編還為大家整理了更多動(dòng)態(tài)折線圖的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助
實(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í)際收益率', '大盤(pán)收益率'], }, 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:'大盤(pán)收益率', 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ì)顯示不出來(lái) #myChart{ width: 100%; height: 500px; margin: 0 auto; } </style>
要注意的有三點(diǎn):
mounted中init并setOption初始化echarts
echartsOption里的data綁定數(shù)組
setInterval中要更新數(shù)組并重新將數(shù)組賦值給echarts選項(xiàng)
以上就是“怎么使用Vue+Echarts實(shí)現(xiàn)繪制動(dòng)態(tài)折線圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。