import React, { useRef, useEffect, useState, useMemo } from "react"; import * as echarts from "echarts"; import { SearchOutlined } from "@ant-design/icons"; import _, { indexOf } from "lodash"; import moment from "moment"; import "antd/dist/antd.css"; import { Select, Button, DatePicker, Checkbox, message, Spin } from "antd"; import { getFactory, getEquipType, getEquip, getEquipmentLocation, getEquipmentLocationEchart, } from "../../../reportUtils/getQueryData"; const { Option } = Select; import theme from "../../../reportUtils/theme.json"; function EquipmentCurve() { const dataZoomRef = useRef(); let refinput = useRef(); echarts.registerTheme("black", theme); // 设备类型集合 const [equipType, setEquipType] = useState([]); // 车间集合 const [factory, setFactory] = useState([]); // 设备集合 const [equip, setEquip] = useState([]); //设备类型值 const [equipTypeValue, setEquipTypeValue] = useState(""); //车间值 const [factoryValue, setFactoryValue] = useState(""); //设备值 const [equipValue, setEquipValue] = useState(""); // 点位集合 const [locations, setLocations] = useState([]); // 点位变化值 const [locationValue, setLocationValue] = useState([]); // 时间变化值 const [dateValue, setDateValue] = useState(""); // 存放chartData const [chartData, setChartData] = useState({}); // 存放chartref const [chartRef, setChartRef] = useState([]); const [spin, setSpin] = useState(false); const equipTypeChange = (value) => { setEquipTypeValue(value); }; const factoryChange = (value) => { setFactoryValue(value); }; const equipChange = (value = "") => { if (value && equipValue !== value) { setLocationValue([]); setChartData({}); setChartRef([]); getEquipmentLocation(value).then((data) => { let locationArr = data .sort((a, b) => (a.iotfieldsort > b.iotfieldsort ? 1 : -1)) .map((item) => { return { label: item.iotfielddescribe, value: item.iotField, }; }); setLocations(locationArr); }); } else { setLocationValue([]); setChartData({}); setChartRef([]); setLocations([]); } setEquipValue(value); }; const locationChange = (checkValue) => { setLocationValue(checkValue); if (checkValue.length > 0) { doSearch(checkValue); } else { setChartRef([]); } }; const dateChange = (date, dateString) => { setDateValue(dateString); }; const handleCancelClick = () => { setLocationValue([]); setChartRef([]); }; const handleClick = () => { if (!equipValue) { message.warning("请选择需要查询的设备!"); } else if (!dateValue) { message.warning("请选择需要查询的时间!"); } else if (locationValue.length === 0) { message.warning("请勾选需要查询的点位!"); } else { setSpin(true); doSearch(); } }; // 查询设备类型,查询车间, useEffect(() => { getEquipType().then((data) => { setEquipType(data); }); getFactory().then((data) => { setFactory(data); }); }, []); // 查询设备 监听设备类型 useEffect(() => { getEquip({ equipTypeValue, factoryValue }).then((data) => { setEquip(data); setEquipValue(""); setLocations([]); setLocationValue([]); setChartData({}); setChartRef([]); }); }, [equipTypeValue, factoryValue]); useEffect(() => { let dataZoomChart = echarts.init(dataZoomRef.current); dataZoomChart.clear(); if (locationValue.length > 0) { if (!dataZoomRef.current) { return; } let dataZoomData = {}; let flag = true; for (let i = 0; i < locationValue.length; i++) { if (flag && chartData[locationValue[i]]) { dataZoomData = chartData[locationValue[i]]; flag = false; } } let dataZoomChart = echarts.init(dataZoomRef.current); let option = { xAxis: { type: "category", data: dataZoomData["times"] || [], }, yAxis: { type: "value", scale: true, }, legend: { show: false, height: "10", width: "2000", }, dataZoom: { top: "10", left: "0", type: "slider", width: "92%", height: "30px", }, series: [ { data: dataZoomData["values"] || [], type: "line", }, ], }; dataZoomChart.setOption(option); // 动态生成echart let childsEchart = []; for (let i = 0; i < chartRef.length; i++) { if (!chartData[chartRef[i]]) { break; } let dom = document.getElementById(chartRef[i]); let echatItem = echarts.init(dom, "black"); let optionItem = { grid: { top: "30", height: "125", }, title: { text: `${chartData[chartRef[i]]["title"]}`, x: "center", y: "top", textAlign: "left", }, tooltip: { trigger: "axis", axisPointer: { // link: null, animation: true, type: "cross", }, formatter: function (params) { //在此处直接用 formatter 属性 let showdata = params[0]; // 根据自己的需求返回数据 return `