vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

时间:2021-12-21 11:00:44

一、安装echarts:

cnpm i echarts -D

vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

二、在vue-cli的main.js文件中引用echarts:

import charts from 'echarts'
Vue.prototype.$echarts = charts

vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

三、echarts详细代码:

echarts.vue:

<template>
<div>
<div id="myChart"> </div>
</div>
</template> <script>
export default {
methods: {
drawLine(){
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById('myChart')); this.$axios.get("http://127.0.0.1:8000/get_data")
.then(function(res){
// 绘制图表
myChart.setOption({
title: { text: res.data.title },
tooltip: {},
xAxis: {
data: res.data.xAxisData
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: res.data.seriesData
}]
});
})
.catch(function(err){
console.log(err);
})
}
},
mounted(){
this.drawLine();
}
}
</script> <style>
#myChart{
height: 500px;
}
</style>

vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

四、上面的图表数据通过axios获取,node.js代码如下:

let express = require("express");
let app = express(); app.get("/get_data", function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
let response = {
title: '在Vue中使用echarts',
xAxisData: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"],
seriesData: [10, 26, 16, 20, 16, 30]
};
res.type('application/json');
res.jsonp(response);
}); app.listen(8000, function(){
console.log("开始执行请求");
});

vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

相关文章