![](https://img.haomeiwen.com/i6047683/52f814b5d75cfb7a.png)
"echarts": "^5.1.2",
<template>
<div id="centerEcharts2" class="echarts"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
// startVal 初始值 endVal 最终值 title 标签值
props: {
datas: {
type: [Array, Object, String],
default: ""
}
},
data() {
return {
Time1: null,
datasCopy: ""
};
},
computed: {},
watch: {
// 这是监听json值变化
datas: {
// json为监听参数名
handler: function(val, oldVal) {
// 不能用箭头函数
this.$nextTick(() => {
setTimeout(() => {
this.echartsFun(val);
}, 200);
});
},
immediate: true // 首次赋值是否触发,一般不写
}
},
beforeDestroy() {
clearInterval(this.Time1);
},
methods: {
echartsFun(val) {
this.datasCopy = val;
// console.log(JSON.parse(JSON.stringify(this.datasCopy)));
const t = this;
const myChart = echarts.init(document.getElementById("centerEcharts2"));
const option = {
tooltip: {
trigger: "axis",
confine: true, // 悬停内容超出画布用这个
backgroundColor: "rgba(0,0,0,0.5)",
borderWidth: 0,
textStyle: {
color: "#fff"
},
axisPointer: {
type: "shadow"
},
formatter(params) {
const index = params[0].dataIndex; // 下标
return (
t.datasCopy.xData0[index] +
"<br/>" +
"数量:" +
t.datasCopy.yData[index]
);
}
},
grid: {
top: "5%",
left: "0%",
right: "0%",
bottom: "0",
containLabel: true // 显示范围包含坐标刻度
},
xAxis: [
{
type: "category",
showMaxLabel: true, // X轴显示开头和最后一个信息
data: this.datasCopy.xData,
axisLabel: {
rotate: 50,
textStyle: {
fontSize: 12,
color: "rgba(255,255,255,1)"
}
},
axisLine: {
lineStyle: {
color: "rgba(255,255,255,1)",
width: 1,
type: "solid"
}
},
axisTick: {
show: false
}
}
],
yAxis: [
{
max: function(value) {
if (value.max === 0) {
return value.max + 10;
}
},
axisLabel: {
// 设置单位
formatter: "{value} M"
},
minInterval: 1,
type: "value",
axisTick: {
show: false
},
axisLine: {
show: false,
lineStyle: {
color: "rgba(255,255,255,1)",
width: 1,
type: "solid"
}
},
splitLine: {
lineStyle: {
color: "rgba(255,255,255,0.6)"
}
}
}
],
series: [
{
name: "",
type: "bar",
data: this.datasCopy.yData,
barWidth: 20, // 柱子宽度
barGap: 1, // 柱子之间间距
itemStyle: {
normal: {
color: val => {
return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: "#01DAE8"
},
{
offset: 1,
color: "#01DAE8"
}
]);
}
}
}
}
]
};
myChart.setOption(option, true); // true无数据时更新试图
}
}
};
</script>
<style lang="scss" scoped>
.echarts {
width: 100%;
height: 100%;
}
</style>
感谢大家点赞!
网友评论