首先在vue安装 echarts 命令为 npm install echarts
之后在main.js引入文件并使用它
命令:
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
在要使用echarts的vue文件写如下代码(本文是通过接口获取数据)
<template>
<div class="main-content">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template>
<script>
export default {
name: 'hello',
data () {
return {
articles: [],
articless: [],
}
},
mounted:function(){
this.$http.jsonp('http://127.0.0.1/************(接口地址)', {},{
headers: { }, emulateJSON: true }).then(function(response)
{
this.articles = response.body.user_city
this.articless = response.body.count
this.drawLine(this.articles,this.articless);
}, function(response)
{
console.log(response)
})
},
methods: {
drawLine(city,count){
// 基于准备好的dom,初始化echarts实
let myChart = this.$echarts.init(document.getElementById('main'));
// 绘制图表
myChart.setOption({
title : {
text: '一直播的用户注册量',
subtext: ''
},
tooltip : {
trigger: 'axis'
},
legend: {
data:['注册量','降水量']
},
toolbox: {
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
magicType : {show: true, type: ['line', 'bar']},
restore : {show: true},
saveAsImage : {show: true}
}
},
calculable : true,
xAxis : [
{
type : 'category',
data : city
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
name:'注册量',
type:'bar',
data:count,
markPoint : {
data : [
{type : 'max', name: '最大值'},
{type : 'min', name: '最小值'}
]
},
markLine : {
data : [
{type : 'average', name: '平均值'}
]
}
},
]
});
}
}
}
</script>
<style></style>