vis-dashboard.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { AutoChart, BackEndChartType, getChartType } from '@/components/chart';
  2. import { Datum } from '@antv/ava';
  3. import { useMemo } from 'react';
  4. interface Props {
  5. data: {
  6. data: {
  7. data: Datum[];
  8. describe: string;
  9. title: string;
  10. type: BackEndChartType;
  11. sql: string;
  12. }[];
  13. title: string | null;
  14. display_strategy: string;
  15. chart_count: number;
  16. };
  17. }
  18. const chartLayout = [[2], [1, 2], [1, 3], [2, 1, 2], [2, 1, 3], [3, 1, 3], [3, 2, 3]];
  19. function VisDashboard({ data }: Props) {
  20. const charts = useMemo(() => {
  21. if (data.chart_count > 1) {
  22. const layout = chartLayout[data.chart_count - 2];
  23. let prevIndex = 0;
  24. return layout.map(item => {
  25. const items = data.data.slice(prevIndex, prevIndex + item);
  26. prevIndex = item;
  27. return items;
  28. });
  29. }
  30. return [data.data];
  31. }, [data.data, data.chart_count]);
  32. return (
  33. <div className='flex flex-col gap-3'>
  34. {charts.map((row, index) => (
  35. <div key={`row-${index}`} className='flex gap-3'>
  36. {row.map((chart, subIndex) => (
  37. <div
  38. key={`chart-${subIndex}`}
  39. className='flex flex-1 flex-col justify-between p-4 rounded border border-gray-200 dark:border-gray-500 whitespace-normal'
  40. >
  41. <div>
  42. {chart.title && <div className='mb-2 text-lg'>{chart.title}</div>}
  43. {chart.describe && <div className='mb-4 text-sm text-gray-500'>{chart.describe}</div>}
  44. </div>
  45. <AutoChart data={chart.data} chartType={getChartType(chart.type)} />
  46. </div>
  47. ))}
  48. </div>
  49. ))}
  50. </div>
  51. );
  52. }
  53. export default VisDashboard;