Animated Line Chart
Line chart whose SVG path draws on from left to right with a leading dot.
Source
components/kuchi/animated-line-chart.tsx
1"use client";23import { spring, useCurrentFrame, useVideoConfig } from "remotion";45export interface AnimatedLineChartProps {6 data?: number[];7 width?: number;8 height?: number;9 strokeColor?: string;10 strokeWidth?: number;11 gridColor?: string;12 showDot?: boolean;13 speed?: number;14 className?: string;15}1617export function AnimatedLineChart({18 data = [12, 19, 8, 15, 22, 18, 28, 25, 32],19 width = 1000,20 height = 500,21 strokeColor = "#22c55e",22 strokeWidth = 4,23 gridColor = "#27272a",24 showDot = true,25 speed = 1,26 className,27}: AnimatedLineChartProps) {28 const frame = useCurrentFrame() * speed;29 const { fps, durationInFrames } = useVideoConfig();3031 const padding = 60;32 const innerWidth = width - padding * 2;33 const innerHeight = height - padding * 2;3435 const min = Math.min(...data);36 const max = Math.max(...data);37 const range = max - min || 1;3839 const points = data.map((value, index) => {40 const x = padding + (index / (data.length - 1)) * innerWidth;41 const y = padding + innerHeight - ((value - min) / range) * innerHeight;42 return { x, y };43 });4445 // Analytical path length: sum of segment distances46 let pathLength = 0;47 for (let i = 1; i < points.length; i++) {48 const dx = points[i].x - points[i - 1].x;49 const dy = points[i].y - points[i - 1].y;50 pathLength += Math.sqrt(dx * dx + dy * dy);51 }5253 const d = points54 .map((p, i) => `${i === 0 ? "M" : "L"}${p.x.toFixed(2)},${p.y.toFixed(2)}`)55 .join(" ");5657 // Spring-driven progress: smooth, no bounce. Matches the "smooth" preset58 // from remotion-best-practices/timing.md.59 const progress = spring({60 frame,61 fps,62 durationInFrames: Math.round(durationInFrames * 0.85),63 config: { damping: 200 },64 });6566 const dashOffset = pathLength * (1 - progress);6768 // Find dot position along the path at current progress69 const targetLen = pathLength * progress;70 let traveled = 0;71 let dotX = points[0].x;72 let dotY = points[0].y;73 for (let i = 1; i < points.length; i++) {74 const dx = points[i].x - points[i - 1].x;75 const dy = points[i].y - points[i - 1].y;76 const segLen = Math.sqrt(dx * dx + dy * dy);77 if (traveled + segLen >= targetLen) {78 const t = (targetLen - traveled) / segLen;79 dotX = points[i - 1].x + dx * t;80 dotY = points[i - 1].y + dy * t;81 break;82 }83 traveled += segLen;84 dotX = points[i].x;85 dotY = points[i].y;86 }8788 const gridRows = 4;89 const gridCols = data.length - 1;9091 return (92 <div93 className={className}94 style={{95 position: "absolute",96 inset: 0,97 display: "flex",98 alignItems: "center",99 justifyContent: "center",100 fontFamily:101 "var(--font-geist-sans), -apple-system, BlinkMacSystemFont, sans-serif",102 }}103 >104 <svg105 width={width}106 height={height}107 viewBox={`0 0 ${width} ${height}`}108 style={{ overflow: "visible" }}109 >110 {/* Horizontal grid */}111 {Array.from({ length: gridRows + 1 }).map((_, i) => {112 const y = padding + (i / gridRows) * innerHeight;113 return (114 <line115 key={`h-${i}`}116 x1={padding}117 x2={padding + innerWidth}118 y1={y}119 y2={y}120 stroke={gridColor}121 strokeWidth={1}122 />123 );124 })}125 {/* Vertical grid */}126 {Array.from({ length: gridCols + 1 }).map((_, i) => {127 const x = padding + (i / gridCols) * innerWidth;128 return (129 <line130 key={`v-${i}`}131 x1={x}132 x2={x}133 y1={padding}134 y2={padding + innerHeight}135 stroke={gridColor}136 strokeWidth={1}137 />138 );139 })}140 {/* Axis */}141 <line142 x1={padding}143 x2={padding}144 y1={padding}145 y2={padding + innerHeight}146 stroke={gridColor}147 strokeWidth={2}148 />149 <line150 x1={padding}151 x2={padding + innerWidth}152 y1={padding + innerHeight}153 y2={padding + innerHeight}154 stroke={gridColor}155 strokeWidth={2}156 />157158 {/* Animated line */}159 <path160 d={d}161 fill="none"162 stroke={strokeColor}163 strokeWidth={strokeWidth}164 strokeLinecap="round"165 strokeLinejoin="round"166 strokeDasharray={pathLength}167 strokeDashoffset={dashOffset}168 style={{169 filter: `drop-shadow(0 0 12px ${strokeColor}55)`,170 }}171 />172173 {showDot && progress > 0 && progress < 1 && (174 <circle175 cx={dotX}176 cy={dotY}177 r={strokeWidth * 2}178 fill={strokeColor}179 style={{180 filter: `drop-shadow(0 0 8px ${strokeColor})`,181 }}182 />183 )}184 </svg>185 </div>186 );187}









