smart-switch-ui/src/views/dashboard/component/OnlineLineChart.vue
2024-04-19 16:57:43 +08:00

115 lines
2.1 KiB
Vue

<template>
<div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import * as echarts from 'echarts'
import resize from "@/views/dashboard/mixins/resize";
require('echarts/theme/macarons') // echarts theme
export default {
mixins: [resize],
props: {
className: {
type: String,
default: 'chart'
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '350px'
},
autoResize: {
type: Boolean,
default: true
},
data: {
type: Array,
required: true
}
},
data() {
return {
chart: null
}
},
watch: {
data: {
deep: true,
handler(val) {
this.setOptions(val)
}
}
},
mounted() {
this.$nextTick(() => {
this.initChart()
})
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods: {
initChart() {
this.chart = echarts.init(this.$el, 'macarons')
this.setOptions(this.data)
},
setOptions(data) {
let option = {
grid: {
top: 4,
left: 4,
right: 4,
bottom: 4,
backgroundColor: 'transparent',
show: false,
},
tooltip: {
trigger: 'axis',
show: true,
},
xAxis: {
type: 'category',
boundaryGap: false,
show: false,
data: data.map(item => item.x)
},
yAxis:[{
name: "在线率",
show: false,
type: 'value',
min: 'dataMin',
max: 'dataMax',
},{
name: "充值",
show: false,
type: 'value',
min: 'dataMin',
max: 'dataMax',
},
],
series: [{
name: "在线率",
color: '#165DFF',
lineStyle: {
type: 'dashed'
},
data: data.map(item => item.online),
type: 'line',
smooth: true,
yAxisIndex: 0,
}]
};
this.chart.setOption(option)
}
}
}
</script>