组件页面
<template>
<div class="chart-container" :id="_uid" :style="{width:'100%',height: height}"></div>
</template>
<script>
export default {
name: "SpeedCharts",
props: {
height: {
type: String,
default: '300px'
},
width: {
type: String,
default: '400px'
},
config: {
type: Object,
default: () => {
return {
series: []
}
}
},
},
watch: {
config: {
handler: function () {
this.drawLine()
},
deep: true
}
},
data() {
return {
resizeTimer: null
}
},
mounted() {
this.drawLine()
let _this = this
let myChart = this.$echarts.init(document.getElementById(this._uid))
window.addEventListener('resize', () => {
if (_this.resizeTimer) clearTimeout(_this.resizeTimer);
_this.resizeTimer = setTimeout(() => {
myChart.resize();
_this.drawLine()
}, 300)
})
},
methods: {
drawLine() {
const echarts = this.$echarts;
let myChart = echarts.init(document.getElementById(this._uid));
let config = this.config;
myChart.setOption(config);
}
}
}
</script>
<style lang="scss">
.chart-container {
div:first-child {
width: 100% !important;
canvas {
width: 100% !important;
}
}
}
</style>
父页面
<div>
<GaugeCharts :height="'32vh'" :config=gaugecConfigTwo></GaugeCharts>
</div>
import {GaugeCharts} from "@/components/GaugeCharts,vue";
export default {
components: {Table,GaugeCharts},
data() {
return {
gaugecConfigTwo:{
series: [
{
type: 'gauge',
title : {
offsetCenter : [0, '100%'],
textStyle: {
fontSize: 18,
color:"white",
}
},
radius: "70%",
center: ["30%", "45%"],
axisLine: {
lineStyle: {
width: 10,
color: [
[0.2, 'rgba(253, 70, 40, 1)'],
[0.8, 'rgba(63, 146, 236, 1)'],
[1, 'rgba(29, 138, 27, 1)']
]
}
},
pointer: {
itemStyle: {
color:'auto'
}
},
anchor: {
show: true,
showAbove: true,
size: 15,
itemStyle: {
borderWidth: 8
}
},
axisTick: {
distance: 0,
length: 6,
lineStyle: {
color: 'auto',
width: 1
}
},
splitLine: {
distance: 0,
length: 10,
lineStyle: {
color: 'auto',
width: 1
}
},
axisLabel: {
color: 'auto',
distance: 15,
fontSize: 14
},
detail: {
valueAnimation: true,
fontSize: 30,
color:'auto',
offsetCenter: [0, '70%']
},
data: [
{
value: 90,
name: '健康指数'
}
]
}
]
}
}
}