SimulatePie.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { querySimulationProfit } from '@/services/SmartOps';
  2. import { useRequest } from '@umijs/max';
  3. import dayjs from 'dayjs';
  4. import * as echarts from 'echarts';
  5. import { useEffect, useRef } from 'react';
  6. const SimulatePie = (props) => {
  7. const { projectId } = props;
  8. const domRef = useRef(null);
  9. const chartRef = useRef(null);
  10. const { data } = useRequest(querySimulationProfit, {
  11. defaultParams: [
  12. {
  13. project_id: projectId,
  14. s_time: dayjs().subtract(1, 'day').format('YYYY-MM-DD HH:mm:ss'),
  15. e_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  16. },
  17. ],
  18. onSuccess(data) {
  19. console.log(data);
  20. let options = getOption(data);
  21. // chartRef.current.clear();
  22. chartRef.current.setOption(options);
  23. },
  24. });
  25. const getProfit = () => {
  26. if (!data?.info) return '-';
  27. return Object.values(data.info).reduce(
  28. (total, currentValue) => total + currentValue,
  29. 0,
  30. );
  31. };
  32. useEffect(() => {
  33. chartRef.current = echarts.init(domRef.current);
  34. // 在组件卸载时销毁图表实例
  35. return () => {
  36. chartRef.current.dispose();
  37. };
  38. }, []);
  39. return (
  40. <div className="card-box" style={{ padding: 20, marginTop: 20 }}>
  41. <h2 style={{ textAlign: 'center', fontSize: 24 }}>
  42. 通过模拟仿真预计未来一日可省 &nbsp;
  43. <span style={{ fontSize: 28, color: '#F5A623' }}>{getProfit()}元</span>
  44. </h2>
  45. <div ref={domRef} style={{ height: '25vh' }}></div>
  46. </div>
  47. );
  48. };
  49. function getOption(data) {
  50. let seriesData = [];
  51. let type = {
  52. 0: '未分类',
  53. 1: '过滤周期优化',
  54. 2: '絮凝剂投加优化',
  55. 3: '膜寿命预测',
  56. 4: '膜反冲洗建议',
  57. 5: '膜化学清洗建议',
  58. 6: 'RO一段优化建议',
  59. 7: 'RO二段优化建议',
  60. 8: 'RO三段优化建议',
  61. 9: 'RO杀菌周期优化',
  62. 10: 'RO冲洗周期优化',
  63. };
  64. Object.entries(data.info).map(([key, value]) => {
  65. if (value > 0) {
  66. seriesData.push({
  67. value,
  68. name: type[key],
  69. });
  70. }
  71. });
  72. let option = {
  73. color: ['#30EDFD', '#FFC800'],
  74. tooltip: {
  75. trigger: 'item',
  76. },
  77. series: [
  78. {
  79. type: 'pie',
  80. radius: ['50%', '70%'],
  81. label: {
  82. fontSize: 22,
  83. },
  84. emphasis: {
  85. itemStyle: {
  86. shadowBlur: 10,
  87. shadowOffsetX: 0,
  88. shadowColor: 'rgba(0, 0, 0, 0.5)',
  89. },
  90. },
  91. data: seriesData,
  92. },
  93. ],
  94. };
  95. return option;
  96. }
  97. export default SimulatePie;