Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

7.2 KB
Raw Blame
import { useCallback, useMemo, useRef } from 'react';
import { Color } from '@signozhq/design-tokens';
import { Group } from '@visx/group';
import { Pie as VisxPie } from '@visx/shape';
import { defaultStyles, useTooltip, useTooltipInPortal } from '@visx/tooltip';
import cx from 'classnames';
import { getYAxisFormattedValue } from 'components/Graph/yAxisConfig';
import { useResizeObserver } from 'hooks/useDimensions';
import Legend from 'lib/uPlotV2/components/Legend/Legend';
import { LegendPosition } from 'lib/uPlotV2/components/types';
import { LegendItem } from 'lib/uPlotV2/config/types';

import { PieChartProps, PieSlice } from 'lib/visualization/charts/types';
import { calculateChartDimensions } from 'lib/visualization/charts/utils';

import { useLegendVisibility } from 'lib/visualization/hooks/useLegendVisibility';
import PieArc from 'lib/visualization/charts/Pie/PieArc';
import PieCenterLabel from 'lib/visualization/charts/Pie/PieCenterLabel';
import styles from 'lib/visualization/charts/Pie/Pie.module.scss';
import { PieTooltipData } from 'lib/visualization/charts/Pie/types';
import {
	getDonutGeometry,
	getFillColor,
} from 'lib/visualization/charts/Pie/utils';

/**
 * Donut chart rendered with @visx. Splits its area into chart + legend with the
 * same `calculateChartDimensions` logic as the uPlot charts (right column /
 * up-to-two bottom rows), renders the shared chart Legend, and delegates the
 * arcs and centre total to PieArc / PieCenterLabel. Pure presentation — slices
 * are pre-resolved by the caller.
 */
