|
<template>
<div :class="className" :style="{height:height,width:width}"></div>
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import { debounce } from './chart'
export default {
props: {
className: {
type: String,
default: 'chart'
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '350px'
},
chartData: {
type: Array
}
},
data() {
return {
chart: null
}
},
mounted() {
this.initChart()
this.__resizeHanlder = debounce(() => {
if (this.chart) {
this.chart.resize()
}
}, 100)
window.addEventListener('resize', this.__resizeHanlder)
},
beforeDestroy() {
if (!this.chart) {
return
}
window.removeEventListener('resize', this.__resizeHanlder)
this.chart.dispose()
this.chart = null
},
watch: {
chartData: {
deep: true,
handler(val) {
this.setOptions(val)
}
}
},
methods: {
setOptions(datastr) {
this.chart.setOption({
title: {
text: '分渠道',
textStyle: {
color: '#333',
fontSize: 14
},
top: '0'
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
dataZoom: [
{
type: 'slider',
start: 60,
end: 100
},
{
type: 'inside'
}
],
xAxis: {
data: datastr.map(item => {
return item.time
})
},
yAxis: {
type: 'value'
},
legend: {
data: ['PC端', '移动端', '合计']
},
toolbox: {
show: true,
feature: {
// dataZoom: {
// yAxisIndex: 'none'
// },
dataView: { readOnly: false },
magicType: {
type: ['line', 'bar']
},
restore: {},
saveAsImage: {}
}
},
series: [
{
name: '移动端',
type: 'bar',
stack: '总量',
barMaxWidth: 35,
data: datastr.map(item => {
return item.h5
})
},
{
name: 'PC端',
type: 'bar',
stack: '总量',
data: datastr.map(item => {
return item.pc
})
},
{
name: '合计',
type: 'line',
stack: '总量',
symbol: 'circle',
// areaStyle: {normal: {}},
// label: {
// normal: {
// show: true,
// position: 'top',
// formatter: function (params) {
// for (var i = 0, l = option.xAxis.data.length; i < l; i++) {
// if (option.xAxis.data[i] == params.name) {
// // option.series[0].data[i];
// var num = option.series[0].data[i]+option.series[1].data[i]+option.series[2].data[i];
// return '合计:' + num;
// }
// }
// }
//
// }
// },
// itemStyle:{
// normal:{
// // color:"#dd4444",
// barBorderRadius:0,
// label:{
// show:true,
// position: 'top',
// formatter:function (params) {
// return params.val
// }
// }
// }
// },
data: datastr.map(item => {
return item.num
}),
itemStyle: {
normal: {
label: {
show: true
}
}
}
}
]
})
},
initChart() {
this.chart = echarts.init(this.$el) // , 'macarons'
var that = this
setTimeout(function() {
that.setOptions(that.chartData)
}, 1)
}
}
}
</script>
|