ebnsina

Chart

Component

About

A themed chart card with a title, a one-line takeaway, and an SVG plot (bar, line, area, pie, radar, radial) that draws itself in once scrolled into view, with optional center content and a custom tooltip.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Chart to your project and fits it to your theme.

Code

How Chart is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

import Chart from "@/components/ui/chart";
import { barY, defineChart } from "@tanstack/charts";
import { scaleBand } from "@tanstack/charts/scales/band";
import { scaleLinear } from "@tanstack/charts/scales/linear";
import { tooltip } from "@tanstack/charts/tooltip";

const returns = [
  { day: "Sat", onTime: 48, late: 6 },
  { day: "Sun", onTime: 52, late: 9 },
  { day: "Mon", onTime: 61, late: 4 },
  { day: "Tue", onTime: 57, late: 7 },
  { day: "Wed", onTime: 63, late: 5 },
  { day: "Thu", onTime: 44, late: 11 },
];
const nf = new Intl.NumberFormat();

const definition = defineChart({
  marks: [barY(returns, { x: "day", y: "onTime", radius: 4 })],
  scales: {
    x: {
      scale: () => scaleBand<string>().padding(0.3),
      axis: { label: "Day" },
    },
    y: {
      scale: scaleLinear,
      nice: true,
      grid: true,
      axis: { label: "Returned on time", ticks: { count: 4 } },
    },
  },
  tooltip,
});

export default function ChartTooltipDemo() {
  return (
    <div className="flex w-full items-center justify-center p-10">
      <div className="w-full max-w-xl">
        <Chart
          title="Returns this week"
          description="Thursday had the most late returns."
          definition={definition}
          tooltipBody={({ points }) => {
            const row = points[0]?.datum as
              (typeof returns)[number] | undefined;
            if (!row) return null;
            return (
              <div className="tip">
                <strong>{row.day}</strong>
                <span>
                  <i className="swatch on" />
                  On time <b>{nf.format(row.onTime)}</b>
                </span>
                <span>
                  <i className="swatch late" />
                  Late <b>{nf.format(row.late)}</b>
                </span>
              </div>
            );
          }}
        />
      </div>
    </div>
  );
}

Dependencies

More by ebnsina

View all
  • Countdown Timer

    Countdown Timer

    35
  • Video Player

    Video Player

    26
  • Alarm Clock

    Alarm Clock

    24
  • Hourglass Timer

    Hourglass Timer

    20
  • Voice Note

    Voice Note

    19
  • Sortable List

    Sortable List

    18
  • Sign In Auth Card

    Sign In Auth Card

    14
  • Split Button

    Split Button

    13