Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bar charts #39

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 33 additions & 4 deletions app/components/TableCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,34 @@ import {

import { Card } from "~/components/ui/card";

type CountByProperty = [string, string][];

function calculateCountPercentages(countByProperty: CountByProperty) {
const totalCount = countByProperty.reduce(
(sum, row) => sum + parseInt(row[1]),
0
);

return countByProperty.map((row) => {
const count = parseInt(row[1]);
const percentage = ((count / totalCount) * 100).toFixed(2);
return `${percentage}%`;
});
}
export default function TableCard({
countByProperty,
columnHeaders,
}: InferProps<typeof TableCard.propTypes>) {
const barChartPercentages = calculateCountPercentages(
countByProperty as CountByProperty
);
return (
<Card>
<Table>
<TableHeader>
<TableRow>
{/* empty header required otherwise column for the bar chart wont render */}
<th />
{(columnHeaders || []).map((header: string, index) => (
<TableHead
key={header}
Expand All @@ -33,12 +52,22 @@ export default function TableCard({
</TableRow>
</TableHeader>
<TableBody>
{(countByProperty || []).map((item) => (
<TableRow key={item[0]}>
<TableCell className="font-medium">
{(countByProperty || []).map((item, key) => (
<TableRow
key={item[0]}
className="relative group [&_td]:last:rounded-b-md"
>
{/* element _must_ be a `td` otherwise we get hydration errors */}
<td
className="bg-orange-200 absolute h-full after:content-[''] group-hover:opacity-50"
style={{
width: barChartPercentages[key],
}}
/>
<TableCell className="font-medium relative">
{item[0]}
</TableCell>
<TableCell className="text-right">
<TableCell className="text-right relative">
{item[1]}
</TableCell>
</TableRow>
Expand Down