gogoWebsite

Using echarts in vue

Updated to 7 hours ago

Welcome to my community:/Q52km
Red packets are given out in the community from time to time

Article Catalog

  • 1. Installation
  • 2、Introduce in vue (global introduction)
  • 3. Use in vue
  • 4, the template code in which position
  • 5, complete with an example of a vue page:
  • 6、Realize the effect
  • 7. Problems that may be encountered, the download is not successful. Use
  • 8、11:25-32 "export 'default' (imported as 'echarts') was not found in 'echarts

1. Installation

npm install echarts --save

2、Introduce in vue (global introduction)

// Introducing echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts


3. Use in vue

Need to use the echart place first set a div id, width and height

Tip:
Multiple data report templates can be introduced on a single page
Typographic placement using divs


4, the template code in which position

Focused attention:Where const option = { } is the code we need to introduce the echart charts


<template>
  <div>
    <div ref="chart" style="width:50%;height:376px"></div>
  </div>

</template>

To instantiate the echarts object in the mounted lifecycle function. Make sure the dom element is mounted to the page.

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, complete with an example of a vue page:


<template>
  <div>
    <div ref="chart" style="width:50%;height:376px"></div>
    <div ref="chart1" style="width:50%;height:376px"></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: [

                ['Orders', 43.3, 85.8],
                ['Order 1', 83.1, 73.4],
                ['Order 2', 86.4, 65.2],
                ['Order 3', 72.4, 53.9],
                ['Order 4', 82.4, 53.9],
                ['Order 5', 42.4, 53.9],
                ['Order 6', 72.4, 53.9],
                ['Order 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: ['January', 'February', 'March', 'April', 'May', 'June', 'July','August','September','October','November','December']
            },
            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、Realize the effect

在这里插入图片描述


7. Problems that may be encountered, the download is not successful. Use

cnpm install echarts --save

在这里插入图片描述


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

Modification of the introduction

// pull intoecharts
import *as echarts from 'echarts'
.$echarts = echarts