<template> <div :id="id" style="width: 100%; height: 100%"></div> </template> <script> import * as echarts from "echarts"; import { guid } from "@/utils/util"; import { reactive, toRefs, onMounted, watch } from "vue"; export default { name: "moreBar", props: { data: Object, }, setup(props) { const state = reactive({ id: guid(), chart: null, }); const resizeTheChart = () => { if (state.chart) { state.chart.resize(); } }; const init = () => { let chartDom = echarts.init(document.getElementById(state.id)); let option; option = { tooltip: { trigger: "axis", axisPointer: { type: "shadow", }, }, grid: { top: "10%", left: "3%", right: "3%", bottom: "10%", containLabel: true, }, legend: { data: ["绩效", "积分"], bottom: 0, }, xAxis: [ { type: "category", axisTick: { show: false }, data: props.data.xData, }, ], yAxis: [ { name: "绩效", type: "value", }, { name: "积分", type: "value", min: 0, max: 1, }, ], series: [ { name: "绩效", type: "bar", barGap: 0, emphasis: { focus: "series", }, data: props.data.yData.y1, barWidth: 30, itemStyle: { normal: { color: new echarts.graphic.LinearGradient( 0, 1, 0, 0, // 0,0,1,0表示从左向右 0,0,0,1表示从右向左 [ { offset: 0, color: "rgba(23, 183, 255, 1)" }, { offset: 1, color: "rgba(3, 108, 255, 1)" }, ] ), }, }, }, { name: "积分", type: "bar", yAxisIndex: 1, emphasis: { focus: "series", }, data: props.data.yData.y2, barWidth: 30, itemStyle: { normal: { color: new echarts.graphic.LinearGradient( 0, 1, 0, 0, // 0,0,1,0表示从左向右 0,0,0,1表示从右向左 [ { offset: 0, color: "rgba(255, 187, 23, 1)" }, { offset: 1, color: "rgba(255, 151, 3, 1)" }, ] ), }, }, }, ], }; option && chartDom.setOption(option, true); state.chart = chartDom; }; onMounted(() => { init(); window.addEventListener("resize", resizeTheChart); }); return { ...toRefs(state), resizeTheChart, init, }; }, }; </script>