Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Usage chart continued

Tim Wilsone9e5332f0799
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
index 78ab671ad3..395ce504d9 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/page-client.tsx
@@ -119,7 +119,7 @@ export default function WorkspaceBillingClient() {
</div>
)}
</div>
- <div className="grid divide-y divide-gray-200 border-y border-gray-200">
+ <div className="grid grid-cols-[minmax(0,1fr)] divide-y divide-gray-200 border-y border-gray-200">
<div>
<div
className={cn(
@@ -295,11 +295,12 @@ function UsageTabCard({
>
<div
className={cn(
- "size-full rounded-full bg-gradient-to-r from-transparent to-violet-700",
+ "size-full rounded-full [mask-image:linear-gradient(90deg,transparent,black_80%)]",
warning && "to-rose-500",
)}
style={{
transform: `translateX(-${100 - Math.floor((usage / Math.max(0, usage, limit)) * 100)}%)`,
+ backgroundImage: `linear-gradient(90deg, #D8277A, #7E3AEA)`,
}}
/>
</div>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
index 3c1b0fbb07..103512a09e 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/billing/usage-chart.tsx
@@ -2,8 +2,11 @@ import useUsage from "@/lib/swr/use-usage";
import { Bars } from "@/ui/charts/bars";
import TimeSeriesChart from "@/ui/charts/time-series-chart";
import XAxis from "@/ui/charts/x-axis";
+import YAxis from "@/ui/charts/y-axis";
+import { formatDate, nFormatter } from "@dub/utils";
+import { LinearGradient } from "@visx/gradient";
import { useSearchParams } from "next/navigation";
-import { useMemo } from "react";
+import { Fragment, useMemo } from "react";
const RESOURCES = ["links", "events", "revenue"] as const;
@@ -27,6 +30,7 @@ export function UsageChart() {
<div className="h-64">
{chartData && chartData.length > 0 ? (
<TimeSeriesChart
+ key={resource}
type="bar"
data={chartData}
series={[
@@ -37,9 +41,56 @@ export function UsageChart() {
isActive: true,
},
]}
+ tooltipClassName="p-0"
+ tooltipContent={(d) => {
+ return (
+ <>
+ <p className="border-b border-gray-200 px-4 py-3 text-sm text-gray-900">
+ {formatDate(d.date)}
+ </p>
+ <div className="grid grid-cols-2 gap-x-6 gap-y-2 px-4 py-3 text-sm">
+ <Fragment key={resource}>
+ <div className="flex items-center gap-2">
+ <div className="h-2 w-2 rounded-sm bg-violet-500 shadow-[inset_0_0_0_1px_#0003]" />
+ <p className="capitalize text-gray-600">{resource}</p>
+ </div>
+ <p className="text-right font-medium text-gray-900">
+ {resource === "revenue" && "$"}
+ {nFormatter(d.values.usage, { full: true })}
+ </p>
+ </Fragment>
+ </div>
+ </>
+ );
+ }}
>
- <Bars />
+ <LinearGradient id="usage-bar-gradient">
+ <stop stopColor="#7E3AEA" stopOpacity={1} offset="20%" />
+ <stop stopColor="#D8277A" stopOpacity={0} offset="100%" />
+ </LinearGradient>
+ <Bars
+ seriesStyles={[
+ {
+ id: "usage",
+ barFill: "#00000019",
+ },
+ ]}
+ />
+ <Bars
+ seriesStyles={[
+ {
+ id: "usage",
+ barFill: "url(#usage-bar-gradient)",
+ },
+ ]}
+ />
<XAxis />
+ <YAxis
+ showGridLines
+ tickFormat={
+ resource === "revenue" ? (v) => `$${nFormatter(v)}` : nFormatter
+ }
+ />
</TimeSeriesChart>
) : null}
</div>
diff --git a/apps/web/package.json b/apps/web/package.json
index 2f699df886..c28b603c0b 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -41,6 +41,7 @@
"@vercel/functions": "^1.4.2",
"@vercel/og": "^0.6.3",
"@visx/axis": "^2.14.0",
+ "@visx/clip-path": "^3.3.0",
"@visx/curve": "^3.3.0",
"@visx/event": "^2.6.0",
"@visx/geo": "^2.10.0",
diff --git a/apps/web/ui/charts/bars.tsx b/apps/web/ui/charts/bars.tsx
index 6dfe66c0fb..6af8d7572b 100644
--- a/apps/web/ui/charts/bars.tsx
+++ b/apps/web/ui/charts/bars.tsx
@@ -1,85 +1,107 @@
import { cn } from "@dub/utils";
+import { RectClipPath } from "@visx/clip-path";
import { LinearGradient } from "@visx/gradient";
import { Group } from "@visx/group";
import { BarRounded } from "@visx/shape";
import { AnimatePresence, motion } from "framer-motion";
-import { useMemo } from "react";
+import { useId } from "react";
import { useChartContext } from "./chart-context";
export function Bars({
- seriesClassNames,
+ seriesStyles,
}: {
- seriesClassNames?: { id: string; gradient?: string; bar?: string }[];
+ seriesStyles?: {
+ id: string;
+ gradientClassName?: string;
+ barClassName?: string;
+ barFill?: string;
+ }[];
}) {
- const { data, series, margin, xScale, yScale, height, startDate, endDate } =
- useChartContext();
+ const clipPathId = useId();
+ const {
+ data,
+ series,
+ margin,
+ xScale,
+ yScale,
+ width,
+ height,
+ startDate,
+ endDate,
+ } = useChartContext();
if (!("bandwidth" in xScale))
throw new Error("Bars require a band scale (type=bar)");
- // Data with all values set to zero to animate from
- const zeroedData = useMemo(() => {
- return data.map((d) => ({
- ...d,
- values: Object.fromEntries(Object.keys(d.values).map((key) => [key, 0])),
- })) as typeof data;
- }, [data]);
-
return (
<Group left={margin.left} top={margin.top}>
+ <RectClipPath id={clipPathId} x={0} y={0} width={width} height={height} />
<AnimatePresence>
{series
.filter(({ isActive }) => isActive)
- .map((s) => (
- // Prevent ugly x-scale animations when start/end dates change with unique key
- <motion.g
- initial={{ opacity: 1 }}
- exit={{ opacity: 0 }}
- transition={{ duration: 0.1 }}
- key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
- >
- {/* Bar gradient */}
- <LinearGradient
- className={cn(
- s.colorClassName ?? "text-blue-500",
- seriesClassNames?.find(({ id }) => id === s.id)?.gradient,
- )}
- id={`${s.id}-background`}
- fromOffset="0%"
- from="currentColor"
- fromOpacity={0.01}
- toOffset="40%"
- to="currentColor"
- toOpacity={1}
- x1={0}
- x2={0}
- y1={1}
- />
+ .map((s) => {
+ const styles = seriesStyles?.find(({ id }) => id === s.id);
+ return (
+ // Prevent ugly x-scale animations when start/end dates change with unique key
+ <motion.g
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.1 }}
+ key={`${s.id}_${startDate.toString()}_${endDate.toString()}`}
+ clipPath={`url(#${clipPathId})`}
+ >
+ {/* Bar gradient */}
+ <LinearGradient
+ className={cn(
+ s.colorClassName ?? "text-blue-500",
+ styles?.gradientClassName,
+ )}
+ id={`${s.id}-background`}
+ fromOffset="0%"
+ from="currentColor"
+ fromOpacity={0.01}
+ toOffset="40%"
+ to="currentColor"
+ toOpacity={1}
+ x1={0}
+ x2={0}
+ y1={1}
+ />
- {/* Bars */}
- {data.map((d) => {
- const barWidth = xScale.bandwidth();
- const y = yScale(s.valueAccessor(d) ?? 0);
- const barHeight = height - y;
- return (
- <BarRounded
- key={d.date.toString()}
- x={xScale(d.date) ?? 0}
- y={y}
- width={barWidth}
- height={barHeight}
- radius={1000}
- top
- className={cn(
- s.colorClassName ?? "text-blue-700",
- seriesClassNames?.find(({ id }) => id === s.id)?.bar,
- )}
- fill={`url(#${s.id}-background)`}
- />
- );
- })}
- </motion.g>
- ))}
+ {/* Bars */}
+ <motion.g
+ initial={{ y: 100 }}
+ animate={{ y: 0 }}
+ transition={{ duration: 0.15, ease: "easeOut" }}
+ >
+ {data.map((d) => {
+ const barWidth = xScale.bandwidth();
+ const x = xScale(d.date) ?? 0;
+ const y = yScale(s.valueAccessor(d) ?? 0);
+ const barHeight = height - y;
+ const radius = Math.min(barWidth, barHeight) / 2;
+ return barHeight > 0 ? (
+ <BarRounded
+ key={d.date.toString()}
+ x={x}
+ y={y}
+ width={barWidth}
+ height={barHeight}
+ radius={1000}
+ top
+ className={cn(
+ s.colorClassName ?? "text-blue-700",
+ styles?.barClassName,
+ )}
+ fill={styles?.barFill ?? `url(#${s.id}-background)`}
+ />
+ ) : null;
+ })}
+ </motion.g>
+ </motion.g>
+ );
+ })}
</AnimatePresence>
</Group>
);
diff --git a/apps/web/ui/charts/time-series-chart.tsx b/apps/web/ui/charts/time-series-chart.tsx
index 798f4f5d96..a9a625215b 100644
--- a/apps/web/ui/charts/time-series-chart.tsx
+++ b/apps/web/ui/charts/time-series-chart.tsx
@@ -84,7 +84,8 @@ function TimeSeriesChartInner<T extends Datum>({
.filter((v): v is number => v != null);
return {
- minY: Math.min(...values),
+ // Start at 0 for bar charts
+ minY: type === "area" ? Math.min(...values) : Math.min(0, ...values),
maxY: Math.max(...values),
};
}, [data, series]);
@@ -114,7 +115,7 @@ function TimeSeriesChartInner<T extends Datum>({
align: 0.5,
}),
};
- }, [startDate, endDate, minY, maxY, height, width, type]);
+ }, [startDate, endDate, minY, maxY, height, width, data.length, type]);
const chartContext: ChartContextType<T> = {
type,
@@ -162,8 +163,11 @@ function TimeSeriesChartInner<T extends Datum>({
("bandwidth" in xScale ? (
<>
<Bar
- x={xScale(tooltipData.date) ?? 0}
- width={xScale.bandwidth()}
+ x={
+ (xScale(tooltipData.date) ?? 0) -
+ xScale.bandwidth() * xScale.padding()
+ }
+ width={xScale.bandwidth() * (1 + xScale.padding() * 2)}
y={0}
height={height}
fill="black"
@@ -218,7 +222,7 @@ function TimeSeriesChartInner<T extends Datum>({
key={tooltipData.date.toString()}
left={(tooltipLeft ?? 0) + margin.left}
top={(tooltipTop ?? 0) + margin.top}
- offsetLeft={8}
+ offsetLeft={"bandwidth" in xScale ? xScale.bandwidth() + 8 : 8}
offsetTop={12}
className="absolute"
unstyled={true}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3c2a154ab5..e06aafddf9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -119,6 +119,9 @@ importers:
'@visx/axis':
specifier: ^2.14.0
version: 2.14.0([email protected])
+ '@visx/clip-path':
+ specifier: ^3.3.0
+ version: 3.3.0([email protected])
'@visx/curve':
specifier: ^3.3.0
version: 3.3.0
@@ -10028,6 +10031,16 @@ packages:
react-dom: 18.2.0([email protected])
dev: false
+ resolution: {integrity: sha512-uMuI2M05qZTgUdTSHJGg4VDr2gytGmGyuaC89iByHqNaeMHkrJqQi/cOFAZi4D0dn75p7lVirJijEgDgSpcrMQ==}
+ peerDependencies:
+ react: ^16.0.0-0 || ^17.0.0-0 || ^18.0.0-0
+ dependencies:
+ '@types/react': 18.2.48
+ prop-types: 15.8.1
+ react: 18.2.0
+ dev: false
+
resolution: {integrity: sha512-9b6JOnx91gmOQiSPhUOxdsvcnW88fgqfTPKoVgQxidMsD/I3wksixtwo8TR/vtEz2aHzzsEEhlv1qK7Y3yaSDw==}
dependencies: