程序员

在Vue中使用echarts

2019-08-23  本文已影响0人  猿择

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

image.png
/**
 * 各种画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图了

上一篇下一篇

猜你喜欢

热点阅读