最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue中使用echarts的方法實(shí)例詳解

 更新時(shí)間:2023年05月10日 09:13:16   作者:Mr.Aholic  
這篇文章主要介紹了vue中使用echarts的方法,結(jié)合實(shí)例形式詳細(xì)分析了vue中使用echarts的包安裝、引入、生命周期函數(shù)元素掛載等相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下

1、安裝

npm install echarts --save

2、在vue中引入(全局引入)

// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

3、在vue中的使用

需要用到echart的地方先設(shè)置一個(gè)div的id、寬高

提示:
可以在一個(gè)頁(yè)面中引入多個(gè)數(shù)據(jù)報(bào)表模板
使用div進(jìn)行位置的排版放置

4、模板代碼放在哪個(gè)位置

重點(diǎn)注意:其中const option = { }就是我們需要引進(jìn)echart圖表的代碼

<template>
  <div>
    <div ref="chart"></div>
  </div>
</template>

要在mounted生命周期函數(shù)中實(shí)例化echarts對(duì)象。確保dom元素已經(jīng)掛載到頁(yè)面中。

mounted(){
    this.getEchartData()  
    },
   methods: {
    getEchartData() {
      const chart = this.$refs.chart
      if (chart) {
        const myChart = this.$echarts.init(chart)
        const option = {...}
        myChart.setOption(option)
        window.addEventListener("resize", function() {
          myChart.resize()
        })
      }
       this.$on('hook:destroyed',()=>{
         window.removeEventListener("resize", function() {
          myChart.resize();
        });
        })
    }
  }

5、完整的一個(gè)vue頁(yè)面實(shí)例:

<template>
  <div>
    <div ref="chart"></div>
    <div ref="chart1"></div>
  </div>
</template>
<script>
  export default {
    data() {
    },
    mounted() {
      this.getEchartData()
      this.getEchartData1()
    },
    methods: {
      getEchartData() {
        const chart = this.$refs.chart
        if (chart) {
          const myChart = this.$echarts.init(chart)
          const option = { legend: {},
            tooltip: {},
            dataset: {
              source: [
                ['訂單', 43.3, 85.8],
                ['訂單1', 83.1, 73.4],
                ['訂單2', 86.4, 65.2],
                ['訂單3', 72.4, 53.9],
                ['訂單4', 82.4, 53.9],
                ['訂單5', 42.4, 53.9],
                ['訂單6', 72.4, 53.9],
                ['訂單7', 72.4, 53.9]
              ]
            },
            xAxis: { type: 'category' },
            yAxis: {},
            series: [ { type: 'bar' }, { type: 'bar' }]}
          myChart.setOption(option)
          window.addEventListener("resize", function() {
            myChart.resize()
          })
        }
        this.$on('hook:destroyed',()=>{
          window.removeEventListener("resize", function() {
            myChart.resize();
          });
        })
      },
      getEchartData1() {
        const chart1 = this.$refs.chart1
        if (chart1) {
          const myChart = this.$echarts.init(chart1)
          const option = {
            title: {
              text: 'Stacked Line'
            },
            tooltip: {
              trigger: 'axis'
            },
            legend: {
              data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            toolbox: {
              feature: {
                saveAsImage: {}
              }
            },
            xAxis: {
              type: 'category',
              boundaryGap: false,
              data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月','八月','九月','十月','十一月','十二月']
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: 'Email',
                type: 'line',
                stack: 'Total',
                data: [120, 132, 101, 134, 90, 230, 210,120, 132, 101, 134, 90, 230]
              },
              {
                name: 'Union Ads',
                type: 'line',
                stack: 'Total',
                data: [220, 182, 191, 234, 290, 330, 310,220, 182, 191, 234, 290, 330]
              },
              {
                name: 'Video Ads',
                type: 'line',
                stack: 'Total',
                data: [150, 232, 201, 154, 190, 330, 410,150, 232, 201, 154, 190, 330]
              },
              {
                name: 'Direct',
                type: 'line',
                stack: 'Total',
                data: [320, 332, 301, 334, 390, 330, 320,320, 332, 301, 334, 390, 330]
              },
              {
                name: 'Search Engine',
                type: 'line',
                stack: 'Total',
                data: [820, 932, 901, 934, 1290, 1330, 1320,820, 932, 901, 934, 1290, 1330]
              }
            ]
          }
          myChart.setOption(option)
          window.addEventListener("resize", function() {
            myChart.resize()
          })
        }
        this.$on('hook:destroyed',()=>{
          window.removeEventListener("resize", function() {
            myChart.resize();
          });
        })
      },
    },
    watch: {},
    created() {
    }
  }
</script>

6、實(shí)現(xiàn)效果

在這里插入圖片描述

7、可能遇到的問(wèn)題,下載不成功。使用

cnpm install echarts --save

在這里插入圖片描述

8、11:25-32 "export ‘default’ (imported as ‘echarts’) was not found in 'echarts

修改引入的方式

// 引入echarts
import *as echarts from 'echarts'
Vue.prototype.$echarts = echarts

相關(guān)文章

最新評(píng)論

南和县| 洪江市| 华安县| 赣州市| 伊川县| 鸡泽县| 邓州市| 左贡县| 永川市| 井陉县| 平乐县| 怀柔区| 商河县| 四子王旗| 江北区| 乌拉特中旗| 昌黎县| 新巴尔虎左旗| 南汇区| 鄂州市| 大新县| 万宁市| 七台河市| 长垣县| 博爱县| 慈利县| 高邮市| 金门县| 民乐县| 石台县| 宜兰市| 青川县| 呼玛县| 武宣县| 大洼县| 鄯善县| 衡水市| 临潭县| 商丘市| 米林县| 绥滨县|