bar-graph.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use client";
  2. import type { ComponentRenderProps } from "./types";
  3. import { baseClass, getCustomClass } from "./utils";
  4. interface DataPoint {
  5. label: string;
  6. value: number;
  7. }
  8. export function BarGraph({ element }: ComponentRenderProps) {
  9. const { props } = element;
  10. const customClass = getCustomClass(props);
  11. const data = (props.data as DataPoint[]) || [];
  12. const title = props.title as string | undefined;
  13. const maxValue = Math.max(...data.map((d) => d.value), 1);
  14. return (
  15. <div className={`${baseClass} ${customClass}`}>
  16. {title ? (
  17. <div className="text-xs font-medium mb-2 text-left">{title}</div>
  18. ) : null}
  19. <div className="flex gap-1">
  20. {data.map((d, i) => (
  21. <div key={i} className="flex-1 flex flex-col items-center gap-1">
  22. <div className="text-[8px] text-muted-foreground">
  23. {d.value}
  24. </div>
  25. <div className="w-full h-20 flex items-end">
  26. <div
  27. className="w-full bg-foreground/80 rounded-t transition-all"
  28. style={{ height: `${(d.value / maxValue) * 100}%`, minHeight: 2 }}
  29. />
  30. </div>
  31. <div className="text-[8px] text-muted-foreground truncate w-full text-center">
  32. {d.label}
  33. </div>
  34. </div>
  35. ))}
  36. </div>
  37. </div>
  38. );
  39. }