index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Column } from '@ant-design/charts';
  2. import moment from 'moment';
  3. import { useEffect } from 'react';
  4. import { service } from '@/pages/Analysis';
  5. import type { ColumnConfig } from '@ant-design/plots/es/components/column';
  6. const calculationDate = () => {
  7. const dd = new Date();
  8. dd.setDate(dd.getDate() - 30);
  9. const y = dd.getFullYear();
  10. const m = dd.getMonth() + 1 < 10 ? `0${dd.getMonth() + 1}` : dd.getMonth() + 1;
  11. const d = dd.getDate() < 10 ? `0${dd.getDate()}` : dd.getDate();
  12. return `${y}-${m}-${d} 00:00:00`;
  13. };
  14. const MessageChart = () => {
  15. const list = [
  16. {
  17. dashboard: 'device',
  18. object: 'message',
  19. measurement: 'quantity',
  20. dimension: 'agg',
  21. group: 'sameDay',
  22. params: {
  23. time: '1d',
  24. format: 'yyyy-MM-dd',
  25. },
  26. },
  27. {
  28. dashboard: 'device',
  29. object: 'message',
  30. measurement: 'quantity',
  31. dimension: 'agg',
  32. group: 'sameMonth',
  33. params: {
  34. limit: 30,
  35. time: '1d',
  36. format: 'yyyy-MM-dd',
  37. from: calculationDate(),
  38. to: `${moment(new Date()).format('YYYY-MM-DD')} 23:59:59`,
  39. },
  40. },
  41. {
  42. dashboard: 'device',
  43. object: 'message',
  44. measurement: 'quantity',
  45. dimension: 'agg',
  46. group: 'month',
  47. params: {
  48. time: '1M',
  49. format: 'yyyy-MM-dd',
  50. from: calculationDate(),
  51. to: `${moment(new Date()).format('YYYY-MM-DD')} 23:59:59`,
  52. },
  53. },
  54. ];
  55. useEffect(() => {
  56. service.getMulti(list).subscribe((data) => {
  57. console.log(data);
  58. });
  59. }, []);
  60. const data = [
  61. {
  62. type: '1-3秒',
  63. value: 0.16,
  64. },
  65. {
  66. type: '4-10秒',
  67. value: 0.125,
  68. },
  69. {
  70. type: '11-30秒',
  71. value: 0.24,
  72. },
  73. {
  74. type: '31-60秒',
  75. value: 0.19,
  76. },
  77. {
  78. type: '1-3分',
  79. value: 0.22,
  80. },
  81. {
  82. type: '3-10分',
  83. value: 0.05,
  84. },
  85. {
  86. type: '10-30分',
  87. value: 0.01,
  88. },
  89. {
  90. type: '30+分',
  91. value: 0.015,
  92. },
  93. ];
  94. const paletteSemanticRed = '#F4664A';
  95. const brandColor = '#5B8FF9';
  96. const config: ColumnConfig = {
  97. data,
  98. xField: 'type',
  99. yField: 'value',
  100. seriesField: '',
  101. color: function color(_ref: any) {
  102. const { type } = _ref;
  103. if (type === '10-30分' || type === '30+分') {
  104. return paletteSemanticRed;
  105. }
  106. return brandColor;
  107. },
  108. width: 200,
  109. height: 200,
  110. label: {
  111. offset: 10,
  112. },
  113. legend: false,
  114. xAxis: {
  115. label: {
  116. autoHide: true,
  117. autoRotate: false,
  118. },
  119. },
  120. };
  121. return <Column {...config} />;
  122. };
  123. export default MessageChart;