Skip to content

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

Refactor useCoordinatesLayer and UsageChart for improved data handling and visualization

Bill Yang4833744d91cd
diff --git a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts
index f2fe8075a..e956aca62 100644
--- a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts
+++ b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts
@@ -43,7 +43,11 @@ export function useCoordinatesLayer({
const addCoordinatesLayer = () => {
if (!map.current) return;
- const highest = liveSessionLocations.reduce((acc, curr) => Math.max(acc, curr.count), 0) || 1;
+ const topSize =
+ Math.pow(
+ liveSessionLocations.reduce((acc, curr) => Math.max(acc, curr.count), 0),
+ 0.5
+ ) || 1;
// Calculate size scale based on total number of locations
const totalLocations = liveSessionLocations.length;
@@ -107,13 +111,13 @@ export function useCoordinatesLayer({
["exponential", 2],
["zoom"],
0,
- ["interpolate", ["linear"], ["get", "count"], 1, 1 * sizeMultiplier, highest, 3 * sizeMultiplier],
+ ["interpolate", ["linear"], ["get", "count"], 1, 1 * sizeMultiplier, topSize, 5 * sizeMultiplier],
5,
- ["interpolate", ["linear"], ["get", "count"], 1, 3 * sizeMultiplier, highest, 7 * sizeMultiplier],
+ ["interpolate", ["linear"], ["get", "count"], 1, 3 * sizeMultiplier, topSize, 10 * sizeMultiplier],
10,
- ["interpolate", ["linear"], ["get", "count"], 1, 7 * sizeMultiplier, highest, 15 * sizeMultiplier],
+ ["interpolate", ["linear"], ["get", "count"], 1, 7 * sizeMultiplier, topSize, 20 * sizeMultiplier],
15,
- ["interpolate", ["linear"], ["get", "count"], 1, 15 * sizeMultiplier, highest, 30 * sizeMultiplier],
+ ["interpolate", ["linear"], ["get", "count"], 1, 15 * sizeMultiplier, topSize, 40 * sizeMultiplier],
],
// "circle-radius": [
// "interpolate",
@@ -214,8 +218,21 @@ export function useCoordinatesLayer({
});
}
- // Update colors when data changes
+ // Update paint properties when data changes
if (map.current.getLayer("realtime-coordinates-layer")) {
+ map.current.setPaintProperty("realtime-coordinates-layer", "circle-radius", [
+ "interpolate",
+ ["exponential", 2],
+ ["zoom"],
+ 0,
+ ["interpolate", ["linear"], ["get", "count"], 1, 1 * sizeMultiplier, topSize, 5 * sizeMultiplier],
+ 5,
+ ["interpolate", ["linear"], ["get", "count"], 1, 3 * sizeMultiplier, topSize, 10 * sizeMultiplier],
+ 10,
+ ["interpolate", ["linear"], ["get", "count"], 1, 7 * sizeMultiplier, topSize, 20 * sizeMultiplier],
+ 15,
+ ["interpolate", ["linear"], ["get", "count"], 1, 15 * sizeMultiplier, topSize, 40 * sizeMultiplier],
+ ]);
map.current.setPaintProperty("realtime-coordinates-layer", "circle-color", [
"case",
["get", "isFiltered"],
diff --git a/client/src/components/UsageChart.tsx b/client/src/components/UsageChart.tsx
index 0833cb50b..8d453aacf 100644
--- a/client/src/components/UsageChart.tsx
+++ b/client/src/components/UsageChart.tsx
@@ -174,17 +174,19 @@ export function UsageChart({ organizationId, startDate, endDate, timeZone = "UTC
return (
<div className="text-sm bg-neutral-850 p-3 rounded-md min-w-[100px] border border-neutral-750">
<div className="font-medium mb-1">{currentTime.toLocaleString(DateTime.DATE_MED)}</div>
- {slice.points.reverse().map((point: any) => {
- return (
- <div key={point.serieId} className="flex justify-between gap-4 text-sm">
- <div className="flex items-center gap-2 text-neutral-300">
- <div className="w-2 h-2 rounded-full" style={{ backgroundColor: point.seriesColor }} />
- <span>{point.seriesId.charAt(0).toUpperCase() + point.seriesId.slice(1)}</span>
+ {slice.points
+ .sort((a: any, b: any) => a.seriesId.localeCompare(b.seriesId))
+ .map((point: any) => {
+ return (
+ <div key={point.serieId} className="flex justify-between gap-4 text-sm">
+ <div className="flex items-center gap-2 text-neutral-300">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: point.seriesColor }} />
+ <span>{point.seriesId.charAt(0).toUpperCase() + point.seriesId.slice(1)}</span>
+ </div>
+ <div>{Number(point.data.yFormatted).toLocaleString()}</div>
</div>
- <div>{Number(point.data.yFormatted).toLocaleString()}</div>
- </div>
- );
- })}
+ );
+ })}
<div className="text-sm mt-2 flex justify-between">
<div className="text-neutral-300">Total</div>
<div className="font-semibold">{total.toLocaleString()}</div>