LineCard.jsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import moment from 'moment';
  3. import { Button, Card, DatePicker, Space } from 'antd';
  4. import ECharts from '@/components/ECharts'
  5. import echarts from '@/components/ECharts/echarts'
  6. import { assignOption } from '@/components/ECharts/utils'
  7. import request from '@/utils/request'
  8. import Title from './Title'
  9. const lineOption = {
  10. xAxis: {
  11. type: 'category',
  12. },
  13. yAxis: {
  14. type: 'value'
  15. },
  16. series: {
  17. type: 'line',
  18. areaStyle: {
  19. opacity: 0.8,
  20. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  21. offset: 0,
  22. color: 'rgba(55, 162, 255)'
  23. }, {
  24. offset: 1,
  25. color: 'rgba(1, 191, 236)'
  26. }])
  27. },
  28. smooth: true,
  29. lineStyle: {
  30. width: 0
  31. }
  32. }
  33. }
  34. const Condition = (props) => {
  35. const [dtRange, setDtRange] = useState()
  36. const handleWeek = () => {
  37. setDtRange([
  38. moment().startOf('week'),
  39. moment(),
  40. ])
  41. }
  42. const handleMonth = () => {
  43. setDtRange([
  44. moment().startOf('month'),
  45. moment(),
  46. ])
  47. }
  48. const handleChange = (vals) => {
  49. setDtRange(vals)
  50. }
  51. useEffect(() => {
  52. props.onChange(dtRange)
  53. // eslint-disable-next-line react-hooks/exhaustive-deps
  54. }, [dtRange])
  55. useEffect(() => {
  56. // 默认本周
  57. handleWeek()
  58. // eslint-disable-next-line react-hooks/exhaustive-deps
  59. }, [])
  60. return (
  61. <Space>
  62. <Button onClick={handleMonth}>本月</Button>
  63. <Button onClick={handleWeek}>本周</Button>
  64. <DatePicker.RangePicker value={dtRange} onChange={handleChange}/>
  65. </Space>
  66. )
  67. }
  68. export default (props) => {
  69. const chartRef = useRef()
  70. const [loading, setLoading] = useState(false)
  71. const [data, setData] = useState([])
  72. const [option, setOption] = useState(lineOption)
  73. const [dtRange, setDtRange] = useState()
  74. const [params, setParams] = useState()
  75. useEffect(() => {
  76. if (dtRange) {
  77. setParams({
  78. startDate: dtRange[0].format('YYYY-MM-DD'),
  79. endDate: dtRange[1].format('YYYY-MM-DD')
  80. })
  81. }
  82. }, [dtRange])
  83. useEffect(() => {
  84. if (params) {
  85. setLoading(true)
  86. request(props.api, { params }).then(res => {
  87. setData(res)
  88. setLoading(false)
  89. }).catch(() => {
  90. setLoading(false)
  91. })
  92. }
  93. }, [params, props.api])
  94. useEffect(() => {
  95. const opt = assignOption(lineOption, {
  96. xAxis: {
  97. data: data.map((item) => item.dt)
  98. },
  99. series: {
  100. data: data.map((item) => item.val)
  101. }
  102. })
  103. setOption(opt)
  104. }, [data])
  105. return (
  106. <Card
  107. title={<Title text={props.title} icon={props.icon} />}
  108. extra={<Condition value={dtRange} onChange={(vals) => setDtRange(vals)} />}
  109. style={props.style}
  110. >
  111. <div style={{ margin: '0 auto', width: props.width || '100%'}}>
  112. <ECharts loading={loading} ref={chartRef} option={option} width={props.width || '100%'} />
  113. </div>
  114. </Card>
  115. )
  116. }