vue 項目使用 echarts


 

Echarts 官網:http://echarts.apache.org/zh/index.html

 

安裝依賴

npm安裝

npm install echarts -S

 

或者用淘寶鏡像安裝

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

 

全局引入

在main.js中引入

import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts

 

注意:一定要注意引入引入方式,否則會報錯

若有報錯可參照vue中使用echarts報錯:“TypeError: Cannot read property ‘init‘ of undefined“報錯原因及解決方案進行解決

 

創建圖表

 

此例是創建柱狀圖

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

<script>
export default {
  name: 'index',
  data() {
    return {
    };
  },
  mounted() {
    this.myecharts()
  },
  methods: {
    myecharts(){
      var myChart = this.$echarts.init(document.getElementById('main'));
        // 指定圖表的配置項和數據
        var option = {
            title: {
                text: 'ECharts 入門示例'
            },
            tooltip: {},
            legend: {
                data:['銷量']
            },
            xAxis: {
                data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
            },
            yAxis: {},
            series: [{
                name: '銷量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        };
        // 使用剛指定的配置項和數據顯示圖表。
        myChart.setOption(option);
    }
  },
};
</script>

 

 


免責聲明!

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



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