cnpm install echarts -S
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
---------------------------------------------------
<template>
<div id="myChart" :style="{width: '1300px', height: '600px'}"></div>
</template>
<script>
export default {
name: 'FilmChart',
data () {
return {
msg: 'Welcome to Your Vue.js App',
xData: ['11','22','33'],
yData: [2,3,4]
}
},
mounted(){
this.getX();
this.getY();
this.drawLine();
},
methods: {
drawLine(){
// 基於准備好的dom,初始化echarts實例
let myChart = this.$echarts.init(document.getElementById('myChart'))
// 繪制圖表
myChart.setOption({
title: { text: '影片統計' },
tooltip: {},
xAxis: {
data: this.xData
},
yAxis: {},
series: [{
name: '數量',
type: 'bar',
data: this.yData
}]
});
},
async getX(){ /*同步請求,異步可能出問題,方法調用時數據還沒完成替換*/
var res = await this.axios.get("type/typeList1")
for (let i=0;i<res.data.length;i++){
this.xData[i] = res.data[i].typeName
}
console.log(this.xData)
},
async getY(){
var res = await this.axios.get("film/countFilm")
for (let i=0;i<res.data.length;i++){
this.yData[i] = res.data[i]
}
console.log(this.yData)
this.drawLine();
},
},
watch:{
'xData':function(){
console.log('watch_xData:'+this.xData);
}
}
}
</script>
<style scoped>
</style>