export default function Pie({
	data,
	yAxisUnit,
	decimalPrecision,
	isDarkMode,
	position = LegendPosition.BOTTOM,
	id,
	onSliceClick,
	'data-testid': testId,
}: PieChartProps): JSX.Element {
	const labels = useMemo(() => data.map((slice) => slice.label), [data]);

	const { hiddenKeys, focusedSeriesIndex, setFocusedKey, onLegendAction } =
		useLegendVisibility({ keys: labels, id });

	const legendItems = useMemo<LegendItem[]>(
		() =>
			data.map((slice, index) => ({
				seriesIndex: index,
				label: slice.label,
				color: slice.color,
				show: !hiddenKeys.has(slice.label),
			})),
		[data, hiddenKeys],
	);

	// Hidden slices drop out so the remaining arcs + centre total recompute.
	const visibleData = useMemo(
		() => data.filter((slice) => !hiddenKeys.has(slice.label)),
		[data, hiddenKeys],
	);

	const {
		tooltipOpen,
		tooltipLeft,
		tooltipTop,
		tooltipData,
		hideTooltip,
		showTooltip,
	} = useTooltip<PieTooltipData>();

	const { containerRef, TooltipInPortal } = useTooltipInPortal({
		scroll: true,
		detectBounds: true,
	});

	const wrapperRef = useRef<HTMLDivElement>(null);
	const { width: containerWidth, height: containerHeight } =
		useResizeObserver(wrapperRef);

	// Reuse the uPlot chart/legend split so the donut + legend get the same area
	// allocation (right column, or up-to-two bottom rows) as every other panel.
	const {
		width,
		height,
		legendWidth,
		legendHeight,
		averageLegendWidth,
		showLegendSearch,
	} = useMemo(
		() =>
			calculateChartDimensions({
				containerWidth,
				containerHeight,
				legendConfig: { position },
				seriesLabels: labels,
			}),
		[containerWidth, containerHeight, position, labels],
	);

	// Donut geometry derived from the allocated chart box, sized to leave room
	// for the external leader labels (see getDonutGeometry).
	const { size, radius, innerRadius } = useMemo(
		() => getDonutGeometry(width, height),
		[width, height],
	);

	const totalValue = useMemo(
		() => visibleData.reduce((sum, slice) => sum + slice.value, 0),
		[visibleData],
	);

	const labelColor = isDarkMode ? Color.BG_VANILLA_100 : Color.BG_INK_400;
	const activeColor =
		focusedSeriesIndex !== null ? data[focusedSeriesIndex].color : null;

	const handleSliceEnter = useCallback(
		(slice: PieSlice, centroidX: number, centroidY: number): void => {
			showTooltip({
				tooltipData: {
					label: slice.label,
					value: getYAxisFormattedValue(
						slice.value.toString(),
						yAxisUnit || 'none',
						decimalPrecision,
					),
					color: slice.color,
				},
				tooltipTop: centroidY + height / 2,
				tooltipLeft: centroidX + width / 2,
			});
			setFocusedKey(slice.label);
		},
		[showTooltip, setFocusedKey, yAxisUnit, decimalPrecision, height, width],
	);

	const handleSliceLeave = useCallback((): void => {
		hideTooltip();
		setFocusedKey(null);
	}, [hideTooltip, setFocusedKey]);

	if (!data.length) {
		return (
			<div
				ref={wrapperRef}
				className={styles.pieChartWrapper}
				data-testid={testId}
			>
				<div className={styles.pieChartNoData}>No data</div>
			</div>
		);
	}

	const isRightLegend = position === LegendPosition.RIGHT;

	return (
		<div
			ref={wrapperRef}
			className={styles.pieChartWrapper}
			style={{ flexDirection: isRightLegend ? 'row' : 'column' }}
			data-testid={testId}
		>
			<div className={styles.pieChartContainer} style={{ width, height }}>
				{size > 0 && (
					<svg width={width} height={height} ref={containerRef}>
						<Group top={height / 2} left={width / 2}>
							<VisxPie
								data={visibleData}
								pieValue={(slice: PieSlice): number => slice.value}
								outerRadius={radius}
								innerRadius={innerRadius}
								padAngle={0.01}
								cornerRadius={3}
								width={size}
								height={size}
							>
								{(pie): JSX.Element[] =>
									pie.arcs.map((arc) => (
										<PieArc
											key={`arc-${arc.data.label}-${arc.data.value}-${arc.startAngle.toFixed(
												6,
											)}`}
											slice={arc.data}
											arcPath={pie.path(arc) || ''}
											centroid={pie.path.centroid(arc)}
											startAngle={arc.startAngle}
											endAngle={arc.endAngle}
											radius={radius}
											totalValue={totalValue}
											yAxisUnit={yAxisUnit}
											decimalPrecision={decimalPrecision}
											labelColor={labelColor}
											fill={getFillColor(arc.data.color, activeColor)}
											onEnter={handleSliceEnter}
											onLeave={handleSliceLeave}
											onClick={onSliceClick}
										/>
									))
								}
							</VisxPie>
							<PieCenterLabel
								total={totalValue}
								yAxisUnit={yAxisUnit}
								decimalPrecision={decimalPrecision}
								radius={radius}
								innerRadius={innerRadius}
								color={labelColor}
							/>
						</Group>
					</svg>
				)}
				{tooltipOpen && tooltipData && (
					<TooltipInPortal
						top={tooltipTop}
						left={tooltipLeft}
						className={styles.pieChartTooltip}
						style={{
							...defaultStyles,
							color: labelColor,
						}}
					>
						<div
							className={styles.pieChartIndicator}
							style={{ background: tooltipData.color }}
						/>
						<div className={styles.pieChartTooltipContent}>
							<span>{tooltipData.label}</span>
							<span className={styles.pieChartTooltipValue}>{tooltipData.value}</span>
						</div>
					</TooltipInPortal>
				)}
			</div>
			<div
				className={cx(styles.pieChartLegend, {
					[styles.pieChartLegendRight]: isRightLegend,
				})}
				style={{
					width: legendWidth,
					height: legendHeight,
				}}
			>
				<Legend
					items={legendItems}
					position={position}
					averageLegendWidth={averageLegendWidth}
					showSearch={showLegendSearch}
					focusedSeriesIndex={focusedSeriesIndex}
					onAction={onLegendAction}
				/>
			</div>
		</div>
	);
}