All files / react-base/src/components/ui line-chart.tsx

0% Statements 0/38
0% Branches 0/1
0% Functions 0/1
0% Lines 0/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import { CartesianGrid, Line, LineChart as RechartsLineChart, XAxis, YAxis } from "recharts";
import {
  ChartContainer,
  ChartLegend,
  ChartLegendContent,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@/components/ui/chart";
import { cn } from "@/lib/utils";
 
export type LineChartDataPoint = Record<string, string | number>;
 
type LineChartProps = {
  data: LineChartDataPoint[];
  config: ChartConfig;
  dataKeys: string[];
  categoryKey: string;
  className?: string;
  showLegend?: boolean;
  showDots?: boolean;
};
 
export function LineChart({
  data,
  config,
  dataKeys,
  categoryKey,
  className,
  showLegend = true,
  showDots = true,
}: LineChartProps) {
  return (
    <ChartContainer config={config} className={cn("aspect-auto h-[300px] w-full", className)}>
      <RechartsLineChart
        accessibilityLayer
        data={data}
        margin={{ left: 12, right: 12, top: 12, bottom: 0 }}
      >
        <CartesianGrid vertical={false} strokeDasharray="3 3" />
        <XAxis
          dataKey={categoryKey}
          tickLine={false}
          axisLine={false}
          tickMargin={8}
          tickFormatter={(value) => String(value).slice(0, 3)}
        />
        <YAxis tickLine={false} axisLine={false} tickMargin={8} width={40} />
        <ChartTooltip cursor={false} content={<ChartTooltipContent indicator="line" />} />
        {showLegend && <ChartLegend content={<ChartLegendContent />} />}
        {dataKeys.map((key) => (
          <Line
            key={key}
            dataKey={key}
            type="monotone"
            stroke={`var(--color-${key})`}
            strokeWidth={2}
            dot={showDots ? { fill: `var(--color-${key})`, r: 3 } : false}
            activeDot={{ r: 5 }}
          />
        ))}
      </RechartsLineChart>
    </ChartContainer>
  );
}