|
| 1 | +import { useMemo } from 'react' |
| 2 | + |
| 3 | +interface DataPoint { |
| 4 | + iteration: number |
| 5 | + erlang: number |
| 6 | + blocking_prob: number |
| 7 | +} |
| 8 | + |
| 9 | +interface ProgressChartProps { |
| 10 | + data: DataPoint[] |
| 11 | + height?: number |
| 12 | +} |
| 13 | + |
| 14 | +export function ProgressChart({ data, height = 200 }: ProgressChartProps) { |
| 15 | + const chartData = useMemo(() => { |
| 16 | + if (data.length === 0) return null |
| 17 | + |
| 18 | + const maxBlocking = Math.max(...data.map((d) => d.blocking_prob), 0.01) |
| 19 | + const width = 100 |
| 20 | + |
| 21 | + // Normalize data points |
| 22 | + const points = data.map((d, i) => ({ |
| 23 | + x: (i / Math.max(data.length - 1, 1)) * width, |
| 24 | + y: height - (d.blocking_prob / maxBlocking) * (height - 20) - 10, |
| 25 | + value: d.blocking_prob, |
| 26 | + erlang: d.erlang, |
| 27 | + })) |
| 28 | + |
| 29 | + // Create path |
| 30 | + const pathD = points |
| 31 | + .map((p, i) => `${i === 0 ? 'M' : 'L'} ${p.x} ${p.y}`) |
| 32 | + .join(' ') |
| 33 | + |
| 34 | + return { points, pathD, maxBlocking } |
| 35 | + }, [data, height]) |
| 36 | + |
| 37 | + if (!chartData || data.length < 2) { |
| 38 | + return ( |
| 39 | + <div |
| 40 | + className="flex items-center justify-center bg-gray-50 rounded-lg border border-gray-200" |
| 41 | + style={{ height }} |
| 42 | + > |
| 43 | + <span className="text-sm text-gray-400"> |
| 44 | + Waiting for progress data... |
| 45 | + </span> |
| 46 | + </div> |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + const latestValue = data[data.length - 1] |
| 51 | + |
| 52 | + return ( |
| 53 | + <div className="bg-white rounded-lg border border-gray-200 p-4"> |
| 54 | + <div className="flex items-center justify-between mb-2"> |
| 55 | + <span className="text-sm font-medium text-gray-700">Blocking Probability</span> |
| 56 | + <span className="text-sm text-gray-500"> |
| 57 | + Current: <span className="font-mono font-medium text-fusion-600"> |
| 58 | + {(latestValue.blocking_prob * 100).toFixed(4)}% |
| 59 | + </span> |
| 60 | + </span> |
| 61 | + </div> |
| 62 | + |
| 63 | + <svg |
| 64 | + viewBox={`0 0 100 ${height}`} |
| 65 | + className="w-full" |
| 66 | + style={{ height }} |
| 67 | + preserveAspectRatio="none" |
| 68 | + > |
| 69 | + {/* Grid lines */} |
| 70 | + <g className="text-gray-200"> |
| 71 | + {[0, 0.25, 0.5, 0.75, 1].map((ratio) => ( |
| 72 | + <line |
| 73 | + key={ratio} |
| 74 | + x1="0" |
| 75 | + y1={10 + ratio * (height - 20)} |
| 76 | + x2="100" |
| 77 | + y2={10 + ratio * (height - 20)} |
| 78 | + stroke="currentColor" |
| 79 | + strokeWidth="0.5" |
| 80 | + /> |
| 81 | + ))} |
| 82 | + </g> |
| 83 | + |
| 84 | + {/* Area fill */} |
| 85 | + <path |
| 86 | + d={`${chartData.pathD} L 100 ${height - 10} L 0 ${height - 10} Z`} |
| 87 | + fill="url(#gradient)" |
| 88 | + opacity="0.3" |
| 89 | + /> |
| 90 | + |
| 91 | + {/* Line */} |
| 92 | + <path |
| 93 | + d={chartData.pathD} |
| 94 | + fill="none" |
| 95 | + stroke="#0284c7" |
| 96 | + strokeWidth="2" |
| 97 | + strokeLinecap="round" |
| 98 | + strokeLinejoin="round" |
| 99 | + vectorEffect="non-scaling-stroke" |
| 100 | + /> |
| 101 | + |
| 102 | + {/* Latest point */} |
| 103 | + {chartData.points.length > 0 && ( |
| 104 | + <circle |
| 105 | + cx={chartData.points[chartData.points.length - 1].x} |
| 106 | + cy={chartData.points[chartData.points.length - 1].y} |
| 107 | + r="3" |
| 108 | + fill="#0284c7" |
| 109 | + vectorEffect="non-scaling-stroke" |
| 110 | + /> |
| 111 | + )} |
| 112 | + |
| 113 | + {/* Gradient definition */} |
| 114 | + <defs> |
| 115 | + <linearGradient id="gradient" x1="0" y1="0" x2="0" y2="1"> |
| 116 | + <stop offset="0%" stopColor="#0284c7" /> |
| 117 | + <stop offset="100%" stopColor="#0284c7" stopOpacity="0" /> |
| 118 | + </linearGradient> |
| 119 | + </defs> |
| 120 | + </svg> |
| 121 | + |
| 122 | + <div className="flex justify-between text-xs text-gray-400 mt-1"> |
| 123 | + <span>Erlang: {data[0]?.erlang?.toFixed(1) ?? '-'}</span> |
| 124 | + <span>Erlang: {latestValue.erlang?.toFixed(1) ?? '-'}</span> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ) |
| 128 | +} |
0 commit comments