pages_online_devices_chart.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useState } from 'react';
  2. import {
  3. Button, Form, Select, Card, Typography, DatePicker
  4. } from 'antd';
  5. import {
  6. useQuery,
  7. } from '@tanstack/react-query';
  8. import dayjs from 'dayjs';
  9. import { Column} from "@ant-design/plots";
  10. import 'dayjs/locale/zh-cn';
  11. import { MonitorChartsAPI } from './api/index.ts';
  12. // 在线设备数量图表页面
  13. export const OnlineDevicesChartPage = () => {
  14. const [timeRange, setTimeRange] = useState<[dayjs.Dayjs, dayjs.Dayjs]>([
  15. dayjs().subtract(7, 'day'),
  16. dayjs()
  17. ]);
  18. const [dimension, setDimension] = useState<'hour' | 'day' | 'month'>('day');
  19. const { data: onlineRateData, isLoading, refetch } = useQuery({
  20. queryKey: ['adminZichanOnlineRate', timeRange, dimension],
  21. queryFn: async () => {
  22. const params = {
  23. created_at_gte: timeRange[0].format('YYYY-MM-DD HH:mm:ss'),
  24. created_at_lte: timeRange[1].format('YYYY-MM-DD HH:mm:ss'),
  25. dimension
  26. };
  27. // const res = await axios.get<OnlineRateChartData[]>(`${API_BASE_URL}/big/zichan_online_rate_chart`, { params });
  28. const res = await MonitorChartsAPI.fetchOnlineRateData(params);
  29. return res;
  30. }
  31. });
  32. const { Title } = Typography;
  33. const { RangePicker } = DatePicker;
  34. const handleSearch = () => {
  35. refetch();
  36. };
  37. return (
  38. <div>
  39. <Title level={2}>在线设备数量变化</Title>
  40. <Card>
  41. <Form layout="inline" style={{ marginBottom: '16px' }}>
  42. <Form.Item label="时间范围">
  43. <RangePicker
  44. value={timeRange}
  45. onChange={(dates) => dates && setTimeRange(dates as [dayjs.Dayjs, dayjs.Dayjs])}
  46. />
  47. </Form.Item>
  48. <Form.Item label="时间维度">
  49. <Select
  50. value={dimension}
  51. onChange={setDimension}
  52. options={[
  53. { label: '小时', value: 'hour' },
  54. { label: '天', value: 'day' },
  55. { label: '月', value: 'month' }
  56. ]}
  57. style={{ width: '100px' }}
  58. />
  59. </Form.Item>
  60. <Form.Item>
  61. <Button type="primary" onClick={handleSearch}>查询</Button>
  62. </Form.Item>
  63. </Form>
  64. <div style={{ height: '500px' }}>
  65. {!isLoading && onlineRateData && (
  66. <Column
  67. data={onlineRateData}
  68. xField="time_interval"
  69. yField="total_devices"
  70. color="#36cfc9"
  71. label={{
  72. position: 'top',
  73. style: {
  74. fill: '#000',
  75. },
  76. text: (items: Record<string, any>) => {
  77. let content = items['time_interval'];
  78. content += `\n(${(items['total_devices'])})`;
  79. return content;
  80. },
  81. }}
  82. xAxis={{
  83. label: {
  84. style: {
  85. fill: '#000',
  86. },
  87. },
  88. }}
  89. yAxis={{
  90. label: {
  91. style: {
  92. fill: '#000',
  93. },
  94. },
  95. }}
  96. autoFit={true}
  97. interaction={{
  98. tooltip: false
  99. }}
  100. />
  101. )}
  102. </div>
  103. </Card>
  104. </div>
  105. );
  106. };