2021-03-11 15:06:13 +08:00
|
|
|
|
<template>
|
|
|
|
|
<div class="Echarts" style="height: 100%;display: flex;margin-top: 10px;">
|
|
|
|
|
<div id="echart" style="width: 100%;height: 80vh;" />
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import { BASE_BAR, BASE_LINE } from '../chart/chart'
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'ChartComponent',
|
|
|
|
|
props: {
|
|
|
|
|
chart: {
|
|
|
|
|
type: Object,
|
|
|
|
|
required: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data() {
|
2021-03-11 15:19:44 +08:00
|
|
|
|
return {
|
|
|
|
|
myChart: {}
|
|
|
|
|
}
|
2021-03-11 15:06:13 +08:00
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
chart() {
|
|
|
|
|
this.drawEcharts()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted() {
|
2021-03-11 15:19:44 +08:00
|
|
|
|
// 基于准备好的dom,初始化echarts实例
|
|
|
|
|
this.myChart = this.$echarts.init(document.getElementById('echart'))
|
2021-03-11 15:06:13 +08:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
drawEcharts() {
|
|
|
|
|
const chart = this.chart
|
|
|
|
|
let chart_option = {}
|
|
|
|
|
// todo type
|
|
|
|
|
if (chart.type === 'bar') {
|
|
|
|
|
chart_option = JSON.parse(JSON.stringify(BASE_BAR))
|
|
|
|
|
} else if (chart.type === 'line') {
|
|
|
|
|
chart_option = JSON.parse(JSON.stringify(BASE_LINE))
|
|
|
|
|
}
|
|
|
|
|
// console.log(chart_option);
|
2021-03-17 16:12:30 +08:00
|
|
|
|
// 处理data
|
2021-03-11 15:06:13 +08:00
|
|
|
|
if (chart.data) {
|
|
|
|
|
chart_option.title.text = chart.title
|
|
|
|
|
chart_option.xAxis.data = chart.data.x
|
|
|
|
|
chart.data.series.forEach(function(y) {
|
|
|
|
|
chart_option.legend.data.push(y.name)
|
|
|
|
|
chart_option.series.push(y)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// console.log(chart_option);
|
2021-03-17 16:12:30 +08:00
|
|
|
|
// 处理shape attr
|
|
|
|
|
if (chart.customAttr) {
|
2021-03-18 10:08:12 +08:00
|
|
|
|
const customAttr = JSON.parse(chart.customAttr)
|
|
|
|
|
if (customAttr.color) {
|
|
|
|
|
chart_option.color = customAttr.color.colors
|
2021-03-17 16:12:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
|
|
|
|
</style>
|