【vue】vue中引入echarts的兩種方式


1.安裝echarts依賴

  npm install echarts -S

2.創建圖表

a:全局引入

main.js頁面

import echarts from 'echarts'

Vue.prototype.$echarts = echarts

Hello.vue頁面

<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
<script>
export default {
name: 'FuncFormsBase',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted () {
this.drawLine();
},
methods: {
drawLine () {
var echarts = require('echarts');
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
title: {
text: 'ECharts 入門示例'
},
tooltip: {},
xAxis: {
data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
</script>

<style scoped>
</style>

  

b:按需引入

上面全局引入會將所有的echarts圖表打包,導致體積過大,所以我覺得最好還是按需引入。

// 引入基本模板
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() {
      // 基於准備好的dom,初始化echarts實例
      let myChart = echarts.init(document.getElementById('myChart'))
      // 繪制圖表
      myChart.setOption({
        title: { text: 'ECharts 入門示例' },
        tooltip: {},
        xAxis: {
          data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
        },
        yAxis: {},
        series: [{
          name: '銷量',
          type: 'bar',
          data: [5, 20, 36, 10, 10, 20]
        }]
      });
    }
  }
}

 



免責聲明!

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



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