bar-graph.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 items-end gap-1 h-24">
  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
  26. className="w-full bg-foreground/80 rounded-t transition-all"
  27. style={{ height: `${(d.value / maxValue) * 100}%`, minHeight: 2 }}
  28. />
  29. <div className="text-[8px] text-muted-foreground truncate w-full text-center">
  30. {d.label}
  31. </div>
  32. </div>
  33. ))}
  34. </div>
  35. </div>
  36. );
  37. }