|
| 1 | +import React, { useRef } from "react"; |
| 2 | +import ReactECharts from "echarts-for-react"; |
| 3 | +import dayjs from "dayjs"; |
| 4 | +import { debounce } from "lodash"; |
| 5 | + |
| 6 | +interface Props { |
| 7 | + data: Array<any>; |
| 8 | + setDateRange: (range: { fromTimestamp: string; toTimestamp: string }) => void; |
| 9 | +} |
| 10 | + |
| 11 | +const UserActivityByTimeChart = ({ data, setDateRange }: Props) => { |
| 12 | + const chartRef = useRef<any>(null); |
| 13 | + |
| 14 | + const debouncedSetDateRange = useRef( |
| 15 | + debounce((fromTimestamp: string, toTimestamp: string) => { |
| 16 | + setDateRange({ fromTimestamp, toTimestamp }); |
| 17 | + }, 500) // Delays fetching only after zooming stops |
| 18 | + ).current; |
| 19 | + |
| 20 | + // Extract min/max dates from the data |
| 21 | + const allDates = data.map((log) => log.eventTime && dayjs(log.eventTime).format("YYYY-MM-DD")); |
| 22 | + const minDate = allDates.length ? dayjs(Math.min(...allDates.map((d) => new Date(d).getTime()))) : dayjs().subtract(7, "days"); |
| 23 | + const maxDate = allDates.length ? dayjs(Math.max(...allDates.map((d) => new Date(d).getTime()))) : dayjs(); |
| 24 | + |
| 25 | + // Generate full date range including missing days |
| 26 | + const fullDateRange: string[] = []; |
| 27 | + let currentDate = minDate; |
| 28 | + while (currentDate.isBefore(maxDate) || currentDate.isSame(maxDate, "day")) { |
| 29 | + fullDateRange.push(currentDate.format("YYYY-MM-DD")); |
| 30 | + currentDate = currentDate.add(1, "day"); |
| 31 | + } |
| 32 | + |
| 33 | + // Group data by date and eventType |
| 34 | + const timeSeriesData = data.reduce((acc: any, log: any) => { |
| 35 | + const eventTime = log.eventTime ? new Date(log.eventTime) : null; |
| 36 | + if (eventTime && !isNaN(eventTime.getTime())) { |
| 37 | + const date = eventTime.toISOString().split("T")[0]; // Extract date part |
| 38 | + if (!acc[date]) acc[date] = 0; |
| 39 | + acc[date] = acc[date] + 1; |
| 40 | + } |
| 41 | + return acc; |
| 42 | + }, {}); |
| 43 | + |
| 44 | + // Prepare series data for each event type |
| 45 | + const series = [{ |
| 46 | + name: "App Views", |
| 47 | + type: "line", |
| 48 | + stack: "total", |
| 49 | + data: fullDateRange.map((date) => timeSeriesData[date] || 0), // Fill gaps with 0 |
| 50 | + itemStyle: { |
| 51 | + color: "#1890ff", |
| 52 | + }, |
| 53 | + }]; |
| 54 | + |
| 55 | + const handleChartEvents = (params: any) => { |
| 56 | + if (params.start !== undefined && params.end !== undefined) { |
| 57 | + const startIndex = Math.floor((params.start / 100) * (fullDateRange.length - 1)); |
| 58 | + const endIndex = Math.floor((params.end / 100) * (fullDateRange.length - 1)); |
| 59 | + |
| 60 | + const fromDate = new Date(fullDateRange[startIndex] || fullDateRange[0]); // Keep start of day |
| 61 | + const toDate = new Date(fullDateRange[endIndex] || fullDateRange[fullDateRange.length - 1]); |
| 62 | + |
| 63 | + toDate.setHours(23, 59, 59, 999); |
| 64 | + |
| 65 | + const fromTimestamp = fromDate.toISOString(); |
| 66 | + const toTimestamp = toDate.toISOString(); |
| 67 | + debouncedSetDateRange(fromTimestamp, toTimestamp); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <ReactECharts |
| 73 | + ref={chartRef} |
| 74 | + option={{ |
| 75 | + title: { text: "App Usage Log", left: "center" }, |
| 76 | + tooltip: { trigger: "axis", axisPointer: { type: "shadow" } }, |
| 77 | + legend: { left: "left", orient: "vertical", top: "12%" }, // Ensure labels are on the left |
| 78 | + grid: { left: "20%", right: "4%", bottom: "3%", containLabel: true }, |
| 79 | + xAxis: { |
| 80 | + type: "category", |
| 81 | + data: fullDateRange, |
| 82 | + axisLabel: { rotate: 45 }, |
| 83 | + }, |
| 84 | + yAxis: { |
| 85 | + type: "value", |
| 86 | + }, |
| 87 | + dataZoom: [ |
| 88 | + { |
| 89 | + type: "slider", |
| 90 | + xAxisIndex: 0, |
| 91 | + filterMode: "weakFilter", |
| 92 | + show: true, |
| 93 | + start: 0, |
| 94 | + end: 100, |
| 95 | + realtime: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + type: "inside", |
| 99 | + xAxisIndex: 0, |
| 100 | + realtime: false, |
| 101 | + }, |
| 102 | + ], |
| 103 | + series, |
| 104 | + }} |
| 105 | + onEvents={{ dataZoom: handleChartEvents }} |
| 106 | + style={{ height: "400px", marginBottom: "20px" }} |
| 107 | + /> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +export default UserActivityByTimeChart; |
0 commit comments