<template> <div :id="id" style="width: 100%; height: 100%"></div> </template> <script> import * as echarts from "echarts"; import { guid } from "@/utils/ruoyi"; import { reactive, toRefs, onMounted, watch, nextTick, onBeforeUnmount, } from "vue"; export default { name: "qcswCharts", props: { data: Object, refresh: Number, }, setup (props) { const state = reactive({ id: guid(), chart: null, }); const resizeTheChart = () => { if (state.chart) { state.chart.resize(); } }; const init = () => { console.log(props, "props"); // let timeList, // levelList, // dataList = []; // timeList = props.data.timeList; // timeList = timeList?.map((item) => item.slice(6, 16)); // dataList = props.data.dataList; // levelList = props.data.levelList; let chartDom = echarts.init(document.getElementById(state.id)); // if (dataList && dataList.length == 0) return; // console.log(props.data.unit, ' props.data.unit'); var option; option = { tooltip: { trigger: "axis", axisPointer: { type: "shadow", }, backgroundColor: "#082e57", borderColor: "#00D2FF", textStyle: { fontSize: 14, fontWeight: 500, color: "#BFDDD5", }, formatter (params) { var name = params[0].name var value = params[0].value var typename = `降水量:${value}mm` return ( '<span style="float:left;">' + '时间:' + name + '</span></br>' + '<span style="float:left;">' + typename + '</span></br>' ) } }, grid: { top: "15%", left: "10%", right: "10%", bottom: "20%", // containLabel: true, }, color: ["#2980E0", "#1FDAC5"], // color: ["red", "black", "yellow", "pink", "cyan"], // legend: { // textStyle: { // color: "#E8F7FF", // }, // itemHeight: 10, // itemWidth: 10, // icon: "circle", // left: "60%", // top: "5%", // data: ["前池一", "前池二"], // }, xAxis: { type: "category", // name: "小时", data: props.data.XAxis, boundaryGap: false, splitLine: { show: false, }, axisLabel: { color: "#DADADA", }, }, yAxis: [ { type: "value", // name: "m", alignTicks: true, // min: 0, min: function (value) { return value.min; }, nameTextStyle: { color: "#298AFF", }, axisLine: { show: true, }, splitLine: { show: true, lineStyle: { type: "dotted", color: "#0E6BA5", }, }, axisLabel: { color: "#298AFF", }, }, ], series: [ { // name: "前池一", data: props.data.YAxis, type: "line", smooth: true, yAxisIndex: 0, showSymbol: false, areaStyle: { // 控制渐变的颜色区间 color: { type: "linear", x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: "rgba(25, 97, 162)" // 0% 处的颜色 }, { offset: 1, color: "rgba(25, 97, 162,0.2)" // 100% 处的颜色 } ], global: false // 缺省为 false }, // normal: { // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ // offset: 0, // color: '#00FFFF'//下层渐变 // }, { // offset: 0.8, // color: 'rgba(137, 189, 27, 0)' // }], false), // shadowColor: 'rgba(0, 0, 0, 0.1)', // shadowBlur: 10 // } }, itemStyle: { normal: { color: "#2980E0", lineStyle: { color: "#2980E0", }, }, }, }, // { // name: "前池二", // data: [0.2, 0.3, 0.1, 0.5, 0.6], // // data: [1, 1, 5, 6, 8, 9, 9, 4], // type: "line", // smooth: true, // yAxisIndex: 0, // showSymbol: false, // itemStyle: { // normal: { // color: "#1FDAC5", // lineStyle: { // color: "#1FDAC5", // }, // }, // }, // }, ], }; option && chartDom.setOption(option, true); state.chart = chartDom; }; watch( () => props.refresh, () => { if (state.chartDom) { state.chartDom.dispose(); state.chartDom = null; } setTimeout(() => { init(); }, 0); } ); onMounted(() => { // setTimeout(() => { // init(); // }, 1000); nextTick(() => { init(); }); window.addEventListener("resize", resizeTheChart); }); onBeforeUnmount(() => { if (state.chart) { state.chart.dispose(); } }); return { ...toRefs(state), resizeTheChart, init, }; }, }; </script> <style></style>