Add table view as alternative to donut chart for event properties

This commit is contained in:
Bas Broekhuizen 2025-07-08 10:17:18 +02:00
parent abc02465e3
commit 0c78e31300
3 changed files with 60 additions and 22 deletions

View file

@ -14,12 +14,14 @@
color: var(--primary400); color: var(--primary400);
} }
.title { .header {
text-align: center; margin-bottom: 40px;
font-weight: bold;
margin: 20px 0;
} }
.chart { .title {
font-weight: bold;
}
.data {
min-height: 620px; min-height: 620px;
} }

View file

@ -1,7 +1,9 @@
import { GridColumn, GridTable } from 'react-basics'; import { useMemo } from 'react';
import { GridColumn, GridTable, Flexbox, Button, ButtonGroup } from 'react-basics';
import { useEventDataProperties, useEventDataValues, useMessages } from '@/components/hooks'; import { useEventDataProperties, useEventDataValues, useMessages } from '@/components/hooks';
import { LoadingPanel } from '@/components/common/LoadingPanel'; import { LoadingPanel } from '@/components/common/LoadingPanel';
import PieChart from '@/components/charts/PieChart'; import PieChart from '@/components/charts/PieChart';
import ListTable from '@/components/metrics/ListTable';
import { useState } from 'react'; import { useState } from 'react';
import { CHART_COLORS } from '@/lib/constants'; import { CHART_COLORS } from '@/lib/constants';
import styles from './EventProperties.module.css'; import styles from './EventProperties.module.css';
@ -9,12 +11,19 @@ import styles from './EventProperties.module.css';
export function EventProperties({ websiteId }: { websiteId: string }) { export function EventProperties({ websiteId }: { websiteId: string }) {
const [propertyName, setPropertyName] = useState(''); const [propertyName, setPropertyName] = useState('');
const [eventName, setEventName] = useState(''); const [eventName, setEventName] = useState('');
const [propertyView, setPropertyView] = useState('table');
const { formatMessage, labels } = useMessages(); const { formatMessage, labels } = useMessages();
const { data, isLoading, isFetched, error } = useEventDataProperties(websiteId); const { data, isLoading, isFetched, error } = useEventDataProperties(websiteId);
const { data: values } = useEventDataValues(websiteId, eventName, propertyName); const { data: values } = useEventDataValues(websiteId, eventName, propertyName);
const chartData =
propertyName && values const propertySum = useMemo(() => {
? { return values?.reduce((sum, { total }) => sum + total, 0) ?? 0;
}, [values]);
const chartData = useMemo(() => {
if (!propertyName || !values) return null;
return {
labels: values.map(({ value }) => value), labels: values.map(({ value }) => value),
datasets: [ datasets: [
{ {
@ -23,8 +32,17 @@ export function EventProperties({ websiteId }: { websiteId: string }) {
borderWidth: 0, borderWidth: 0,
}, },
], ],
} };
: null; }, [propertyName, values]);
const tableData = useMemo(() => {
if (!propertyName || !values || propertySum === 0) return [];
return values.map(({ value, total }) => ({
x: value,
y: total,
z: 100 * (total / propertySum),
}));
}, [propertyName, values, propertySum]);
const handleRowClick = row => { const handleRowClick = row => {
setEventName(row.eventName); setEventName(row.eventName);
@ -52,9 +70,25 @@ export function EventProperties({ websiteId }: { websiteId: string }) {
<GridColumn name="total" label={formatMessage(labels.count)} alignment="end" /> <GridColumn name="total" label={formatMessage(labels.count)} alignment="end" />
</GridTable> </GridTable>
{propertyName && ( {propertyName && (
<div className={styles.chart}> <div className={styles.data}>
<div className={styles.title}>{propertyName}</div> <Flexbox className={styles.header} gap={12} justifyContent="space-between">
<div className={styles.title}>{`${eventName}: ${propertyName}`}</div>
<ButtonGroup
selectedKey={propertyView}
onSelect={key => setPropertyView(key as string)}
>
<Button key="table">{formatMessage(labels.table)}</Button>
<Button key="chart">{formatMessage(labels.chart)}</Button>
</ButtonGroup>
</Flexbox>
{values?.length === 0 ? (
<div>{formatMessage(labels.noData)}</div>
) : propertyView === 'table' ? (
<ListTable data={tableData} />
) : (
<PieChart key={propertyName + eventName} type="doughnut" data={chartData} /> <PieChart key={propertyName + eventName} type="doughnut" data={chartData} />
)}
</div> </div>
)} )}
</div> </div>

View file

@ -310,6 +310,8 @@ export const labels = defineMessages({
paidVideo: { id: 'label.paid-video', defaultMessage: 'Paid video' }, paidVideo: { id: 'label.paid-video', defaultMessage: 'Paid video' },
grouped: { id: 'label.grouped', defaultMessage: 'Grouped' }, grouped: { id: 'label.grouped', defaultMessage: 'Grouped' },
other: { id: 'label.other', defaultMessage: 'Other' }, other: { id: 'label.other', defaultMessage: 'Other' },
chart: { id: 'label.chart', defaultMessage: 'Chart' },
table: { id: 'label.table', defaultMessage: 'Table' },
}); });
export const messages = defineMessages({ export const messages = defineMessages({