详解Vue中使用Echarts的两种方式

网友投稿 182 2023-07-27

详解Vue中使用Echarts的两种方式

1. 直接引入echarts

先npm安装echarts

npm install echarts --save

开发:

main.js

import myCharts from './comm/js/myCharts.js'

vue.use(myCharts)

myCharts.js

/**

* 各种画echarts图表的方法都封装在这里

*/

import echarts from 'echarts'

(function() {

var chart = {};

chart.install = function(vue) {

vue.prototype.$chart = {

//画一条简单的线

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);

},

}

}

if(typeof exports == 'object') {

module.exports = chart

}

})()

hello.vue

...

...

mounted() {

this.$chart.line1('chart1');

},

2、使用vue-echarts

先npm安装vue-echarts

npm install vue-echarts

开发:

main.js

import ECharts from 'vue-echarts/components/ECharts'

import 'echarts/lib/chart/bar'

import 'echarts/lib/component/tooltip'

Vue.component('chart', ECharts)

hello.vue

...

...

data: function() {

return {

orgOptions: {},

}

},

...

mounted() {

this.orgOptions = {

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

}]

}

}

总结

以上所述是给大家介绍的Vue中使用Echarts的两种方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中的字节流文件读取教程(一)
下一篇:vue 解决addRoutes动态添加路由后刷新失效问题
相关文章