欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue+Echarts實現(xiàn)繪制動態(tài)折線圖

 更新時間:2023年03月16日 11:41:20   作者:Saga Two  
這篇文章主要為大家詳細介紹了如何利用Vue和Echarts實現(xiàn)繪制動態(tài)折線圖,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

1 引入Echarts

1.1 安裝

使用如下命令通過 npm 安裝 ECharts

npm install echarts --save

注:本文安裝Echarts版本為:“echarts”: “5.2.1”

1.2 引入

安裝完成以后,可以將echarts全部引入,這樣一來,我們可以在該頁面使用echarts所有組件;引入代碼如下:

import * as echarts from "echarts";

1.3 基本使用

vue+Echarts基本使用請見:在Vue項目中引入 ECharts

2 動態(tài)折線圖

2.1 基本折線圖

折線圖得基本引入使用見:vue引入Echarts畫折線圖

2.2 動態(tài)折線圖

動態(tài)折線圖分兩種,一種為動渲染靜態(tài)數(shù)據(jù),產(chǎn)生動態(tài)變化得動畫效果的折線圖,另一種為動態(tài)渲染動態(tài)數(shù)據(jù)產(chǎn)生折線圖;一下給出我國人口總數(shù)20年變化示例。如圖所示:

實現(xiàn)以上效果最重要的就是利用Echarts中的動畫屬性animation;并使用animationDuration控制動畫時間;配置項代碼如下:

const optionFree = {
        xAxis: {
          data: this.xData
        },
        yAxis: {
          name: "人口(萬)",
          min: "120000",
          max: "150000"
        },
        animation: true,
        animationDuration: 20000,
        series: [
          {
            data: this.populationData,
            type: "line",
            smooth: true,
            endLabel: {
              show: true
            }
          }
        ]
      };

以上動圖效果中還是用了endLabel屬性控制在折線最后展示數(shù)值。

補充

除了上文的實現(xiàn)方法,小編還為大家整理了更多動態(tài)折線圖的實現(xiàn)方法,希望對大家有所幫助

實現(xiàn)代碼

<template>
 <div id="myChart"></div>
</template>

<script>
import echarts from 'echarts'
export default {
 name: 'DynamicLineChart',
 data () {
  return {
  	// 實時數(shù)據(jù)數(shù)組
   date: [],
   yieldRate: [],
   yieldIndex: [],
   // 折線圖echarts初始化選項
   echartsOption: {
    legend: {
     data: ['實際收益率', '大盤收益率'],
    },
    xAxis: {
     name: '時間',
     nameTextStyle: {
      fontWeight: 600,
      fontSize: 18
     },
     type: 'category',
     boundaryGap: false,
     data: this.date,	// 綁定實時數(shù)據(jù)數(shù)組
    },
    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,	// 綁定實時數(shù)據(jù)數(shù)組
     },
     {
      name:'大盤收益率',
      type:'line',
      smooth: true,
      data: this.yieldIndex,	// 綁定實時數(shù)據(jù)數(shù)組
     }
    ]
   }
  }
 },
 mounted () {
  this.myChart = echarts.init(document.getElementById('myChart'), 'light');	// 初始化echarts, theme為light
  this.myChart.setOption(this.echartsOption);	// echarts設置初始化選項
  setInterval(this.addData, 3000);	// 每三秒更新實時數(shù)據(jù)到折線圖
 },
 methods: {
 	// 獲取當前時間
  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();
   // 定義時間格式
   return (h < 10 ? '0' + h : h) + ':' + (i < 10 ? '0' + i : i) + ':' + (s < 10 ? '0' + s : s);
  },
  // 添加實時數(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選項
    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>
// 設定寬高,不然超出windows會顯示不出來
#myChart{
 width: 100%;
 height: 500px;
 margin: 0 auto;
}
</style>

要注意的有三點:

  • mounted中init并setOption初始化echarts
  • echartsOption里的data綁定數(shù)組
  • setInterval中要更新數(shù)組并重新將數(shù)組賦值給echarts選項

到此這篇關于Vue+Echarts實現(xiàn)繪制動態(tài)折線圖的文章就介紹到這了,更多相關Vue Echarts繪制動態(tài)折線圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論