Add revenue chart cumulative mode.

This commit is contained in:
Francis Cao committed 2026-05-12 10:32:43 -07:00
1 parent 21b56d2922
commit c6ec8385c9
6 files changed
+146 -178

No files matched your search

+3 -3
View File
@@ -1,5 +1,3 @@
import { useTheme } from '@umami/react-zen';
import { memo, useCallback, useMemo, useState } from 'react';
import { Chart, type ChartProps } from '@/components/charts/Chart';
import { ChartTooltip } from '@/components/charts/ChartTooltip';
import { useLocale } from '@/components/hooks';
@@ -7,6 +5,8 @@ import { renderNumberLabels } from '@/lib/charts';
import { getThemeColors } from '@/lib/colors';
import { DATE_FORMATS, formatDate } from '@/lib/date';
import { formatLongCurrency, formatLongNumber } from '@/lib/format';
import { useTheme } from '@umami/react-zen';
import { memo, useCallback, useMemo, useState } from 'react';
const MemoChart = memo(Chart);
@@ -125,7 +125,7 @@ function BarChartComponent({
dateFormats[unit],
locale,
),
color: labelColors?.[0]?.backgroundColor,
color: labelColors?.[0]?.borderColor || labelColors?.[0]?.backgroundColor,
value: currency
? formatLongCurrency(dataPoints[0].raw.y, currency)
: `${formatLongNumber(dataPoints[0].raw.y)} ${dataPoints[0].dataset.label}`,
+2
View File
@@ -256,6 +256,8 @@ export const labels: Record<string, string> = {
month: 'label.month',
date: 'label.date',
pageOf: 'label.page-of',
period: 'label.period',
cumulative: 'label.cumulative',
create: 'label.create',
search: 'label.search',
numberOfRecords: 'label.number-of-records',
+103 -5
View File
@@ -1,25 +1,106 @@
import { colord } from 'colord';
import { useCallback, useMemo } from 'react';
import { BarChart } from '@/components/charts/BarChart';
import { useLocale } from '@/components/hooks';
import { useLocale, useMessages } from '@/components/hooks';
import { renderDateLabels } from '@/lib/charts';
import { CHART_COLORS } from '@/lib/constants';
import { generateTimeSeries } from '@/lib/date';
import { DATE_FORMATS, DATE_FUNCTIONS, formatDate, generateTimeSeries } from '@/lib/date';
export interface RevenueChartProps {
data: { x: string; t: string; y: number; count: number }[];
mode?: 'period' | 'cumulative';
unit: string;
minDate: Date;
maxDate: Date;
currency: string;
}
export function RevenueChart({ data, unit, minDate, maxDate, currency }: RevenueChartProps) {
export function RevenueChart({
data,
mode = 'period',
unit,
minDate,
maxDate,
currency,
}: RevenueChartProps) {
const { t, labels } = useMessages();
const { locale, dateLocale } = useLocale();
const isCumulative = mode === 'cumulative';
const chartData: any = useMemo(() => {
if (!data?.length) return { datasets: [] };
if (isCumulative) {
const cumulativeMaxDate = maxDate > new Date() ? new Date() : maxDate;
const cutoff = formatDate(cumulativeMaxDate, DATE_FORMATS[unit], dateLocale);
const bucketDates = getBucketDates(minDate, maxDate, unit, dateLocale);
const totals = data.reduce(
(obj, { t, y }) => {
obj[t] = (obj[t] || 0) + y;
return obj;
},
{} as Record<string, number>,
);
let total = 0;
const timeseries = generateTimeSeries(
Object.entries(totals).map(([x, y]) => ({ x, y })),
minDate,
maxDate,
unit,
dateLocale,
).map(point => {
const d = point.d ?? bucketDates.get(point.x);
if (point.x > cutoff) {
return {
...point,
d,
y: null,
};
}
total += point.y || 0;
return {
...point,
d,
y: total,
};
});
const color = colord(CHART_COLORS[0]);
return {
datasets: [
{
type: 'line',
label: t(labels.revenue),
data: timeseries,
backgroundColor: (context: any) => {
const { chart } = context;
const { ctx, chartArea } = chart;
if (!chartArea) {
return color.alpha(0.18).toRgbString();
}
const gradient = ctx.createLinearGradient(0, chartArea.top, 0, chartArea.bottom);
gradient.addColorStop(0, color.alpha(0.22).toRgbString());
gradient.addColorStop(1, color.alpha(0.02).toRgbString());
return gradient;
},
borderColor: color.alpha(0.95).toRgbString(),
borderWidth: 2,
pointRadius: 2,
tension: 0.25,
fill: true,
},
],
};
}
const map = data.reduce(
(obj, { x, t, y }) => {
if (!obj[x]) obj[x] = [];
@@ -41,7 +122,7 @@ export function RevenueChart({ data, unit, minDate, maxDate, currency }: Revenue
};
}),
};
}, [data, minDate, maxDate, unit, dateLocale]);
}, [data, minDate, maxDate, unit, dateLocale, isCumulative, t, labels.revenue]);
const renderXLabel = useCallback(renderDateLabels(unit, locale), [unit, locale]);
@@ -52,9 +133,26 @@ export function RevenueChart({ data, unit, minDate, maxDate, currency }: Revenue
minDate={minDate}
maxDate={maxDate}
currency={currency}
stacked={true}
stacked={!isCumulative}
renderXLabel={renderXLabel}
height="400px"
/>
);
}
function getBucketDates(minDate: Date, maxDate: Date, unit: string, locale: string) {
const add = DATE_FUNCTIONS[unit].add;
const start = DATE_FUNCTIONS[unit].start;
const fmt = DATE_FORMATS[unit];
const dates = new Map<string, Date>();
let current = start(minDate);
const end = start(maxDate);
while (current <= end) {
dates.set(formatDate(current, fmt, locale), current);
current = add(current, 1);
}
return dates;
}