|
| 1 | +import { type JSX, useMemo } from 'react'; |
| 2 | +import { PopoutCard } from '@/components/Layout/PopoutCard'; |
| 3 | +import { MultiLineChart } from '@/components/Charts/MultiLine'; |
| 4 | +import type { SeriesData } from '@/components/Charts/MultiLine/MultiLine.types'; |
| 5 | +import { useThemeColors } from '@/hooks/useThemeColors'; |
| 6 | +import { getClassificationLabel, CLASSIFICATION_DESCRIPTIONS } from '@/utils/classification'; |
| 7 | +import type { BlockClassificationCDFChartProps } from './BlockClassificationCDFChart.types'; |
| 8 | + |
| 9 | +/** |
| 10 | + * BlockClassificationCDFChart - Visualizes cumulative distribution of block propagation by node classification |
| 11 | + * |
| 12 | + * Shows CDF (Cumulative Distribution Function) curves for each classification type: |
| 13 | + * - individual |
| 14 | + * - corporate |
| 15 | + * - internal |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```tsx |
| 19 | + * <BlockClassificationCDFChart |
| 20 | + * blockPropagationData={[ |
| 21 | + * { seen_slot_start_diff: 145, classification: 'individual' }, |
| 22 | + * { seen_slot_start_diff: 230, classification: 'corporate' }, |
| 23 | + * ... |
| 24 | + * ]} |
| 25 | + * /> |
| 26 | + * ``` |
| 27 | + */ |
| 28 | +export function BlockClassificationCDFChart({ blockPropagationData }: BlockClassificationCDFChartProps): JSX.Element { |
| 29 | + const themeColors = useThemeColors(); |
| 30 | + |
| 31 | + // Process data into CDF series by classification |
| 32 | + const cdfSeries = useMemo(() => { |
| 33 | + if (blockPropagationData.length === 0) { |
| 34 | + return []; |
| 35 | + } |
| 36 | + |
| 37 | + // Group data by classification |
| 38 | + const classificationGroups = new Map<string, number[]>(); |
| 39 | + blockPropagationData.forEach(point => { |
| 40 | + const classification = point.classification || 'unclassified'; |
| 41 | + if (!classificationGroups.has(classification)) { |
| 42 | + classificationGroups.set(classification, []); |
| 43 | + } |
| 44 | + classificationGroups.get(classification)!.push(point.seen_slot_start_diff); |
| 45 | + }); |
| 46 | + |
| 47 | + // Define classification colors to match the badge colors in classification.ts |
| 48 | + const classificationColors: Record<string, string> = { |
| 49 | + individual: themeColors.primary, |
| 50 | + corporate: '#a855f7', // purple-500 |
| 51 | + internal: themeColors.success, |
| 52 | + unclassified: themeColors.muted, |
| 53 | + }; |
| 54 | + |
| 55 | + // Create CDF series for each classification |
| 56 | + const series: SeriesData[] = []; |
| 57 | + classificationGroups.forEach((times, classification) => { |
| 58 | + // Sort times for CDF calculation |
| 59 | + const sortedTimes = [...times].sort((a, b) => a - b); |
| 60 | + const totalNodes = sortedTimes.length; |
| 61 | + |
| 62 | + // Calculate CDF: [time in seconds, cumulative percentage] |
| 63 | + const cdfData: Array<[number, number]> = sortedTimes.map((time, index) => [ |
| 64 | + time / 1000, // Convert ms to seconds |
| 65 | + ((index + 1) / totalNodes) * 100, |
| 66 | + ]); |
| 67 | + |
| 68 | + series.push({ |
| 69 | + name: getClassificationLabel(classification), |
| 70 | + data: cdfData, |
| 71 | + color: classificationColors[classification] || themeColors.muted, |
| 72 | + smooth: true, |
| 73 | + lineWidth: 2, |
| 74 | + showSymbol: false, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + // Sort series by name for consistent legend order |
| 79 | + return series.sort((a, b) => a.name.localeCompare(b.name)); |
| 80 | + }, [blockPropagationData, themeColors]); |
| 81 | + |
| 82 | + // Handle empty data |
| 83 | + if (blockPropagationData.length === 0) { |
| 84 | + return ( |
| 85 | + <PopoutCard title="Block Propagation by Classification (CDF)" anchorId="block-classification-cdf" modalSize="xl"> |
| 86 | + {({ inModal }) => ( |
| 87 | + <div |
| 88 | + className={ |
| 89 | + inModal |
| 90 | + ? 'flex h-96 items-center justify-center text-muted' |
| 91 | + : 'flex h-72 items-center justify-center text-muted' |
| 92 | + } |
| 93 | + > |
| 94 | + <p>No block propagation data available</p> |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </PopoutCard> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const subtitle = `Cumulative distribution of block arrival times by node classification`; |
| 102 | + |
| 103 | + return ( |
| 104 | + <PopoutCard |
| 105 | + title="Block Propagation by Classification (CDF)" |
| 106 | + anchorId="block-classification-cdf" |
| 107 | + subtitle={subtitle} |
| 108 | + modalSize="xl" |
| 109 | + > |
| 110 | + {({ inModal }) => ( |
| 111 | + <div className="space-y-4"> |
| 112 | + {/* Chart */} |
| 113 | + <MultiLineChart |
| 114 | + series={cdfSeries} |
| 115 | + xAxis={{ |
| 116 | + type: 'value', |
| 117 | + name: 'Slot Time (s)', |
| 118 | + min: 0, |
| 119 | + max: 12, |
| 120 | + }} |
| 121 | + yAxis={{ |
| 122 | + name: 'Cumulative %', |
| 123 | + min: 0, |
| 124 | + max: 100, |
| 125 | + }} |
| 126 | + height={inModal ? 384 : 288} |
| 127 | + showLegend={true} |
| 128 | + legendPosition="bottom" |
| 129 | + useNativeLegend={true} |
| 130 | + tooltipTrigger="axis" |
| 131 | + syncGroup="slot-time" |
| 132 | + /> |
| 133 | + |
| 134 | + {/* Classification Legend */} |
| 135 | + <div className="rounded-sm bg-muted/10 px-4 py-3"> |
| 136 | + <p className="mb-2 text-xs font-medium text-muted">Node Classifications:</p> |
| 137 | + <div className="space-y-1.5"> |
| 138 | + {(['individual', 'corporate', 'internal'] as const).map(cls => ( |
| 139 | + <div key={cls} className="flex items-start gap-2 text-xs"> |
| 140 | + <span className="mt-0.5 shrink-0 font-semibold text-foreground"> |
| 141 | + {CLASSIFICATION_DESCRIPTIONS[cls].label}: |
| 142 | + </span> |
| 143 | + <span className="text-muted">{CLASSIFICATION_DESCRIPTIONS[cls].description}</span> |
| 144 | + </div> |
| 145 | + ))} |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </PopoutCard> |
| 151 | + ); |
| 152 | +} |
0 commit comments