import * as echarts from 'echarts'; import React, { useEffect, useRef, useState, ForwardedRef, forwardRef, useImperativeHandle } from 'react'; import _ from 'lodash'; import { Spin } from "antd"; import HydrocarbonService from "../../tmpl/hcservice-v3"; import { DataConfigProps } from "../data"; const defOption = { title: { show: true, text: "30 %", x: 'center', y: 'center', textStyle: { color: '#ffffff', fontSize: 24, }, }, angleAxis: { axisLine: { show: false, }, axisLabel: { show: false, }, splitLine: { show: false, }, axisTick: { show: false, }, min: 0, max: 100, startAngle: 90, }, radiusAxis: { type: 'category', axisLine: { show: false, }, axisTick: { show: false, }, axisLabel: { show: true, }, data: [], }, polar: { radius: '150%', center: ['50%', '50%'], }, series: [ // 极坐标系下柱状图 { // 实际值 type: 'bar', data: [30], z: 1, coordinateSystem: 'polar', barWidth: 24, roundCap: 1, color: "#446bf5" }, { // 背景图形 type: 'bar', data: [100], z: 0, silent: true, coordinateSystem: 'polar', barWidth: 24, roundCap: true, color: '#E8EDF3', barGap: '-100%', }, ], }; export interface ComponentStyle { title?: { [key: string]: any }; barWidth?: number; color?: string; backgroundColor?: string; renderer?: "canvas" | "svg"; } 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(0); 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 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, value } = val.initData; const temData = value?.[0]?.value?.[0]?.v1 || 0; setData(temData * 100); if (config?.requestMode === "polling") { pollingIntervalRef.current = setInterval(() => { getData() }, config?.pollingInterval || 60000); } } } // ws订阅数据更新 const handleWSData = (val: DataConfigProps) => { if (val.sourceType === "sourceId" && val.wsData) { const { value } = val.initData; const temData = value?.[0]?.value?.[0]?.v1 || 0; setData(temData * 100); } } 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 { config, value } = data || {}; const temData = value?.[0]?.value?.[0]?.v1 || 0; setData(temData * 100); return 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') { const { value } = dataConfig.staticData || {}; setData((value || 0) * 100); } }, [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(0); } }, [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 { title, color, backgroundColor, barWidth } = config; const option = _.cloneDeep(defOption); // 编辑态数据源类型显示默认数据30 const temData = isDesignMode && dataConfig?.sourceType === 'sourceId' ? 30 : (data || 0); option.title = { ...option.title, ...title, text: `${temData || "-"}%` }; option.series = option.series.map((item: any, index) => { index === 0 && (item.data = [temData]); item.color = index === 0 ? color : backgroundColor; item.barWidth = barWidth; return item; }); 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;