index.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {memo} from "react";
  2. import {Table} from "antd";
  3. import { Column ,Pie ,Line} from '@ant-design/plots';
  4. import {Area} from "@ant-design/charts";
  5. export const ChartsGraph = memo(({
  6. type,
  7. viewer
  8. })=>{
  9. const {columns,resultList,dimensionColumns,metricColumns} = viewer;
  10. const queryColumns = (columns||[]).map(item=> (
  11. {
  12. title: item.name,
  13. dataIndex: item.nameEn,
  14. key: item.nameEn,
  15. }
  16. ))
  17. const dimensionObj = dimensionColumns && dimensionColumns[0];
  18. const dimension = dimensionObj?.nameEn
  19. const dimensionType = dimensionObj?.showType;
  20. const metric = dimensionColumns && metricColumns[0]?.nameEn;
  21. const data = (resultList||[]).map(item=> ({
  22. dimension: item[dimension],
  23. metric: item[metric],
  24. category:item[dimensionColumns[1]?.nameEn]
  25. }))
  26. // console.log(data)
  27. const DemoColumn = () => {
  28. const config = {
  29. data,
  30. xField: (d)=>dimensionType==="DATE" ? new Date(d.dimension).toLocaleDateString():d.dimension,
  31. stack:false,
  32. yField: 'metric',
  33. colorField: 'category',
  34. axis: {y: { labelFormatter: '~s' }},
  35. containerStyle:{height:'50vh'},
  36. legend: false,
  37. };
  38. return <Column {...config} />;
  39. };
  40. const DemoArea = () => {
  41. const config = {
  42. data: data,
  43. xField: (d)=>dimensionType==="DATE" ? new Date(d.dimension).toLocaleDateString():d.dimension,
  44. yField: 'metric',
  45. colorField: 'category',
  46. shapeField: 'smooth',
  47. group: true,
  48. stack:false,
  49. axis: {
  50. x: { title: 'Date' },
  51. y: { labelFormatter: '~s' },
  52. },
  53. legend: {
  54. color: { size: 72, autoWrap: true, maxRows: 3, cols: 6 },
  55. },
  56. };
  57. return <Area {...config} />;
  58. };
  59. const DemoPie = () => {
  60. const config = {
  61. data,
  62. angleField: 'metric',
  63. colorField: 'category',
  64. label: {
  65. text: 'category',
  66. style: {
  67. fontWeight: 'bold',
  68. },
  69. },
  70. legend: {
  71. color: {
  72. title: false,
  73. position: 'right',
  74. rowPadding: 5,
  75. },
  76. },
  77. containerStyle:{
  78. height:'50vh'
  79. },
  80. };
  81. return <Pie {...config} />;
  82. };
  83. const DemoLine = () => {
  84. const config = {
  85. data,
  86. xField: 'dimension',
  87. yField: 'metric',
  88. sizeField: 'metric',
  89. shapeField: 'trail',
  90. legend: { size: false },
  91. colorField: 'category',
  92. containerStyle:{
  93. height:'50vh'
  94. },
  95. };
  96. return <Line {...config} />;
  97. };
  98. switch (type) {
  99. case 'bar_chart': return DemoColumn();
  100. case 'pie_chart': return DemoPie();
  101. case 'line_chart': return DemoLine();
  102. case 'area_chart' : return DemoArea();
  103. default:
  104. return (
  105. <Table
  106. columns={queryColumns}
  107. dataSource={resultList}
  108. />
  109. )
  110. }
  111. })