Fixed bar chart rendering.

This commit is contained in:
Mike Cao 2025-06-18 21:31:42 -07:00
parent 96fcdefeed
commit 3a737e7e25
5 changed files with 47 additions and 15 deletions

View file

@ -5,7 +5,7 @@ import { Chart, ChartProps } from '@/components/charts/Chart';
import { useLocale } from '@/components/hooks'; import { useLocale } from '@/components/hooks';
import { renderNumberLabels } from '@/lib/charts'; import { renderNumberLabels } from '@/lib/charts';
import { getThemeColors } from '@/lib/colors'; import { getThemeColors } from '@/lib/colors';
import { formatDate } from '@/lib/date'; import { formatDate, formatDateByUnit } from '@/lib/date';
import { formatLongCurrency, formatLongNumber } from '@/lib/format'; import { formatLongCurrency, formatLongNumber } from '@/lib/format';
const dateFormats = { const dateFormats = {
@ -57,8 +57,9 @@ export function BarChart({
x: { x: {
type: XAxisType, type: XAxisType,
stacked: true, stacked: true,
min: minDate, min: formatDateByUnit(minDate, unit),
max: maxDate, max: formatDateByUnit(maxDate, unit),
offset: true,
time: { time: {
unit, unit,
}, },

View file

@ -11,7 +11,7 @@ const messages = {
'en-US': enUS, 'en-US': enUS,
}; };
const selector = (state: { locale: any }) => state.locale; const selector = (state: { locale: string }) => state.locale;
export function useLocale() { export function useLocale() {
const locale = useApp(selector); const locale = useApp(selector);

View file

@ -4,6 +4,7 @@ import { BarChart, BarChartProps } from '@/components/charts/BarChart';
import { useLocale, useMessages } from '@/components/hooks'; import { useLocale, useMessages } from '@/components/hooks';
import { renderDateLabels } from '@/lib/charts'; import { renderDateLabels } from '@/lib/charts';
import { getThemeColors } from '@/lib/colors'; import { getThemeColors } from '@/lib/colors';
import { formatDateByUnit } from '@/lib/date';
export interface PageviewsChartProps extends BarChartProps { export interface PageviewsChartProps extends BarChartProps {
data: { data: {
@ -20,7 +21,7 @@ export interface PageviewsChartProps extends BarChartProps {
export function PageviewsChart({ data, unit, ...props }: PageviewsChartProps) { export function PageviewsChart({ data, unit, ...props }: PageviewsChartProps) {
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { theme } = useTheme(); const { theme } = useTheme();
const { locale } = useLocale(); const { locale, dateLocale } = useLocale();
const { colors } = useMemo(() => getThemeColors(theme), [theme]); const { colors } = useMemo(() => getThemeColors(theme), [theme]);
const chartData: any = useMemo(() => { const chartData: any = useMemo(() => {
@ -31,14 +32,18 @@ export function PageviewsChart({ data, unit, ...props }: PageviewsChartProps) {
datasets: [ datasets: [
{ {
label: formatMessage(labels.visitors), label: formatMessage(labels.visitors),
data: data.sessions, data: convertDataset(data.sessions, unit, dateLocale),
borderWidth: 1, borderWidth: 1,
barPercentage: 0.9,
categoryPercentage: 0.9,
...colors.chart.visitors, ...colors.chart.visitors,
order: 3, order: 3,
}, },
{ {
label: formatMessage(labels.views), label: formatMessage(labels.views),
data: data.pageviews, data: convertDataset(data.pageviews, unit, dateLocale),
barPercentage: 0.9,
categoryPercentage: 0.9,
borderWidth: 1, borderWidth: 1,
...colors.chart.views, ...colors.chart.views,
order: 4, order: 4,
@ -81,3 +86,7 @@ export function PageviewsChart({ data, unit, ...props }: PageviewsChartProps) {
/> />
); );
} }
function convertDataset(data: { x: string; y: number }[], unit: string, locale?: any) {
return data.map(d => ({ ...d, x: formatDateByUnit(d.x, unit, locale) }));
}

View file

@ -34,6 +34,7 @@ import {
subWeeks, subWeeks,
endOfMinute, endOfMinute,
isSameDay, isSameDay,
parseISO,
} from 'date-fns'; } from 'date-fns';
import { getDateLocale } from '@/lib/lang'; import { getDateLocale } from '@/lib/lang';
import { DateRange } from '@/lib/types'; import { DateRange } from '@/lib/types';
@ -128,7 +129,7 @@ export function parseDateValue(value: string) {
} }
export function parseDateRange(value: string | object, locale = 'en-US'): DateRange { export function parseDateRange(value: string | object, locale = 'en-US'): DateRange {
if (typeof value === 'object') { if (typeof value !== 'string') {
return value as DateRange; return value as DateRange;
} }
@ -140,7 +141,7 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
}; };
} }
if (value?.startsWith?.('range')) { if (value.startsWith('range')) {
const [, startTime, endTime] = value.split(':'); const [, startTime, endTime] = value.split(':');
const startDate = new Date(+startTime); const startDate = new Date(+startTime);
@ -164,7 +165,7 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
switch (unit) { switch (unit) {
case 'hour': case 'hour':
return { return {
startDate: num ? subHours(startOfHour(now), num - 1) : startOfHour(now), startDate: num ? subHours(startOfHour(now), num) : startOfHour(now),
endDate: endOfHour(now), endDate: endOfHour(now),
offset: 0, offset: 0,
num: num || 1, num: num || 1,
@ -173,7 +174,7 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
}; };
case 'day': case 'day':
return { return {
startDate: num ? subDays(startOfDay(now), num - 1) : startOfDay(now), startDate: num ? subDays(startOfDay(now), num) : startOfDay(now),
endDate: endOfDay(now), endDate: endOfDay(now),
unit: num ? 'day' : 'hour', unit: num ? 'day' : 'hour',
offset: 0, offset: 0,
@ -183,7 +184,7 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
case 'week': case 'week':
return { return {
startDate: num startDate: num
? subWeeks(startOfWeek(now, { locale: dateLocale }), num - 1) ? subWeeks(startOfWeek(now, { locale: dateLocale }), num)
: startOfWeek(now, { locale: dateLocale }), : startOfWeek(now, { locale: dateLocale }),
endDate: endOfWeek(now, { locale: dateLocale }), endDate: endOfWeek(now, { locale: dateLocale }),
unit: 'day', unit: 'day',
@ -193,7 +194,7 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
}; };
case 'month': case 'month':
return { return {
startDate: num ? subMonths(startOfMonth(now), num - 1) : startOfMonth(now), startDate: num ? subMonths(startOfMonth(now), num) : startOfMonth(now),
endDate: endOfMonth(now), endDate: endOfMonth(now),
unit: num ? 'month' : 'day', unit: num ? 'month' : 'day',
offset: 0, offset: 0,
@ -202,7 +203,7 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
}; };
case 'year': case 'year':
return { return {
startDate: num ? subYears(startOfYear(now), num - 1) : startOfYear(now), startDate: num ? subYears(startOfYear(now), num) : startOfYear(now),
endDate: endOfYear(now), endDate: endOfYear(now),
unit: 'month', unit: 'month',
offset: 0, offset: 0,
@ -282,6 +283,27 @@ export function getMinimumUnit(startDate: number | Date, endDate: number | Date)
return 'year'; return 'year';
} }
export function formatDateByUnit(dateInput: string | Date, unit: string, locale?: any) {
const date = typeof dateInput === 'string' ? parseISO(dateInput) : dateInput;
switch (unit) {
case 'minute':
return format(startOfMinute(date), 'yyyy-MM-dd HH:mm');
case 'hour':
return format(startOfHour(date), 'yyyy-MM-dd HH');
case 'day':
return format(startOfDay(date), 'yyyy-MM-dd');
case 'week':
return format(startOfWeek(date, { locale }), "yyyy-'W'II");
case 'month':
return format(startOfMonth(date), 'yyyy-MM');
case 'year':
return format(startOfYear(date), 'yyyy');
default:
return format(startOfDay(date), 'yyyy-MM-dd');
}
}
export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) { export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) {
const arr = []; const arr = [];
const { diff, add, start } = DATE_FUNCTIONS[unit]; const { diff, add, start } = DATE_FUNCTIONS[unit];

View file

@ -76,7 +76,7 @@ async function clickhouseQuery(
from ( from (
select select
${getDateSQL('website_event.created_at', unit, timezone)} as t, ${getDateSQL('website_event.created_at', unit, timezone)} as t,
sum(views)as y sum(views) as y
from website_event_stats_hourly website_event from website_event_stats_hourly website_event
where website_id = {websiteId:UUID} where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64} and created_at between {startDate:DateTime64} and {endDate:DateTime64}