LineChat.jsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { useEffect, useRef, useState, useMemo } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import * as echarts from '@/native/ec-canvas/echarts';
  4. import { getChannelLineChat } from '@/services/agent'
  5. function initChart(canvas, width, height, dpr) {
  6. const chart = echarts.init(canvas, null, {
  7. width: width,
  8. height: height,
  9. devicePixelRatio: dpr // new
  10. });
  11. canvas.setChart(chart);
  12. // var option = {
  13. // xAxis: {
  14. // type: 'category',
  15. // boundaryGap: false,
  16. // data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  17. // // show: false
  18. // },
  19. // yAxis: {
  20. // x: 'center',
  21. // type: 'value',
  22. // splitLine: {
  23. // lineStyle: {
  24. // type: 'dashed'
  25. // }
  26. // }
  27. // // show: false
  28. // },
  29. // series: [{
  30. // name: 'A',
  31. // type: 'line',
  32. // smooth: true,
  33. // data: [18, 36, 65, 30, 78, 40, 33]
  34. // }, {
  35. // name: 'B',
  36. // type: 'line',
  37. // smooth: true,
  38. // data: [12, 50, 51, 35, 70, 30, 20]
  39. // }, {
  40. // name: 'C',
  41. // type: 'line',
  42. // smooth: true,
  43. // data: [10, 30, 31, 50, 40, 20, 10]
  44. // }]
  45. // };
  46. // chart.setOption(option);
  47. return chart;
  48. }
  49. export default (props) => {
  50. const { buildingId, xType, dateRange } = props
  51. const [options, setOptions] = useState()
  52. const chartRef = useRef()
  53. const ec = useMemo(() => {
  54. const onInit = (...args) => {
  55. const chart = initChart(...args)
  56. chartRef.current = chart
  57. return chart
  58. }
  59. return { onInit }
  60. }, [])
  61. useEffect(() => {
  62. if (buildingId) {
  63. const [startDate, endDate] = dateRange
  64. getChannelLineChat({
  65. buildingId,
  66. type: xType,
  67. startDate: `${startDate}T00:00:00.000Z`,
  68. endDate: `${endDate}T23:59:59.999Z`,
  69. }).then((res) => {
  70. const { newCustomerDetail } = res
  71. const data = [[],[]];
  72. (newCustomerDetail || []).forEach((it) => {
  73. data[0].push(it.coordinate.substring(5))
  74. data[1].push(it.customerNum)
  75. })
  76. setOptions({
  77. xAxis: {
  78. type: 'category',
  79. data: data[0],
  80. },
  81. yAxis: {
  82. type: 'value',
  83. },
  84. series: [{
  85. type: 'line',
  86. smooth: true,
  87. data: data[1],
  88. }],
  89. color: ['#5470c6'],
  90. })
  91. })
  92. }
  93. }, [buildingId, dateRange, xType])
  94. useEffect(() => {
  95. if (options) {
  96. chartRef.current.setOption(options);
  97. }
  98. }, [options])
  99. return (
  100. <ec-canvas ec={ec} />
  101. )
  102. }