Age.jsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import React from 'react';
  2. import * as echarts from 'echarts/core';
  3. import Chart from '@/components/chart';
  4. import { getUserAge } from '@/services/statis';
  5. let dataIndex = 0;
  6. export default (props) => {
  7. const chartRef = React.useRef();
  8. const [option, setOption] = React.useState({});
  9. React.useEffect(() => {
  10. getUserAge().then(res => {
  11. // [{ age: 'xxx', cnt: 12 }] => { 'xxx': 12 }
  12. const dataSource = (res || []).reduce((acc, x) => ({ ...acc, [x.age]: x.cnt }), {});
  13. setOption(getOption(dataSource));
  14. const t = setTimeout(() => {
  15. const chart = chartRef.current;
  16. chart.on('mousemove', function(e) {
  17. chart.dispatchAction({ type: 'downplay', dataIndex });
  18. chart.dispatchAction({ type: 'highlight', dataIndex: e.dataIndex });
  19. dataIndex = e.dataIndex
  20. });
  21. chart.dispatchAction({ type: 'highlight', dataIndex });
  22. clearTimeout(t);
  23. }, 300);
  24. });
  25. }, []);
  26. const onInited = (chart) => chartRef.current = chart;
  27. return (
  28. <Chart option={option} style={{ height: '270px' }} onInited={onInited} />
  29. )
  30. }
  31. function getOption(seriesData) {
  32. const data = Object.keys(seriesData).map(function(key) {
  33. return {
  34. value: seriesData[key],
  35. name: key
  36. }
  37. });
  38. return {
  39. title: {
  40. show: true,
  41. top: '6%',
  42. left: 'center',
  43. text: '人员年龄统计',
  44. textStyle: {
  45. color: '#fff',
  46. fontSize: 18,
  47. fontWeight: 'normal'
  48. }
  49. },
  50. color: ['#3DE0A4', '#FF8940', '#3FC9F4', '#CB55E9'],
  51. series: [
  52. {
  53. type: 'pie',
  54. top: '10%',
  55. radius: ['60%', '70%'],
  56. label: {
  57. show: false,
  58. position: 'center',
  59. lineHeight: 28,
  60. borderRadius: [100, 100, 100, 100],
  61. borderColor: '#FD8F00',
  62. borderWidth: 2,
  63. padding: 16,
  64. width: 100,
  65. height: 100,
  66. formatter: [
  67. '{s1|{d}%}',
  68. '{s2|{b}}',
  69. '{s3|{c}}',
  70. ].join('\n'),
  71. rich: {
  72. s1: {
  73. color: '#FD8F00',
  74. fontSize: 24,
  75. },
  76. s2: {
  77. color: 'rgba(255, 255, 255, .7)',
  78. fontSize: 16,
  79. },
  80. s3: {
  81. color: '#fff',
  82. fontSize: 18,
  83. },
  84. }
  85. },
  86. emphasis: {
  87. scaleSize: 10,
  88. label: {
  89. show: true,
  90. }
  91. },
  92. labelLine: {
  93. show: false
  94. },
  95. data: data,
  96. },
  97. ]
  98. };
  99. }