index.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { ChartData } from '@/types/chat';
  2. import { Card, CardContent, Typography } from '@mui/joy';
  3. import { useMemo } from 'react';
  4. import BarChart from './bar-chart';
  5. import LineChart from './line-chart';
  6. import TableChart from './table-chart';
  7. type Props = {
  8. chartsData: Array<ChartData>;
  9. };
  10. function Chart({ chartsData }: Props) {
  11. const chartRows = useMemo(() => {
  12. if (chartsData) {
  13. const res = [];
  14. // 若是有类型为 IndicatorValue 的,提出去,独占一行
  15. const chartCalc = chartsData?.filter(item => item.chart_type === 'IndicatorValue');
  16. if (chartCalc.length > 0) {
  17. res.push({
  18. charts: chartCalc,
  19. type: 'IndicatorValue',
  20. });
  21. }
  22. const otherCharts = chartsData?.filter(item => item.chart_type !== 'IndicatorValue');
  23. const otherLength = otherCharts.length;
  24. let curIndex = 0;
  25. // charts 数量 3~8个,暂定每行排序
  26. const chartLengthMap = [[0], [1], [2], [1, 2], [1, 3], [2, 1, 2], [2, 1, 3], [3, 1, 3], [3, 2, 3]];
  27. chartLengthMap[otherLength].forEach(item => {
  28. if (item > 0) {
  29. const rowsItem = otherCharts.slice(curIndex, curIndex + item);
  30. curIndex = curIndex + item;
  31. res.push({
  32. charts: rowsItem,
  33. });
  34. }
  35. });
  36. return res;
  37. }
  38. return undefined;
  39. }, [chartsData]);
  40. return (
  41. <div className='flex flex-col gap-3'>
  42. {chartRows?.map((chartRow, index) => (
  43. <div key={`chart_row_${index}`} className={`${chartRow?.type !== 'IndicatorValue' ? 'flex gap-3' : ''}`}>
  44. {chartRow.charts.map(chart => {
  45. if (chart.chart_type === 'IndicatorValue' || chart.type === 'IndicatorValue') {
  46. return (
  47. <div key={chart.chart_uid} className='flex flex-row gap-3'>
  48. {chart.values.map(item => (
  49. <div key={item.name} className='flex-1'>
  50. <Card sx={{ background: 'transparent' }}>
  51. <CardContent className='justify-around'>
  52. <Typography gutterBottom component='div'>
  53. {item.name}
  54. </Typography>
  55. <Typography>{item.value}</Typography>
  56. </CardContent>
  57. </Card>
  58. </div>
  59. ))}
  60. </div>
  61. );
  62. } else if (chart.chart_type === 'LineChart' || chart.type === 'LineChart') {
  63. return <LineChart key={chart.chart_uid} chart={chart} />;
  64. } else if (chart.chart_type === 'BarChart' || chart.type === 'BarChart') {
  65. return <BarChart key={chart.chart_uid} chart={chart} />;
  66. } else if (chart.chart_type === 'Table' || chart.type === 'TableChartData') {
  67. return <TableChart key={chart.chart_uid} chart={chart} />;
  68. }
  69. })}
  70. </div>
  71. ))}
  72. </div>
  73. );
  74. }
  75. export * from './autoChart';
  76. export default Chart;