diff --git a/src/client/components/TimeEventChart.tsx b/src/client/components/TimeEventChart.tsx index 47bdc18..dc3bfe4 100644 --- a/src/client/components/TimeEventChart.tsx +++ b/src/client/components/TimeEventChart.tsx @@ -1,6 +1,6 @@ import { useTheme } from '../hooks/useTheme'; import { DateUnit } from '@tianji/shared'; -import React from 'react'; +import React, { useState } from 'react'; import { formatDateWithUnit } from '../utils/date'; import { Area, @@ -42,6 +42,7 @@ export const TimeEventChart: React.FC<{ const lineDasharray = strokeDasharray.find((s) => s.name === name); return lineDasharray ? lineDasharray.strokeDasharray : undefined; }; + const [selectedItem, setSelectedItem] = useState(['pv', 'uv']); return ( @@ -65,10 +66,27 @@ export const TimeEventChart: React.FC<{ tickFormatter={(text) => formatDateWithUnit(text, props.unit)} /> - } /> + { + setSelectedItem((selected) => { + if (selected.includes(item.value)) { + return selected.filter((s) => s !== item.value); + } else { + return [...selected, item.value]; + } + }); + }} + /> + } + /> } /> + + [number] const ChartLegendContent = React.forwardRef< HTMLDivElement, React.ComponentProps<"div"> & Pick & { hideIcon?: boolean nameKey?: string + } & { + selectedItem?: string[] + onItemClick?: (item: Payload) => void } >( ( - { className, hideIcon = false, payload, verticalAlign = "bottom", nameKey }, + { className, hideIcon = false, payload, verticalAlign = "bottom", nameKey, selectedItem, onItemClick }, ref ) => { const { config } = useChart() @@ -296,8 +300,11 @@ const ChartLegendContent = React.forwardRef<
svg]:h-3 [&>svg]:w-3 [&>svg]:text-muted-foreground" + "flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3 [&>svg]:text-muted-foreground", + typeof onItemClick === 'function' && 'cursor-pointer', + selectedItem?.includes(item.value) && 'font-bold', )} + onClick={() => onItemClick?.(item)} > {itemConfig?.icon && !hideIcon ? (