bar-graph.tsx 1.4 KB

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