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 `
时间:${showdata.axisValueLabel}
数据:${showdata.data}
`; }, }, xAxis: { type: "category", data: chartData[chartRef[i]]["times"], }, yAxis: { type: "value", }, legend: { show: false, height: "10", width: "2000", }, dataZoom: { type: "inside", zoomOnMouseWheel: false, }, series: [ { symbol: "circle", symbolSize: 5, data: chartData[chartRef[i]]["values"], type: "line", }, ], }; echatItem.setOption(optionItem); childsEchart.push(echatItem); } echarts.connect([dataZoomChart].concatchildsEchart); } }, [chartRef]); return (
车间: 
设备类型: 
设备编号: 
查询日期: 
时间窗口选择框:
{locations.length > 0 ? ( ) : ( <> )}
{chartRef.map((item) => { return (
); })}
); function doSearch(checkValue) { let _locationValue = ``; if (checkValue) { _locationValue = checkValue; } else { _locationValue = locationValue; } let params = { workCenterCode:`${factoryValue}`, startTime: `${dateValue} 00:00:00`, endTime: `${dateValue} 23:59:59`, equipCode: equipValue, fields: _locationValue, }; getEquipmentLocationEchart(params).then((data) => { if (data && data.length > 0) { createCharts(); } else{ createChartsEmp(); message.warning("未能查询到数据!"); } setSpin(false); function createChartsEmp() { let locationsValues = _.cloneDeep(locations); let processData = {}; let commonTimes = []; _locationValue.map(key=> { if (key !== "time") { if (!processData.hasOwnProperty(key)) { let index = _.findIndex(locationsValues, function (o) { return o.value === key; }); let title = index >= 0 ? locationsValues[index]["label"] : ""; processData[key] = { title: title, key: key, times: [], values: [], }; } // processData[key]["values"].push([]); } // let time = moment(data[i]["time"]).format("YYYY-MM-DD HH:mm:ss"); // commonTimes.push(time); }) let domCharts = []; commonTimes = _.reverse(commonTimes); for (let key in processData) { processData[key]["times"] = commonTimes; } for (let i = 0; i < _locationValue.length; i++) { if (processData[_locationValue[i]]) { domCharts.push(_locationValue[i]); } } setChartData(processData); setChartRef(domCharts); } function createCharts() { let locationsValues = _.cloneDeep(locations); let processData = {}; let commonTimes = []; for (let i = 0; i < data.length; i++) { for (let key in data[i]) { if (key !== "time") { if (!processData.hasOwnProperty(key)) { let index = _.findIndex(locationsValues, function (o) { return o.value === key; }); let title = index >= 0 ? locationsValues[index]["label"] : ""; processData[key] = { title: title, key: key, times: [], values: [], }; } processData[key]["values"].push(data[i][key]); } } let time = moment(data[i]["time"]).format("YYYY-MM-DD HH:mm:ss"); commonTimes.push(time); } let domCharts = []; commonTimes = _.reverse(commonTimes); for (let key in processData) { processData[key]["times"] = commonTimes; } for (let i = 0; i < _locationValue.length; i++) { if (processData[_locationValue[i]]) { domCharts.push(_locationValue[i]); } } _locationValue.map(x=>{ if(domCharts.indexOf(x)===-1){ domCharts.push(x); let index = _.findIndex(locationsValues, function (o) { return o.value === x; }); let title = index >= 0 ? locationsValues[index]["label"] : ""; processData[x] = { title: title, key: x, times: [], values: [], }; processData[x]["times"] = commonTimes; } }) setChartData(processData); setChartRef(domCharts); } }); } } export default EquipmentCurve;