import './index.less'; import React, { useMemo } from 'react'; import { Card, Row, Col, Icon, Tooltip } from 'antd'; import { CardProps } from 'antd/lib/card'; import { TinyLine } from '@ant-design/charts'; import { TinyLineConfig } from '@ant-design/charts/es/tinyLine'; interface IProps extends CardProps { name: string; help?: string; value: number | undefined; ratio: number | undefined; showChart?: boolean; chartConfig?: TinyLineConfig; compareData?: string[]; } export default ({ name, help, value, ratio, showChart = true, chartConfig, compareData, ...rest }: IProps) => { const height = chartConfig?.height || 60; const helpIcon = useMemo(() => { if (!help) return null; return ( ); }, [help]); const ratioRow = useMemo(() => { if (!ratio) return null; if (ratio > 0) { return ( <> 环比前一日 {Number(ratio).mul(100) + '%'} ); } if (ratio === 0) { return 环比前一日 持平; } return ( <> 环比前一日 {Math.abs(ratio).mul(100) + '%'} ); }, [ratio, value]); const lineChart = useMemo(() => { if (!showChart) return null; const config = { data: [], ...chartConfig, height, autoFit: true, smooth: true, tooltip: { showCrosshairs: true, showMarkers: true, customContent: function(x: string, data: any) { let value = data[0]?.data?.y; value = isNaN(Number(value)) ? 0 : Number(value); return value._toThousands(); }, }, }; if (compareData) { config.tooltip.customContent = function(x: string, data: any) { let value = data[0]?.data?.y; value = isNaN(Number(value)) ? 0 : Number(value); return `${compareData[Number(x)]}: ${value._toThousands()}`; }; } return ; }, [showChart, compareData, chartConfig]); const text = isNaN(Number(value)) ? value || '--' : Number(value)._toThousands(); return ( {name} {helpIcon} {text} {lineChart && (
{lineChart}
)}
{ratioRow}
); };