vue中使用echarts


1、在項目下使用命令行,初始化工程的 npm 環境並安裝 echarts(這里前提是您已經安裝了 npm)

npm init
npm install echarts --save

2、全局引入

main.js

import Vue from 'vue'
//全局引入echarts
import * as echarts from 'echarts';
//需要掛載到Vue原型上
Vue.prototype.$echarts = echarts

組件中使用

<template>
  <div style="width: auto;height: 400px" id="main">
  </div>
</template>
//通過this.$echarts來使用
  export default {
    name: "page",
    mounted(){
        // 在通過mounted調用即可
        this.echartsInit()
    },
    methods: {
        //初始化echarts
        echartsInit() {
            //柱形圖
            //因為初始化echarts 的時候,需要指定的容器 id='main'
            this.$echarts.init(document.getElementById('main')).setOption({
                xAxis: {
                    type: 'category',
                    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: [120, 200, 150, 80, 70, 110, 130],
                    type: 'bar',
                    showBackground: true,
                    backgroundStyle: {
                        color: 'rgba(220, 220, 220, 0.8)'
                    }
                }]
            })
        }
            
    }
  }
</script>

3、局部使用

<template>
  <div style="width: auto;height: 400px" id="main">
  </div>
</template>

<script>
  import * as echarts from 'echarts';
  export default {
    name: "echarts",
    data() {
      return {}
    },
    mounted() {
      this.echartsInit()
    },
    methods:{
      echartsInit() {
     //使用時只需要把setOption里的對象換成echarts中的options或者自己的參數即可 echarts.init(document.getElementById(
'main')).setOption({ xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar', showBackground: true, backgroundStyle: { color: 'rgba(220, 220, 220, 0.8)' } }] }) } } } </script>
————————————————
版權聲明:本文為CSDN博主「Code。。。」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/Soul_guys/article/details/109617069


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM