openreplay/frontend/app/components/Charts/pieUtils.ts
2025-01-06 16:42:09 +01:00

31 lines
No EOL
826 B
TypeScript

import { colors } from './utils';
import { numberWithCommas } from 'App/utils';
export function buildPieData(
chart: Array<Record<string, any>>,
namesMap: string[]
) {
const result: { name: string; value: number }[] = namesMap.map((name) => {
let sum = 0;
chart.forEach((row) => {
sum += Number(row[name] ?? 0);
});
return { name, value: sum };
});
return result;
}
export function pieTooltipFormatter(params: any) {
const { name, value, marker, percent } = params;
return `
<div class="flex flex-col gap-1 bg-white shadow border rounded p-2 z-50">
<div style="margin-bottom: 2px;">${marker} <b>${name}</b></div>
<div>${numberWithCommas(value)} (${percent}%)</div>
</div>
`;
}
export function pickColorByIndex(idx: number) {
return colors[idx % colors.length];
}