<template> <div :class="className" :style="{ height: height, width: width }" /> </template> <script> import * as echarts from "echarts" import resize from "@/components/Charts/mixins/resize" const animationDuration = 2600 export default { mixins: [resize], props: { className: { type: String, default: "chart", }, width: { type: String, default: "100%", }, height: { type: String, default: "300px", }, autoResize: { type: Boolean, default: true, }, chartData: { required: true, }, chartConfig: { type: Object, default: () => { return {} }, }, }, data() { return { chart: null, } }, watch: { chartData: { 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) this.setOptions(this.chartData) }, setOptions(chartData) { // if (!chartData.data) return const option = { tooltip: { trigger: "axis", // formatter: "{b}检出率" + ":" + "{c}%", valueFormatter: (value) => "检出率:" + value + "%", axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: "shadow", // 默认为直线,可选为:'line' | 'shadow' }, backgroundColor: "rgba(50,50,50,0.7)", textStyle: { color: "#fff", }, }, grid: { top: "20%", left: "8%", right: "8%", bottom: "3%", containLabel: true, }, title: { text: this.chartConfig.title, left: "center", textStyle: { color: "#fff", fontSize: 18, fontWeight: "normal", }, top: 0, }, yAxis: { type: "value", name: "百分率", nameTextStyle: { color: "#aaa", }, splitLine: { show: false, }, axisTick: { show: false, }, axisLine: { show: true, lineStyle: { color: "#aaa", }, }, axisLabel: { show: true, formatter: "{value} %", //右侧Y轴文字显示 textStyle: { color: "#aaa", }, }, }, xAxis: { data: [], axisLine: { show: true, //隐藏X轴轴线 lineStyle: { color: "#aaa", }, }, axisTick: { show: true, //隐藏X轴刻度 }, axisLabel: { show: true, interval: "0", rotate: 25, textStyle: { color: "#fff", //X轴文字颜色 }, }, }, series: [ { type: "bar", stack: "vistors", barWidth: 15, data: [], itemStyle: { normal: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: "#00FFE3", }, { offset: 1, color: "#4693EC", }, ]), }, }, animationDuration: 2600, }, ], } const series = chartData.map((v) => { return { name: v.name, value: Number(v.value), } }) option.xAxis.data = series.map((_) => _.name) option.series[0].data = series.map((_) => _.value) this.chart.setOption(option) }, }, } </script>