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

vue使用echarts圖表的詳細(xì)方法

 更新時(shí)間:2022年03月27日 15:35:35   作者:俊俊的小熊餅干  
這篇文章主要為大家詳細(xì)介紹了vue使用echarts圖表的詳細(xì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了vue使用echarts圖表的方法,供大家參考,具體內(nèi)容如下

該示例使用 vue-cli  腳手架搭建

安裝echarts依賴

npm install echarts -S

或者使用國(guó)內(nèi)的淘寶鏡像:

安裝

npm install -g cnpm --registry=https://registry.npm.taobao.org

使用

cnpm install echarts -S

創(chuàng)建圖表

全局引入

main.js

// 引入echarts
import echarts from 'echarts'

Vue.prototype.$echarts = echarts 

Hello.vue

<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
export default {
 name: 'hello',
 data () {
  return {
   msg: 'Welcome to Your Vue.js App'
  }
 },
 mounted(){
  this.drawLine();
 },
 methods: {
  drawLine(){
    // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
    let myChart = this.$echarts.init(document.getElementById('myChart'))
    // 繪制圖表
    myChart.setOption({
      title: { text: '在Vue中使用echarts' },
      tooltip: {},
      xAxis: {
        data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
      },
      yAxis: {},
      series: [{
        name: '銷量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
      }]
    });
  }
 }
}

注意:這里echarts初始化應(yīng)在鉤子函數(shù)mounted()中,這個(gè)鉤子函數(shù)是在el 被新創(chuàng)建的 vm.$el 替換,并掛載到實(shí)例上去之后調(diào)用

按需引入

上面全局引入會(huì)將所有的echarts圖表打包,導(dǎo)致體積過(guò)大,所以我覺(jué)得最好還是按需引入。

Hello.vue

// 引入基本模板
let echarts = require('echarts/lib/echarts')
// 引入柱狀圖組件
require('echarts/lib/chart/bar')
// 引入提示框和title組件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {
 name: 'hello',
 data() {
  return {
   msg: 'Welcome to Your Vue.js App'
  }
 },
 mounted() {
  this.drawLine();
 },
 methods: {
  drawLine() {
   // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
   let myChart = echarts.init(document.getElementById('myChart'))
   // 繪制圖表
   myChart.setOption({
    title: { text: 'ECharts 入門(mén)示例' },
    tooltip: {},
    xAxis: {
     data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
    },
    yAxis: {},
    series: [{
     name: '銷量',
     type: 'bar',
     data: [5, 20, 36, 10, 10, 20]
    }]
   });
  }
 }
}

這里之所以使用 require 而不是 import,是因?yàn)?require 可以直接從 node_modules 中查找,而 import 必須把路徑寫(xiě)全。

頁(yè)面展示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論