2021-03-11 15:06:13 +08:00
|
|
|
|
<template>
|
|
|
|
|
<div class="Echarts" style="height: 100%;display: flex;margin-top: 10px;">
|
2021-03-19 15:54:35 +08:00
|
|
|
|
<div :id="chartId" style="width: 100%;height: 100%;" />
|
2021-03-11 15:06:13 +08:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2021-03-22 15:55:39 +08:00
|
|
|
|
import { BASE_BAR, BASE_LINE, HORIZONTAL_BAR, BASE_PIE, BASE_FUNNEL, BASE_RADAR } from '../chart/chart'
|
2021-03-18 17:20:59 +08:00
|
|
|
|
import { baseBarOption, stackBarOption, horizontalBarOption, horizontalStackBarOption } from '../chart/bar/bar'
|
2021-03-22 15:55:39 +08:00
|
|
|
|
import { baseLineOption, stackLineOption } from '../chart/line/line'
|
2021-03-18 17:20:59 +08:00
|
|
|
|
import { basePieOption } from '../chart/pie/pie'
|
|
|
|
|
import { baseFunnelOption } from '../chart/funnel/funnel'
|
2021-03-22 15:55:39 +08:00
|
|
|
|
import { baseRadarOption } from '../chart/radar/radar'
|
2021-03-11 15:06:13 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'ChartComponent',
|
|
|
|
|
props: {
|
|
|
|
|
chart: {
|
|
|
|
|
type: Object,
|
|
|
|
|
required: true
|
2021-03-19 15:54:35 +08:00
|
|
|
|
},
|
|
|
|
|
chartId: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false
|
2021-03-11 15:06:13 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data() {
|
2021-03-11 15:19:44 +08:00
|
|
|
|
return {
|
|
|
|
|
myChart: {}
|
|
|
|
|
}
|
2021-03-11 15:06:13 +08:00
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
chart() {
|
2021-03-19 15:54:35 +08:00
|
|
|
|
this.drawEcharts()
|
|
|
|
|
},
|
|
|
|
|
resize() {
|
2021-03-11 15:06:13 +08:00
|
|
|
|
this.drawEcharts()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted() {
|
2021-03-11 15:19:44 +08:00
|
|
|
|
// 基于准备好的dom,初始化echarts实例
|
2021-03-19 15:54:35 +08:00
|
|
|
|
this.myChart = this.$echarts.init(document.getElementById(this.chartId))
|
|
|
|
|
this.drawEcharts()
|
2021-03-11 15:06:13 +08:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
drawEcharts() {
|
|
|
|
|
const chart = this.chart
|
|
|
|
|
let chart_option = {}
|
|
|
|
|
// todo type
|
|
|
|
|
if (chart.type === 'bar') {
|
2021-03-18 11:05:14 +08:00
|
|
|
|
chart_option = baseBarOption(JSON.parse(JSON.stringify(BASE_BAR)), chart)
|
2021-03-18 17:20:59 +08:00
|
|
|
|
} else if (chart.type === 'bar-stack') {
|
|
|
|
|
chart_option = stackBarOption(JSON.parse(JSON.stringify(BASE_BAR)), chart)
|
|
|
|
|
} else if (chart.type === 'bar-horizontal') {
|
|
|
|
|
chart_option = horizontalBarOption(JSON.parse(JSON.stringify(HORIZONTAL_BAR)), chart)
|
|
|
|
|
} else if (chart.type === 'bar-horizontal-stack') {
|
|
|
|
|
chart_option = horizontalStackBarOption(JSON.parse(JSON.stringify(HORIZONTAL_BAR)), chart)
|
2021-03-11 15:06:13 +08:00
|
|
|
|
} else if (chart.type === 'line') {
|
2021-03-18 11:05:14 +08:00
|
|
|
|
chart_option = baseLineOption(JSON.parse(JSON.stringify(BASE_LINE)), chart)
|
2021-03-22 15:55:39 +08:00
|
|
|
|
} else if (chart.type === 'line-stack') {
|
|
|
|
|
chart_option = stackLineOption(JSON.parse(JSON.stringify(BASE_LINE)), chart)
|
2021-03-18 17:20:59 +08:00
|
|
|
|
} else if (chart.type === 'pie') {
|
|
|
|
|
chart_option = basePieOption(JSON.parse(JSON.stringify(BASE_PIE)), chart)
|
|
|
|
|
} else if (chart.type === 'funnel') {
|
|
|
|
|
chart_option = baseFunnelOption(JSON.parse(JSON.stringify(BASE_FUNNEL)), chart)
|
2021-03-22 15:55:39 +08:00
|
|
|
|
} else if (chart.type === 'radar') {
|
|
|
|
|
chart_option = baseRadarOption(JSON.parse(JSON.stringify(BASE_RADAR)), chart)
|
2021-03-17 16:12:30 +08:00
|
|
|
|
}
|
2021-03-18 17:55:28 +08:00
|
|
|
|
console.log(chart_option)
|
2021-03-11 15:06:13 +08:00
|
|
|
|
this.myEcharts(chart_option)
|
|
|
|
|
},
|
|
|
|
|
myEcharts(option) {
|
|
|
|
|
// 指定图表的配置项和数据
|
2021-03-11 15:24:25 +08:00
|
|
|
|
const chart = this.myChart
|
|
|
|
|
setTimeout(chart.setOption(option, true), 500)
|
2021-03-11 15:06:13 +08:00
|
|
|
|
window.onresize = function() {
|
2021-03-11 15:24:25 +08:00
|
|
|
|
chart.resize()
|
2021-03-11 15:06:13 +08:00
|
|
|
|
}
|
2021-03-19 15:54:35 +08:00
|
|
|
|
},
|
|
|
|
|
chartResize() {
|
|
|
|
|
// 指定图表的配置项和数据
|
|
|
|
|
const chart = this.myChart
|
|
|
|
|
chart.resize()
|
2021-03-11 15:06:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
|
|
|
|
</style>
|