All files / src/components/charts TimeSeriesChart.tsx

33.33% Statements 1/3
0% Branches 0/5
0% Functions 0/2
33.33% Lines 1/3

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                                                    3x                                                            
import React from "react";
import {
  Bar,
  BarChart,
  CartesianGrid,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";
 
import { AXIS } from "./theme";
import { TooltipCard } from "./TooltipCard";
 
/**
 * A count per day, as columns over a window: one series, one hue (the
 * accent, in `currentColor`), recessive axes, a per-column hover card. In a
 * file of its own, outside the kit's index, so a page test can stub it —
 * `ResponsiveContainer` needs a layout box jsdom does not have.
 */
export interface TimeSeriesPoint {
  /** The day, as the axis and hover show it: "23 Oct". */
  label: string;
  value: number;
}
 
const TimeSeriesChart: React.FC<{
  points: TimeSeriesPoint[];
  /** What a value counts: "answers". */
  unit: string;
  height?: number;
}> = ({ points, unit, height = 180 }) => (
  <div className="text-indigo-600 dark:text-indigo-500" style={{ height }}>
    <ResponsiveContainer width="100%" height="100%">
      <BarChart data={points} margin={{ top: 4, right: 4, bottom: 0, left: -20 }} barCategoryGap={2}>
        <CartesianGrid vertical={false} strokeDasharray="2 4" className="stroke-gray-200 dark:stroke-gray-700" />
        <XAxis dataKey="label" {...AXIS} interval="preserveStartEnd" minTickGap={24} />
        <YAxis {...AXIS} allowDecimals={false} width={44} />
        <Tooltip
          cursor={{ fill: "currentColor", fillOpacity: 0.08 }}
          content={({ active, payload }) =>
            active && payload?.length ? (
              <TooltipCard
                title={String((payload[0].payload as TimeSeriesPoint).label)}
                lines={[[unit, Number(payload[0].value).toLocaleString()]]}
              />
            ) : null
          }
        />
        <Bar dataKey="value" name={unit} fill="currentColor" radius={[4, 4, 0, 0]} isAnimationActive={false} />
      </BarChart>
    </ResponsiveContainer>
  </div>
);
 
export default TimeSeriesChart;