|
| 1 | +import React, { |
| 2 | + useCallback, useRef, |
| 3 | +} from 'react'; |
| 4 | +import echarts from '../../echarts'; |
| 5 | + |
| 6 | +function percent(n, toFixed) { |
| 7 | + const fixed = toFixed || 0; |
| 8 | + if (!n) { |
| 9 | + return 0; |
| 10 | + } |
| 11 | + const p = n * 100; |
| 12 | + return p.toFixed(fixed) - 0; |
| 13 | +} |
| 14 | + |
| 15 | +function initChart(inst, opt = {}) { |
| 16 | + if (!inst) { |
| 17 | + return; |
| 18 | + } |
| 19 | + const barLimit = 10; |
| 20 | + const list = [ |
| 21 | + {"id":"0b2f954dd73e11e882c6246e96766f00","projectId":null,"projectName":null,"groupName":"刘骞的班组","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":12,"absent":0,"lates":0,"exception":0,"regular":11,"lateEarlyCounts":1,"present":16}, |
| 22 | + {"id":"afa94465633311e9ba1c6c92bf628924","projectId":null,"projectName":null,"groupName":"未激活测试","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":0,"absent":0,"lates":0,"exception":0,"regular":0,"lateEarlyCounts":0,"present":0}, |
| 23 | + {"id":"5bfb1a6875ae11e9ba1c6c92bf628924","projectId":null,"projectName":null,"groupName":"老五六","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":0,"absent":0,"lates":0,"exception":0,"regular":0,"lateEarlyCounts":0,"present":4}, |
| 24 | + {"id":"7ea9baaf8d8511e984726c92bf621f6e","projectId":null,"projectName":null,"groupName":"才哥班组","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":0,"absent":0,"lates":0,"exception":0,"regular":0,"lateEarlyCounts":0,"present":0}, |
| 25 | + {"id":"4398cae28d8411e984726c92bf621f6e","projectId":null,"projectName":null,"groupName":"楚莹班组","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":0,"absent":0,"lates":0,"exception":0,"regular":0,"lateEarlyCounts":0,"present":1}, |
| 26 | + {"id":"be62cc2086a611e984726c92bf621f6e","projectId":null,"projectName":null,"groupName":"测试","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":0,"absent":0,"lates":0,"exception":0,"regular":0,"lateEarlyCounts":0,"present":1}, |
| 27 | + {"id":"7c39c027adb911e984726c92bf621f6e","projectId":null,"projectName":null,"groupName":"测试1","groupId":null,"workerId":null,"workerName":null,"workerImage":null,"workers":0,"attendances":0,"absent":0,"lates":0,"exception":0,"regular":0,"lateEarlyCounts":0,"present":0} |
| 28 | + ] |
| 29 | + const option = { |
| 30 | + legend: { |
| 31 | + data: [ |
| 32 | + { |
| 33 | + name: '正常', |
| 34 | + icon: 'roundRect', |
| 35 | + }, |
| 36 | + { |
| 37 | + name: '迟到早退', |
| 38 | + icon: 'roundRect', |
| 39 | + }, |
| 40 | + ], |
| 41 | + itemWidth: 16, |
| 42 | + itemHeight: 8, |
| 43 | + itemGap: 81, |
| 44 | + textStyle: { |
| 45 | + color: 'rgba(255, 255, 255, 0.6)', |
| 46 | + }, |
| 47 | + bottom: 0, |
| 48 | + }, |
| 49 | + textStyle: { |
| 50 | + color: '#fff', |
| 51 | + }, |
| 52 | + grid: { |
| 53 | + left: '3%', |
| 54 | + right: 100, |
| 55 | + bottom: '13%', |
| 56 | + top: 0, |
| 57 | + containLabel: true, |
| 58 | + }, |
| 59 | + dataZoom: [ |
| 60 | + { |
| 61 | + type: 'slider', |
| 62 | + show: false, |
| 63 | + yAxisIndex: [0], |
| 64 | + filterMode: 'filter', |
| 65 | + zoomLock: true, |
| 66 | + startValue: 0, |
| 67 | + endValue: barLimit - 1, |
| 68 | + right: 0, |
| 69 | + textStyle: { |
| 70 | + color: '#fff', |
| 71 | + }, |
| 72 | + handleIcon: 'none', |
| 73 | + labelFormatter: '', |
| 74 | + width: 10, |
| 75 | + fillerColor: '#354f77', |
| 76 | + borderColor: 'rgba(0, 0, 0, 0)', |
| 77 | + }, |
| 78 | + ], |
| 79 | + xAxis: { |
| 80 | + type: 'value', |
| 81 | + show: false, |
| 82 | + max: 100, |
| 83 | + splitLine: { |
| 84 | + show: false, |
| 85 | + }, |
| 86 | + axisLine: { |
| 87 | + show: false, |
| 88 | + }, |
| 89 | + axisLabel: { |
| 90 | + show: false, |
| 91 | + }, |
| 92 | + }, |
| 93 | + yAxis: { |
| 94 | + type: 'category', |
| 95 | + data: list.map(p => (p.groupName.length > 7 ? `${p.groupName.substr(0, 7)}…` : p.groupName)), |
| 96 | + axisLine: { |
| 97 | + show: false, |
| 98 | + }, |
| 99 | + axisTick: { |
| 100 | + show: false, |
| 101 | + }, |
| 102 | + inverse: true, |
| 103 | + max: barLimit - 1, |
| 104 | + }, |
| 105 | + barWidth: 7, |
| 106 | + series: [ |
| 107 | + { |
| 108 | + type: 'bar', |
| 109 | + data: list.map(() => 100), |
| 110 | + color: 'rgba(255, 255, 255, .2)', |
| 111 | + barGap: '-100%', |
| 112 | + label: { |
| 113 | + show: true, |
| 114 | + position: 'right', |
| 115 | + formatter(params) { |
| 116 | + const { |
| 117 | + attendances, present, |
| 118 | + } = list[params.dataIndex]; |
| 119 | + return `${(attendances > present) ? 100 : percent(attendances / (present || 1))}% ${attendances}人`; |
| 120 | + }, |
| 121 | + align: 'right', |
| 122 | + distance: 100, |
| 123 | + padding: [0, 20, 0, 0], |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: '正常', |
| 128 | + type: 'bar', |
| 129 | + stack: true, |
| 130 | + data: list.map((p) => { |
| 131 | + const { regular, attendances, present } = p; |
| 132 | + return (attendances > present) |
| 133 | + ? (regular / attendances * 100) |
| 134 | + : (regular / (present || 1) * 100); |
| 135 | + }), |
| 136 | + color: '#007df9', |
| 137 | + }, |
| 138 | + { |
| 139 | + name: '迟到早退', |
| 140 | + type: 'bar', |
| 141 | + stack: true, |
| 142 | + data: list.map((p) => { |
| 143 | + const { lateEarlyCounts, attendances, present } = p; |
| 144 | + return (attendances > present) |
| 145 | + ? (lateEarlyCounts / attendances * 100) |
| 146 | + : (lateEarlyCounts / (present || 1) * 100); |
| 147 | + }), |
| 148 | + color: '#fdd966', |
| 149 | + }, |
| 150 | + ], |
| 151 | + } |
| 152 | + inst.setOption(option, opt); |
| 153 | +} |
| 154 | + |
| 155 | +function DataChart() { |
| 156 | + const myChart = useRef(null); |
| 157 | + const chartDOMRef = useCallback((el) => { |
| 158 | + if (el) { |
| 159 | + if (!myChart.current) { |
| 160 | + myChart.current = echarts.init(el); |
| 161 | + } |
| 162 | + initChart(myChart.current, { |
| 163 | + notMerge: true, |
| 164 | + }); |
| 165 | + } else { |
| 166 | + if (!myChart.current) { |
| 167 | + return; |
| 168 | + } |
| 169 | + myChart.current.dispose(); |
| 170 | + } |
| 171 | + }, []); |
| 172 | + |
| 173 | + return ( |
| 174 | + <> |
| 175 | + <div style={{ marginLeft: 50 }}>series数组里的对象可以有formatter</div> |
| 176 | + <div ref={chartDOMRef} style={{ height: 300, backgroundColor: 'lightpink' }} /> |
| 177 | + </> |
| 178 | + ); |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +export default DataChart; |
0 commit comments