mirror of
https://github.com/temetro/temetro.git
synced 2026-08-10 10:37:43 +00:00
feat(analysis): add trend charts to the Analysis page
The Analysis page only showed KPI numbers. Add two real time-series and render them as dependency-free bar charts (matching components/chat/sparkline.tsx): - backend: GET /api/analytics now returns `trends.patientsByMonth` (new patients per month over the last 6 months) and `trends.appointmentsByWeekday` (appointments per day for the current week), bucketed in JS from one query each. - frontend: new components/analysis/bar-chart.tsx and two chart sections on the Analysis view (Patient growth, Appointments this week), with i18n keys. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import { type ReactNode, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { BarChart } from "@/components/analysis/bar-chart";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import { type Analytics, getAnalytics } from "@/lib/analytics";
|
||||
|
||||
@@ -45,6 +46,27 @@ function Section({
|
||||
);
|
||||
}
|
||||
|
||||
// A full-width section that frames a single chart in a card.
|
||||
function ChartSection({
|
||||
title,
|
||||
description,
|
||||
children,
|
||||
}: {
|
||||
title: string;
|
||||
description: string;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<section className="flex flex-col gap-3">
|
||||
<div>
|
||||
<h2 className="font-semibold text-lg tracking-tight">{title}</h2>
|
||||
<p className="text-muted-foreground text-sm">{description}</p>
|
||||
</div>
|
||||
<Card className="p-5">{children}</Card>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export function AnalysisView() {
|
||||
const { t } = useTranslation();
|
||||
const [data, setData] = useState<Analytics | null>(null);
|
||||
@@ -92,6 +114,16 @@ export function AnalysisView() {
|
||||
/>
|
||||
</Section>
|
||||
|
||||
<ChartSection
|
||||
description={t("analysis.charts.patientGrowthDescription")}
|
||||
title={t("analysis.charts.patientGrowthTitle")}
|
||||
>
|
||||
<BarChart
|
||||
data={data?.trends.patientsByMonth ?? []}
|
||||
emptyLabel={t("analysis.charts.empty")}
|
||||
/>
|
||||
</ChartSection>
|
||||
|
||||
<Section
|
||||
description={t("analysis.appointments.description")}
|
||||
title={t("analysis.appointments.title")}
|
||||
@@ -114,6 +146,16 @@ export function AnalysisView() {
|
||||
/>
|
||||
</Section>
|
||||
|
||||
<ChartSection
|
||||
description={t("analysis.charts.weeklyAppointmentsDescription")}
|
||||
title={t("analysis.charts.weeklyAppointmentsTitle")}
|
||||
>
|
||||
<BarChart
|
||||
data={data?.trends.appointmentsByWeekday ?? []}
|
||||
emptyLabel={t("analysis.charts.empty")}
|
||||
/>
|
||||
</ChartSection>
|
||||
|
||||
<Section
|
||||
description={t("analysis.prescriptions.description")}
|
||||
title={t("analysis.prescriptions.title")}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
"use client";
|
||||
|
||||
import type { TrendPoint } from "@/lib/analytics";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
// A small dependency-free vertical bar chart (matches the spirit of
|
||||
// components/chat/sparkline.tsx). Bars scale to the series max; each shows its
|
||||
// value above and its label below. Themed with semantic tokens.
|
||||
export function BarChart({
|
||||
data,
|
||||
className,
|
||||
emptyLabel,
|
||||
}: {
|
||||
data: TrendPoint[];
|
||||
className?: string;
|
||||
emptyLabel: string;
|
||||
}) {
|
||||
const max = Math.max(1, ...data.map((d) => d.count));
|
||||
const hasData = data.some((d) => d.count > 0);
|
||||
|
||||
if (data.length === 0 || !hasData) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"flex h-44 items-center justify-center text-muted-foreground text-sm",
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{emptyLabel}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("flex h-44 items-end gap-2 sm:gap-3", className)}>
|
||||
{data.map((d, i) => {
|
||||
const pct = (d.count / max) * 100;
|
||||
return (
|
||||
<div
|
||||
className="flex h-full flex-1 flex-col items-center gap-1.5"
|
||||
key={`${d.label}-${i}`}
|
||||
>
|
||||
<span className="font-medium text-foreground text-xs tabular-nums">
|
||||
{d.count}
|
||||
</span>
|
||||
<div className="flex w-full flex-1 items-end">
|
||||
<div
|
||||
className="w-full rounded-t-md bg-primary/80 transition-colors hover:bg-primary"
|
||||
style={{ height: `${Math.max(pct, d.count > 0 ? 4 : 0)}%` }}
|
||||
title={`${d.label}: ${d.count}`}
|
||||
/>
|
||||
</div>
|
||||
<span className="text-muted-foreground text-xs">{d.label}</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import { apiFetch } from "@/lib/api-client";
|
||||
|
||||
// Server-computed clinic analytics. Mirrors the backend `src/types/analytics.ts`.
|
||||
// All figures are aggregates over the active clinic's real data.
|
||||
export type TrendPoint = { label: string; count: number };
|
||||
|
||||
export type Analytics = {
|
||||
patients: {
|
||||
total: number;
|
||||
@@ -22,6 +24,10 @@ export type Analytics = {
|
||||
open: number;
|
||||
done: number;
|
||||
};
|
||||
trends: {
|
||||
patientsByMonth: TrendPoint[];
|
||||
appointmentsByWeekday: TrendPoint[];
|
||||
};
|
||||
};
|
||||
|
||||
export function getAnalytics(): Promise<Analytics> {
|
||||
|
||||
@@ -402,6 +402,13 @@
|
||||
"description": "Care-team workload",
|
||||
"open": "Open",
|
||||
"completed": "Completed"
|
||||
},
|
||||
"charts": {
|
||||
"patientGrowthTitle": "Patient growth",
|
||||
"patientGrowthDescription": "New patients per month over the last 6 months",
|
||||
"weeklyAppointmentsTitle": "Appointments this week",
|
||||
"weeklyAppointmentsDescription": "Bookings per day for the current week",
|
||||
"empty": "No data yet"
|
||||
}
|
||||
},
|
||||
"activity": {
|
||||
|
||||
Reference in New Issue
Block a user