chart-view.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { AutoChart, BackEndChartType, getChartType } from '@/components/chart/autoChart';
  2. import { formatSql } from '@/utils';
  3. import { Datum } from '@antv/ava';
  4. import { Table, Tabs, TabsProps } from 'antd';
  5. import { CodePreview } from './code-preview';
  6. function ChartView({ data, type, sql }: { data: Datum[]; type: BackEndChartType; sql: string }) {
  7. const columns = data?.[0]
  8. ? Object.keys(data?.[0])?.map(item => {
  9. return {
  10. title: item,
  11. dataIndex: item,
  12. key: item,
  13. };
  14. })
  15. : [];
  16. const ChartItem = {
  17. key: 'chart',
  18. label: 'Chart',
  19. children: <AutoChart data={data} chartType={getChartType(type)} />,
  20. };
  21. const SqlItem = {
  22. key: 'sql',
  23. label: 'SQL',
  24. children: <CodePreview language='sql' code={formatSql(sql ?? '', 'mysql') as string} />,
  25. };
  26. const DataItem = {
  27. key: 'data',
  28. label: 'Data',
  29. children: <Table dataSource={data} columns={columns} scroll={{ x: 'auto' }} />,
  30. };
  31. const TabItems: TabsProps['items'] = type === 'response_table' ? [DataItem, SqlItem] : [ChartItem, SqlItem, DataItem];
  32. return <Tabs defaultActiveKey={type === 'response_table' ? 'data' : 'chart'} items={TabItems} size='small' />;
  33. }
  34. export default ChartView;