Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

polish charts

Bill Yang95c95b86bbf3
diff --git a/client/src/app/components/MainSection/Chart.tsx b/client/src/app/components/MainSection/Chart.tsx
index b6815e7bd..a36caffed 100644
--- a/client/src/app/components/MainSection/Chart.tsx
+++ b/client/src/app/components/MainSection/Chart.tsx
@@ -29,6 +29,27 @@ const getMax = (time: Time) => {
return undefined;
};
+const getMin = (time: Time) => {
+ if (time.mode === "day") {
+ const dayDate = DateTime.fromISO(time.day).startOf("day");
+ return dayDate.toJSDate();
+ } else if (time.mode === "week") {
+ const weekDate = DateTime.fromISO(time.week).startOf("week");
+ return weekDate.toJSDate();
+ } else if (time.mode === "month") {
+ const monthDate = DateTime.fromISO(time.month).startOf("month");
+ return monthDate.toJSDate();
+ } else if (time.mode === "year") {
+ const yearDate = DateTime.fromISO(time.year).startOf("year");
+ return yearDate.toJSDate();
+ } else if (time.mode === "range") {
+ const startDate = DateTime.fromISO(time.startDate).startOf("day");
+ const endDate = DateTime.fromISO(time.endDate).startOf("day");
+ return startDate.toJSDate();
+ }
+ return undefined;
+};
+
export function Chart({
data,
previousData,
@@ -36,17 +57,26 @@ export function Chart({
}: {
data: APIResponse<GetPageViewsResponse> | undefined;
previousData: APIResponse<GetPageViewsResponse> | undefined;
- max?: number;
+ max: number;
}) {
const { time } = useTimeSelection();
+ const lengthDiff =
+ (data?.data?.length ?? 0) - (previousData?.data?.length ?? 0);
+
const formattedData = data?.data?.map((e, i) => ({
x: DateTime.fromSQL(e.time).toUTC().toFormat("yyyy-MM-dd HH:mm:ss"),
y: e.pageviews,
- previousY: previousData?.data?.[i]?.pageviews,
+ previousY:
+ i >= lengthDiff && previousData?.data?.[i - lengthDiff]?.pageviews,
currentTime: DateTime.fromSQL(e.time).toUTC(),
- previousTime: DateTime.fromSQL(previousData?.data?.[i]?.time ?? "").toUTC(),
+ previousTime:
+ i >= lengthDiff
+ ? DateTime.fromSQL(
+ previousData?.data?.[i - lengthDiff]?.time ?? ""
+ ).toUTC()
+ : undefined,
}));
const timeRange =
@@ -73,6 +103,7 @@ export function Chart({
precision: "second",
useUTC: true,
max: getMax(time),
+ min: getMin(time),
}}
yScale={{
type: "linear",
@@ -135,7 +166,7 @@ export function Chart({
fill={[{ match: (d) => d.id === "1", id: "gradient" }]}
sliceTooltip={({ slice }: any) => {
const currentY = Number(slice.points[0].data.yFormatted);
- const previousY = Number(slice.points[0].data.previousY);
+ const previousY = Number(slice.points[0].data.previousY) || 0;
const currentTime = slice.points[0].data.currentTime as DateTime;
const previousTime = slice.points[0].data.previousTime as DateTime;
@@ -164,14 +195,16 @@ export function Chart({
</div>
<div>{round(currentY).toLocaleString()}</div>
</div>
- <div className="flex justify-between text-sm text-muted-foreground">
- <div>
- {time.mode === "day"
- ? previousTime.toFormat("M/d h a")
- : previousTime.toLocaleString()}
+ {previousTime && (
+ <div className="flex justify-between text-sm text-muted-foreground">
+ <div>
+ {time.mode === "day"
+ ? previousTime.toFormat("M/d h a")
+ : previousTime.toLocaleString()}
+ </div>
+ <div>{round(previousY).toLocaleString()}</div>
</div>
- <div>{round(previousY).toLocaleString()}</div>
- </div>
+ )}
</div>
);
}}
diff --git a/client/src/app/components/MainSection/MainSection.tsx b/client/src/app/components/MainSection/MainSection.tsx
index e10374750..a6f50488f 100644
--- a/client/src/app/components/MainSection/MainSection.tsx
+++ b/client/src/app/components/MainSection/MainSection.tsx
@@ -58,7 +58,7 @@ export function MainSection() {
</div>
<div className="h-[350px] relative">
<div className="absolute top-0 left-0 w-full h-full">
- <PreviousChart data={previousData} />
+ <PreviousChart data={previousData} max={maxOfDataAndPreviousData} />
</div>
<div className="absolute top-0 left-0 w-full h-full">
<Chart
diff --git a/client/src/app/components/MainSection/PreviousChart.tsx b/client/src/app/components/MainSection/PreviousChart.tsx
index 7275e7113..beb1dddeb 100644
--- a/client/src/app/components/MainSection/PreviousChart.tsx
+++ b/client/src/app/components/MainSection/PreviousChart.tsx
@@ -3,29 +3,45 @@ import { ResponsiveLine } from "@nivo/line";
import { DateTime } from "luxon";
import { APIResponse, GetPageViewsResponse } from "../../../hooks/api";
import { nivoTheme } from "../../../lib/nivo";
-import { useTimeSelection } from "../../../lib/timeSelectionStore";
+import { Time, useTimeSelection } from "../../../lib/timeSelectionStore";
+import { useMemo } from "react";
+
+const getMin = (time: Time) => {
+ if (time.mode === "day") {
+ const dayDate = DateTime.fromISO(time.day).startOf("day");
+ return dayDate.toJSDate();
+ } else if (time.mode === "week") {
+ const weekDate = DateTime.fromISO(time.week).startOf("week");
+ return weekDate.toJSDate();
+ } else if (time.mode === "month") {
+ const monthDate = DateTime.fromISO(time.month).startOf("month");
+ return monthDate.toJSDate();
+ } else if (time.mode === "year") {
+ const yearDate = DateTime.fromISO(time.year).startOf("year");
+ return yearDate.toJSDate();
+ } else if (time.mode === "range") {
+ const startDate = DateTime.fromISO(time.startDate).startOf("day");
+ const endDate = DateTime.fromISO(time.endDate).startOf("day");
+ return startDate.toJSDate();
+ }
+ return undefined;
+};
export function PreviousChart({
data,
max,
}: {
data: APIResponse<GetPageViewsResponse> | undefined;
- max?: number;
+ max: number;
}) {
- const { time } = useTimeSelection();
+ const { previousTime: time } = useTimeSelection();
const formattedData = data?.data?.map((e) => ({
x: DateTime.fromSQL(e.time).toUTC().toFormat("yyyy-MM-dd HH:mm:ss"),
y: e.pageviews,
}));
- const timeRange =
- time.mode === "range"
- ? DateTime.fromISO(time.startDate).diff(
- DateTime.fromISO(time.endDate),
- "days"
- ).days
- : 0;
+ const min = useMemo(() => getMin(time), [data]);
return (
<ResponsiveLine
@@ -42,13 +58,14 @@ export function PreviousChart({
format: "%Y-%m-%d %H:%M:%S",
precision: "second",
useUTC: true,
+ min,
}}
yScale={{
type: "linear",
min: 0,
stacked: false,
reverse: false,
- max: max,
+ max,
}}
enableGridX={false}
enableGridY={false}
@@ -86,23 +103,6 @@ export function PreviousChart({
motionConfig="stiff"
enableSlices={"x"}
colors={["hsl(var(--neutral-700))"]}
- // sliceTooltip={({ slice }) => {
- // // if (isPrevious) return null;
- // return (
- // <div className="text-sm bg-neutral-900 p-2 rounded-md">
- // <div>
- // {DateTime.fromJSDate(
- // slice.points[0].data.x as any
- // ).toLocaleString(DateTime.DATETIME_SHORT)}
- // </div>
- // <div>
- // {round(Number(slice.points[0].data.yFormatted)).toLocaleString()}
- // </div>
- // </div>
- // );
- // }}
-
- // layers={["grid", "markers", "areas", "", "lines", "points", "slices", "axes", "legends"]}
/>
);
}
diff --git a/client/src/lib/timeSelectionStore.ts b/client/src/lib/timeSelectionStore.ts
index 2ff00344e..e8ec91e99 100644
--- a/client/src/lib/timeSelectionStore.ts
+++ b/client/src/lib/timeSelectionStore.ts
@@ -59,10 +59,11 @@ export const useTimeSelection = create<Store>((set) => ({
day: DateTime.fromISO(time.day).minus({ days: 1 }).toISODate() ?? "",
};
} else if (time.mode === "range") {
- const timeRangeLength = DateTime.fromISO(time.endDate).diff(
- DateTime.fromISO(time.startDate),
- "days"
- ).days;
+ const timeRangeLength =
+ DateTime.fromISO(time.endDate).diff(
+ DateTime.fromISO(time.startDate),
+ "days"
+ ).days + 1;
if (timeRangeLength > 180) {
bucketToUse = "month";