在Vue中使用echarts

個人喜好使用cnpm,速度比npm快N倍
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install echarts --save
在assets目錄下新建js目錄,再添加myCharts.js文件:

/**
 * 各種畫echarts圖表的方法都封裝在這裏
 * 注意:爲了方便學習echarts沒有采用按需引入的方式
 */

import echarts from 'echarts'
const install = function(Vue) {
    Object.defineProperties(Vue.prototype, {
        $chart: {
            get() {
                return {
                    //畫一條簡單的線
                    line1: function (id) {
                        this.chart = echarts.init(document.getElementById(id));
                        this.chart.clear();

                        const optionData = {
                            xAxis: {
                                type: 'category',
                                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                            },
                            yAxis: {
                                type: 'value'
                            },
                            series: [{
                                data: [820, 932, 901, 934, 1290, 1330, 1320],
                                type: 'line',
                                smooth: true
                            }]
                        };

                        this.chart.setOption(optionData);
                    },
                }
            }
        }
    })
}

export default {
    install
}

在main.js中添加如下代碼

import myCharts from '@/assets/js/myCharts.js'
Vue.use(myCharts)

至此就可以和在js中一樣使用echarts圖了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章