import * as echarts from 'echarts'; import React, { useEffect, useRef, useState, ForwardedRef, forwardRef, useImperativeHandle } from 'react'; import _ from 'lodash'; import { Spin } from "antd"; import { useLocale } from "../../locale/useLocale"; import HydrocarbonService from "../../tmpl/hcservice-v3"; import { DataConfigProps } from "../data"; const defOption = { title: { text: '暂无数据', left: 'center', top: 'middle', show: false, // 无数据时显示 }, tooltip: {}, grid: { top: 30, let: 30, right: 30, bottom: 30 }, legend: { show: false }, shape: "polygon", radar: { indicator: [ { name: 'A', max: 6500 }, { name: 'B', max: 16000 }, { name: 'C', max: 30000 }, { name: 'D', max: 38000 }, { name: 'E', max: 52000 }, { name: 'F', max: 25000 } ], splitArea: { areaStyle: { color: 'transparent', }, }, axisName: { color: '#afb6d3', fontsize: 12, fontWeight: "normal", }, axisLine: { lineStyle: { color: '#384278', }, }, splitLine: { lineStyle: { color: '#384278', }, }, }, series: [ { type: 'radar', data: [{ value: [4200, 3000, 20000, 35000, 50000, 18000], name: 'A' }, { value: [5000, 14000, 28000, 26000, 42000, 21000], name: 'B' }] } ] }; const legends = { top: { top: 0 }, bottom: { bottom: 0 }, left: { left: 5, top: "center" }, right: { right: 5, top: "center" }, leftTop: { left: 5 }, rightTop: { right: 5 }, leftBottom: { left: 5, bottom: 0 }, rightBottom: { right: 5, bottom: 0 }, }; // 处理关系字段显示 const handleRelationVal = (val: string) => { if (!val) { return val; } if (typeof val === "string" && val?.includes("@R@")) { return val.split("@R@")[1]; } else { return val; } } // 处理数据格式 const handleData = (data: { config: { [key: string]: string }, value: { [key: string]: any }[] }[]) => { if (!data?.length) { return []; } const temData = data.map((r) => { const { config, value } = r; const isD = Object.keys(config).some(r => r.includes("d")); // 过滤只有v1、v2等数据 const vNames = Object.keys(config).filter((r) => r.startsWith("v") && !r.includes("-")).sort((a, b) => parseInt(a.substring(1)) - parseInt(b.substring(1))); return isD ? { name: config.d1, data: vNames.map((r) => ({ name: config[r], data: value.map((v) => [handleRelationVal(v["d1"]), v[r]]), })) } : { data: value.map((r, index) => ({ name: `数值${index || ""}`, data: vNames.map(v => [config[v], r[v]]) })) }; }); return temData[0]?.data || []; }; export interface ComponentStyle { tooltip?: boolean; color?: string[]; grid?: { top: number, let: number, right: number, bottom: number }; legend?: { [key: string]: any }; renderer?: "canvas" | "svg"; lineStyle?: { width: number }; shape?: string; isArea?: boolean; areaStyle?: { opacity: number }; splitAreaColor?: string; axisName?: { color: string, fontsize: number, fontWeight: string, }, axisLineColor?: string, splitLineColor?: string, } export interface ComponentProps { style?: ComponentStyle; base: { width: number, height: number }; data?: DataConfigProps; isDesignMode?: boolean; // 是否编辑模式 initSearchParams?: { [key: string]: any }; // 全局初始化外部参数,例:传参包含设备id,则在查询时带上设备id } export interface ComponentRef { updateConfig: (newConfig: ComponentProps) => void; destroy: () => void; } const Index = forwardRef((props: ComponentProps, ref: ForwardedRef) => { const { style, data: propsData, isDesignMode } = props; const [config, setConfig] = useState(style || {}); const [renderType, setRenderType] = useState<"canvas" | "svg">(style?.renderer || "svg"); // 渲染模式 const [size, setSize] = useState<{ width: number, height: number }>(); const [dataConfig, setDataConfig] = useState({ ...propsData }); // 数据源配置 const [data, setData] = useState<{ name: string, data: any }[]>([]); const [searchParams, setSearchParams] = useState<{ [key: string]: any }>(); const [initSearchParams, setInitSearchParams] = useState<{ [key: string]: any }>(); const [loading, setLoading] = useState(false); const chartRef = useRef(null); const chart = useRef(null); const pollingIntervalRef = useRef(null); const { translate } = useLocale(); const handleDestroy = () => { pollingIntervalRef.current && clearInterval(pollingIntervalRef.current); window.removeEventListener('resize', handleResize); chart.current && chart.current.dispose(); }; // 全局初始化请求数据处理 const handleInitData = (val: DataConfigProps) => { if (val.sourceType === "sourceId" && val.isInit && val.initData) { const { config: dataConfig, value } = val.initData; const temData = handleData(value); setData(temData); if (dataConfig?.requestMode === "polling") { pollingIntervalRef.current = setInterval(() => { getData() }, dataConfig?.pollingInterval || 60000); } } } // ws订阅数据更新 const handleWSData = (val: DataConfigProps) => { if (val.sourceType === "sourceId" && val.wsData) { const { value } = val.initData; const temData = handleData(value); setData(temData); } } useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { const { base, style: newStyle, data: newDataConfig, initSearchParams: newISParams } = newConfig; setConfig({ ...(newStyle || {}) }); setSize({ width: base.width, height: base.height }); setDataConfig(newDataConfig); setRenderType(newStyle?.renderer || "svg"); handleInitData(newDataConfig); setSearchParams(newDataConfig.searchParams); setInitSearchParams(newISParams); handleWSData(newDataConfig); }, destroy: handleDestroy, })); // 窗口大小变化时重新调整图表大小 const handleResize = () => { chart.current.resize({ animation: { duration: 500 } }); }; useEffect(() => { return () => { handleDestroy(); } }, []); useEffect(() => { handleDestroy(); chart.current = echarts.init(chartRef.current, null, { renderer: renderType }); window.addEventListener('resize', handleResize); }, [renderType]); // 请求数据 const getData = async (params?: object) => { setLoading(true); const { data } = await HydrocarbonService.requestChartData(null, dataConfig?.sourceId, params) || {}; setLoading(false); const temData = handleData(data?.value); setData(temData); return data?.config; }; // 初始化请求数据 const getInitData = async (params?: object) => { pollingIntervalRef.current && clearInterval(pollingIntervalRef.current); const config = await getData(params); if (config?.requestMode === "polling") { pollingIntervalRef.current = setInterval(() => getData(params), config?.pollingInterval || 60000); } }; // 静态数据处理 useEffect(() => { if (dataConfig?.sourceType === 'staticData') { setData(handleData(dataConfig.staticData)); } }, [dataConfig?.sourceType, dataConfig?.staticData]); /** * 数据源数据请求 * 1、编辑态不请求数据 */ useEffect(() => { if (!isDesignMode && dataConfig?.sourceType === 'sourceId' && dataConfig?.sourceId && !dataConfig?.isInit && dataConfig?.isDefRequest !== false) { if (dataConfig?.isInitParams) { // 组件数据源接收外部参数 initSearchParams && getInitData(initSearchParams); } else { getInitData(); } } else if (dataConfig?.sourceType === 'sourceId') { setData([]); } }, [dataConfig?.sourceType, dataConfig?.sourceId, initSearchParams]); // 搜索参数变化时重新请求数据 useEffect(() => { if (searchParams) { const params = { ...(initSearchParams || {}), ...searchParams }; if (dataConfig?.isInitParams) { // 数据源接收外部参数,则参数存在再请求 initSearchParams && getInitData(params); } else { getInitData(params); } } }, [searchParams, initSearchParams]); const renderChart = () => { const { grid, legend, tooltip, color, lineStyle, isArea, areaStyle, shape, splitAreaColor, axisName, axisLineColor, splitLineColor } = config; const option = _.cloneDeep(defOption); // 编辑态无数据显示默认数据 const temData = isDesignMode && !data?.length ? option.series : data; const temMax = Math.max(...data.map(r => r.data?.map(item => item[1]) || []).flat()); const temIndicator = data?.[0]?.data?.map(r => ({ name: r[0], max: temMax })); option.shape = shape; option.grid = grid; option.legend = { ...legend, ...legends[legend?.position] }; option.tooltip = tooltip ? {} : undefined; option.title.text = translate("${暂无数据}"); option.title.show = !temData?.length; option.radar = isDesignMode && !data?.length ? option.radar : { ...option.radar, splitArea: { areaStyle: { color: splitAreaColor || 'transparent', }, }, axisName: axisName || { color: '#afb6d3', fontsize: 12, fontWeight: "normal", }, axisLine: { lineStyle: { color: axisLineColor || '#384278', }, }, splitLine: { lineStyle: { color: splitLineColor || '#384278', }, }, indicator: temIndicator }; option.series = isDesignMode && !data?.length ? option.series : temData.map((r, index) => ({ name: r.name, type: "radar", lineStyle: { ...lineStyle, color: color[index] }, itemStyle: { color: color[index] }, areaStyle: isArea ? areaStyle : undefined, data: [{ name: r.name, value: r.data?.map(item => item[1]) }] })); chart.current.setOption(option, true); }; useEffect(() => { if (config) { renderChart(); } }, [config, data]); useEffect(() => { if (size) { handleResize(); } }, [size]); const renderChartDom = () =>
; if (isDesignMode) { return renderChartDom(); } return {renderChartDom()}; }); export default Index;