Dev Helpers
← Back

Stock movement sparkline

Import and render <StockMovementSparkline /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/StockMovementSparkline.tsx

export default function StockMovementSparkline({
  counts = [12, 0, 5, 18],
}: { counts?: number[] } = {}) {
  const max = Math.max(1, ...counts);
  return (
    <svg
      viewBox="0 0 100 30"
      role="img"
      aria-label={"Stock trend: " + counts.join(", ")}
    >
      <polyline
        fill="none"
        stroke="currentColor"
        points={counts
          .map(
            (v, i) =>
              (i * 100) / Math.max(1, counts.length - 1) + "," + (29 - (v / max) * 28),
          )
          .join(" ")}
      />
    </svg>
  );
}