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.

feat: added new barpanel component (#10266)

Abhi kumar2c948ef9f63a
diff --git a/frontend/src/container/DashboardContainer/visualization/charts/BarChart/BarChart.tsx b/frontend/src/container/DashboardContainer/visualization/charts/BarChart/BarChart.tsx
new file mode 100644
index 0000000000..c165dab6d3
--- /dev/null
+++ b/frontend/src/container/DashboardContainer/visualization/charts/BarChart/BarChart.tsx
@@ -0,0 +1,45 @@
+import { useCallback } from 'react';
+import ChartWrapper from 'container/DashboardContainer/visualization/charts/ChartWrapper/ChartWrapper';
+import BarChartTooltip from 'lib/uPlotV2/components/Tooltip/BarChartTooltip';
+import {
+ BarTooltipProps,
+ TooltipRenderArgs,
+} from 'lib/uPlotV2/components/types';
+
+import { useBarChartStacking } from '../../hooks/useBarChartStacking';
+import { BarChartProps } from '../types';
+
+export default function BarChart(props: BarChartProps): JSX.Element {
+ const { children, isStackedBarChart, config, data, ...rest } = props;
+
+ const chartData = useBarChartStacking({
+ data,
+ isStackedBarChart,
+ config,
+ });
+
+ const renderTooltip = useCallback(
+ (props: TooltipRenderArgs): React.ReactNode => {
+ const tooltipProps: BarTooltipProps = {
+ ...props,
+ timezone: rest.timezone,
+ yAxisUnit: rest.yAxisUnit,
+ decimalPrecision: rest.decimalPrecision,
+ isStackedBarChart: isStackedBarChart,
+ };
+ return <BarChartTooltip {...tooltipProps} />;
+ },
+ [rest.timezone, rest.yAxisUnit, rest.decimalPrecision, isStackedBarChart],
+ );
+
+ return (
+ <ChartWrapper
+ {...rest}
+ config={config}
+ data={chartData}
+ renderTooltip={renderTooltip}
+ >
+ {children}
+ </ChartWrapper>
+ );
+}
diff --git a/frontend/src/container/DashboardContainer/visualization/charts/utils/stackSeriesUtils.ts b/frontend/src/container/DashboardContainer/visualization/charts/utils/stackSeriesUtils.ts
new file mode 100644
index 0000000000..1fc52321fd
--- /dev/null
+++ b/frontend/src/container/DashboardContainer/visualization/charts/utils/stackSeriesUtils.ts
@@ -0,0 +1,116 @@
+import uPlot, { AlignedData } from 'uplot';
+
+/**
+ * Stack data cumulatively (top-down: first series = top, last = bottom).
+ * When `omit(seriesIndex)` returns true, that series is excluded from stacking.
+ */
+export function stackSeries(
+ data: AlignedData,
+ omit: (seriesIndex: number) => boolean,
+): { data: AlignedData; bands: uPlot.Band[] } {
+ const timeAxis = data[0];
+ const pointCount = timeAxis.length;
+ const valueSeriesCount = data.length - 1; // exclude time axis
+
+ const stackedSeries = buildStackedSeries({
+ data,
+ valueSeriesCount,
+ pointCount,
+ omit,
+ });
+ const bands = buildFillBands(valueSeriesCount + 1, omit); // +1 for 1-based series indices
+
+ return {
+ data: [timeAxis, ...stackedSeries] as AlignedData,
+ bands,
+ };
+}
+
+interface BuildStackedSeriesParams {
+ data: AlignedData;
+ valueSeriesCount: number;
+ pointCount: number;
+ omit: (seriesIndex: number) => boolean;
+}
+
+/**
+ * Accumulate from last series upward: last series = raw values, first = total.
+ * Omitted series are copied as-is (no accumulation).
+ */
+function buildStackedSeries({
+ data,
+ valueSeriesCount,
+ pointCount,
+ omit,
+}: BuildStackedSeriesParams): (number | null)[][] {
+ const stackedSeries: (number | null)[][] = Array(valueSeriesCount);
+ const cumulativeSums = Array(pointCount).fill(0) as number[];
+
+ for (let seriesIndex = valueSeriesCount; seriesIndex >= 1; seriesIndex--) {
+ const rawValues = data[seriesIndex] as (number | null)[];
+
+ if (omit(seriesIndex)) {
+ stackedSeries[seriesIndex - 1] = rawValues;
+ } else {
+ stackedSeries[seriesIndex - 1] = rawValues.map((rawValue, pointIndex) => {
+ const numericValue = rawValue == null ? 0 : Number(rawValue);
+ return (cumulativeSums[pointIndex] += numericValue);
+ });
+ }
+ }
+
+ return stackedSeries;
+}
+
+/**
+ * Bands define fill between consecutive visible series for stacked appearance.
+ * uPlot format: [upperSeriesIdx, lowerSeriesIdx].
+ */
+function buildFillBands(
+ seriesLength: number,
+ omit: (seriesIndex: number) => boolean,
+): uPlot.Band[] {
+ const bands: uPlot.Band[] = [];
+
+ for (let seriesIndex = 1; seriesIndex < seriesLength; seriesIndex++) {
+ if (omit(seriesIndex)) {
+ continue;
+ }
+ const nextVisibleSeriesIndex = findNextVisibleSeriesIndex(
+ seriesLength,
+ seriesIndex,
+ omit,
+ );
+ if (nextVisibleSeriesIndex !== -1) {
+ bands.push({ series: [seriesIndex, nextVisibleSeriesIndex] });
+ }
+ }
+
+ return bands;
+}
+
+function findNextVisibleSeriesIndex(
+ seriesLength: number,
+ afterIndex: number,
+ omit: (seriesIndex: number) => boolean,
+): number {
+ for (let i = afterIndex + 1; i < seriesLength; i++) {
+ if (!omit(i)) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+/**
+ * Returns band indices for initial stacked state (no series omitted).
+ * Top-down: first series at top, band fills between consecutive series.
+ * uPlot band format: [upperSeriesIdx, lowerSeriesIdx].
+ */
+export function getInitialStackedBands(seriesCount: number): uPlot.Band[] {
+ const bands: uPlot.Band[] = [];
+ for (let seriesIndex = 1; seriesIndex < seriesCount; seriesIndex++) {
+ bands.push({ series: [seriesIndex, seriesIndex + 1] });
+ }
+ return bands;
+}
diff --git a/frontend/src/container/DashboardContainer/visualization/hooks/useBarChartStacking.ts b/frontend/src/container/DashboardContainer/visualization/hooks/useBarChartStacking.ts
new file mode 100644
index 0000000000..48b1171499
--- /dev/null
+++ b/frontend/src/container/DashboardContainer/visualization/hooks/useBarChartStacking.ts
@@ -0,0 +1,125 @@
+import {
+ MutableRefObject,
+ useCallback,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+} from 'react';
+import { UPlotConfigBuilder } from 'lib/uPlotV2/config/UPlotConfigBuilder';
+import { has } from 'lodash-es';
+import uPlot from 'uplot';
+
+import { stackSeries } from '../charts/utils/stackSeriesUtils';
+
+/** Returns true if the series at the given index is hidden (e.g. via legend toggle). */
+function isSeriesHidden(plot: uPlot, seriesIndex: number): boolean {
+ return !plot.series[seriesIndex]?.show;
+}
+
+function canApplyStacking(
+ unstackedData: uPlot.AlignedData | null,
+ plot: uPlot,
+ isUpdating: boolean,
+): boolean {
+ return (
+ !isUpdating &&
+ !!unstackedData &&
+ !!plot.data &&
+ unstackedData[0]?.length === plot.data[0]?.length
+ );
+}
+
+function setupStackingHooks(
+ config: UPlotConfigBuilder,
+ applyStackingToChart: (plot: uPlot) => void,
+ isUpdatingRef: MutableRefObject<boolean>,
+): () => void {
+ const onDataChange = (plot: uPlot): void => {
+ if (!isUpdatingRef.current) {
+ applyStackingToChart(plot);
+ }
+ };
+
+ const onSeriesVisibilityChange = (
+ plot: uPlot,
+ _seriesIdx: number | null,
+ opts: uPlot.Series,
+ ): void => {
+ if (!has(opts, 'focus')) {
+ applyStackingToChart(plot);
+ }
+ };
+
+ const removeSetDataHook = config.addHook('setData', onDataChange);
+ const removeSetSeriesHook = config.addHook(
+ 'setSeries',
+ onSeriesVisibilityChange,
+ );
+
+ return (): void => {
+ removeSetDataHook?.();
+ removeSetSeriesHook?.();
+ };
+}
+
+export interface UseBarChartStackingParams {
+ data: uPlot.AlignedData;
+ isStackedBarChart?: boolean;
+ config: UPlotConfigBuilder | null;
+}
+
+/**
+ * Handles stacking for bar charts: computes initial stacked data and re-stacks
+ * when data or series visibility changes (e.g. legend toggles).
+ */
+export function useBarChartStacking({
+ data,
+ isStackedBarChart = false,
+ config,
+}: UseBarChartStackingParams): uPlot.AlignedData {
+ // Store unstacked source data so uPlot hooks can access it (hooks run outside React's render cycle)
+ const unstackedDataRef = useRef<uPlot.AlignedData | null>(null);
+ unstackedDataRef.current = isStackedBarChart ? data : null;
+
+ // Prevents re-entrant calls when we update chart data (avoids infinite loop in setData hook)
+ const isUpdatingChartRef = useRef(false);
+
+ const chartData = useMemo((): uPlot.AlignedData => {
+ if (!isStackedBarChart || !data || data.length < 2) {
+ return data;
+ }
+ const noSeriesHidden = (): boolean => false; // include all series in initial stack
+ const { data: stacked } = stackSeries(data, noSeriesHidden);
+ return stacked;
+ }, [data, isStackedBarChart]);
+
+ const applyStackingToChart = useCallback((plot: uPlot): void => {
+ const unstacked = unstackedDataRef.current;
+ if (
+ !unstacked ||
+ !canApplyStacking(unstacked, plot, isUpdatingChartRef.current)
+ ) {
+ return;
+ }
+
+ const shouldExcludeSeries = (idx: number): boolean =>
+ isSeriesHidden(plot, idx);
+ const { data: stacked, bands } = stackSeries(unstacked, shouldExcludeSeries);
+
+ plot.delBand(null);
+ bands.forEach((band: uPlot.Band) => plot.addBand(band));
+
+ isUpdatingChartRef.current = true;
+ plot.setData(stacked);
+ isUpdatingChartRef.current = false;
+ }, []);
+
+ useLayoutEffect(() => {
+ if (!isStackedBarChart || !config) {
+ return undefined;
+ }
+ return setupStackingHooks(config, applyStackingToChart, isUpdatingChartRef);
+ }, [isStackedBarChart, config, applyStackingToChart]);
+
+ return chartData;
+}
diff --git a/frontend/src/container/DashboardContainer/visualization/panels/BarPanel/BarPanel.tsx b/frontend/src/container/DashboardContainer/visualization/panels/BarPanel/BarPanel.tsx
new file mode 100644
index 0000000000..19726099a6
--- /dev/null
+++ b/frontend/src/container/DashboardContainer/visualization/panels/BarPanel/BarPanel.tsx
@@ -0,0 +1,160 @@
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { PanelWrapperProps } from 'container/PanelWrapper/panelWrapper.types';
+import { useIsDarkMode } from 'hooks/useDarkMode';
+import { useResizeObserver } from 'hooks/useDimensions';
+import { LegendPosition } from 'lib/uPlotV2/components/types';
+import ContextMenu from 'periscope/components/ContextMenu';
+import { useDashboard } from 'providers/Dashboard/Dashboard';
+import { useTimezone } from 'providers/Timezone';
+import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
+import uPlot from 'uplot';
+import { getTimeRange } from 'utils/getTimeRange';
+
+import BarChart from '../../charts/BarChart/BarChart';
+import ChartManager from '../../components/ChartManager/ChartManager';
+import { usePanelContextMenu } from '../../hooks/usePanelContextMenu';
+import { prepareBarPanelConfig, prepareBarPanelData } from './utils';
+
+import '../Panel.styles.scss';
+
+function BarPanel(props: PanelWrapperProps): JSX.Element {
+ const {
+ panelMode,
+ queryResponse,
+ widget,
+ onDragSelect,
+ isFullViewMode,
+ onToggleModelHandler,
+ } = props;
+ const uPlotRef = useRef<uPlot | null>(null);
+ const { toScrollWidgetId, setToScrollWidgetId } = useDashboard();
+ const graphRef = useRef<HTMLDivElement>(null);
+ const [minTimeScale, setMinTimeScale] = useState<number>();
+ const [maxTimeScale, setMaxTimeScale] = useState<number>();
+ const containerDimensions = useResizeObserver(graphRef);
+
+ const isDarkMode = useIsDarkMode();
+ const { timezone } = useTimezone();
+
+ useEffect(() => {
+ if (toScrollWidgetId === widget.id) {
+ graphRef.current?.scrollIntoView({
+ behavior: 'smooth',
+ block: 'center',
+ });
+ graphRef.current?.focus();
+ setToScrollWidgetId('');
+ }
+ }, [toScrollWidgetId, setToScrollWidgetId, widget.id]);
+
+ useEffect((): void => {
+ const { startTime, endTime } = getTimeRange(queryResponse);
+
+ setMinTimeScale(startTime);
+ setMaxTimeScale(endTime);
+ }, [queryResponse]);
+
+ const {
+ coordinates,
+ popoverPosition,
+ onClose,
+ menuItemsConfig,
+ clickHandlerWithContextMenu,
+ } = usePanelContextMenu({
+ widget,
+ queryResponse,
+ });
+
+ const config = useMemo(() => {
+ return prepareBarPanelConfig({
+ widget,
+ isDarkMode,
+ currentQuery: widget.query,
+ onClick: clickHandlerWithContextMenu,
+ onDragSelect,
+ apiResponse: queryResponse?.data?.payload as MetricRangePayloadProps,
+ timezone,
+ panelMode,
+ minTimeScale: minTimeScale,
+ maxTimeScale: maxTimeScale,
+ });
+ }, [
+ widget,
+ isDarkMode,
+ queryResponse?.data?.payload,
+ clickHandlerWithContextMenu,
+ onDragSelect,
+ minTimeScale,
+ maxTimeScale,
+ timezone,
+ panelMode,
+ ]);
+
+ const chartData = useMemo(() => {
+ if (!queryResponse?.data?.payload) {
+ return [];
+ }
+ return prepareBarPanelData(queryResponse?.data?.payload);
+ }, [queryResponse?.data?.payload]);
+
+ const layoutChildren = useMemo(() => {
+ if (!isFullViewMode) {
+ return null;
+ }
+ return (
+ <ChartManager
+ config={config}
+ alignedData={chartData}
+ yAxisUnit={widget.yAxisUnit}
+ onCancel={onToggleModelHandler}
+ />
+ );
+ }, [
+ isFullViewMode,
+ config,
+ chartData,
+ widget.yAxisUnit,
+ onToggleModelHandler,
+ ]);
+
+ const onPlotDestroy = useCallback(() => {
+ uPlotRef.current = null;
+ }, []);
+
+ const onPlotRef = useCallback((plot: uPlot | null): void => {
+ uPlotRef.current = plot;
+ }, []);
+
+ return (
+ <div className="panel-container" ref={graphRef}>
+ {containerDimensions.width > 0 && containerDimensions.height > 0 && (
+ <BarChart
+ config={config}
+ legendConfig={{
+ position: widget?.legendPosition ?? LegendPosition.BOTTOM,
+ }}
+ plotRef={onPlotRef}
+ onDestroy={onPlotDestroy}
+ yAxisUnit={widget.yAxisUnit}
+ decimalPrecision={widget.decimalPrecision}
+ timezone={timezone.value}
+ data={chartData as uPlot.AlignedData}
+ width={containerDimensions.width}
+ height={containerDimensions.height}
+ layoutChildren={layoutChildren}
+ isStackedBarChart={widget.stackedBarChart ?? false}
+ >
+ <ContextMenu
+ coordinates={coordinates}
+ popoverPosition={popoverPosition}
+ title={menuItemsConfig.header as string}
+ items={menuItemsConfig.items}
+ onClose={onClose}
+ />
+ </BarChart>
+ )}
+ </div>
+ );
+}
+
+export default BarPanel;
diff --git a/frontend/src/container/DashboardContainer/visualization/panels/BarPanel/utils.ts b/frontend/src/container/DashboardContainer/visualization/panels/BarPanel/utils.ts
new file mode 100644
index 0000000000..76174f898f
--- /dev/null
+++ b/frontend/src/container/DashboardContainer/visualization/panels/BarPanel/utils.ts
@@ -0,0 +1,108 @@
+import { Timezone } from 'components/CustomTimePicker/timezoneUtils';
+import { PANEL_TYPES } from 'constants/queryBuilder';
+import { getInitialStackedBands } from 'container/DashboardContainer/visualization/charts/utils/stackSeriesUtils';
+import { getLegend } from 'lib/dashboard/getQueryResults';
+import getLabelName from 'lib/getLabelName';
+import { OnClickPluginOpts } from 'lib/uPlotLib/plugins/onClickPlugin';
+import {
+ DrawStyle,
+ LineInterpolation,
+ LineStyle,
+ VisibilityMode,
+} from 'lib/uPlotV2/config/types';
+import { UPlotConfigBuilder } from 'lib/uPlotV2/config/UPlotConfigBuilder';
+import { Widgets } from 'types/api/dashboard/getAll';
+import { MetricRangePayloadProps } from 'types/api/metrics/getQueryRange';
+import { Query } from 'types/api/queryBuilder/queryBuilderData';
+import { QueryData } from 'types/api/widgets/getQuery';
+import { AlignedData } from 'uplot';
+
+import { PanelMode } from '../types';
+import { fillMissingXAxisTimestamps, getXAxisTimestamps } from '../utils';
+import { buildBaseConfig } from '../utils/baseConfigBuilder';
+
+export function prepareBarPanelData(
+ apiResponse: MetricRangePayloadProps,
+): AlignedData {
+ const seriesList = apiResponse?.data?.result || [];
+ const timestampArr = getXAxisTimestamps(seriesList);
+ const yAxisValuesArr = fillMissingXAxisTimestamps(timestampArr, seriesList);
+ return [timestampArr, ...yAxisValuesArr];
+}
+
+export function prepareBarPanelConfig({
+ widget,
+ isDarkMode,
+ currentQuery,
+ onClick,
+ onDragSelect,
+ apiResponse,
+ timezone,
+ panelMode,
+ minTimeScale,
+ maxTimeScale,
+}: {
+ widget: Widgets;
+ isDarkMode: boolean;
+ currentQuery: Query;
+ onClick: OnClickPluginOpts['onClick'];
+ onDragSelect: (startTime: number, endTime: number) => void;
+ apiResponse: MetricRangePayloadProps;
+ timezone: Timezone;
+ panelMode: PanelMode;
+ minTimeScale?: number;
+ maxTimeScale?: number;
+}): UPlotConfigBuilder {
+ const builder = buildBaseConfig({
+ widget,
+ isDarkMode,
+ onClick,
+ onDragSelect,
+ apiResponse,
+ timezone,
+ panelMode,
+ panelType: PANEL_TYPES.BAR,
+ minTimeScale,
+ maxTimeScale,
+ });
+
+ builder.setCursor({
+ focus: {
+ prox: 1e3,
+ },
+ });
+
+ if (widget.stackedBarChart) {
+ const seriesCount = (apiResponse?.data?.result?.length ?? 0) + 1; // +1 for 1-based uPlot series indices
+ builder.setBands(getInitialStackedBands(seriesCount));
+ }
+
+ const seriesList: QueryData[] = apiResponse?.data?.result || [];
+ seriesList.forEach((series) => {
+ const baseLabelName = getLabelName(
+ series.metric,
+ series.queryName || '', // query
+ series.legend || '',
+ );
+
+ const label = currentQuery
+ ? getLegend(series, currentQuery, baseLabelName)
+ : baseLabelName;
+
+ builder.addSeries({
+ scaleKey: 'y',
+ drawStyle: DrawStyle.Bar,
+ panelType: PANEL_TYPES.BAR,
+ label: label,
+ colorMapping: widget.customLegendColors ?? {},
+ spanGaps: false,
+ lineStyle: LineStyle.Solid,
+ lineInterpolation: LineInterpolation.Spline,
+ showPoints: VisibilityMode.Never,
+ pointSize: 5,
+ isDarkMode,
+ });
+ });
+
+ return builder;
+}
diff --git a/frontend/src/lib/uPlotV2/components/Tooltip/BarChartTooltip.tsx b/frontend/src/lib/uPlotV2/components/Tooltip/BarChartTooltip.tsx
new file mode 100644
index 0000000000..a4bd3bdeb5
--- /dev/null
+++ b/frontend/src/lib/uPlotV2/components/Tooltip/BarChartTooltip.tsx
@@ -0,0 +1,31 @@
+import { useMemo } from 'react';
+
+import { BarTooltipProps, TooltipContentItem } from '../types';
+import Tooltip from './Tooltip';
+import { buildTooltipContent } from './utils';
+
+export default function BarChartTooltip(props: BarTooltipProps): JSX.Element {
+ const content = useMemo(
+ (): TooltipContentItem[] =>
+ buildTooltipContent({
+ data: props.uPlotInstance.data,
+ series: props.uPlotInstance.series,
+ dataIndexes: props.dataIndexes,
+ activeSeriesIndex: props.seriesIndex,
+ uPlotInstance: props.uPlotInstance,
+ yAxisUnit: props.yAxisUnit ?? '',
+ decimalPrecision: props.decimalPrecision,
+ isStackedBarChart: props.isStackedBarChart,
+ }),
+ [
+ props.uPlotInstance,
+ props.seriesIndex,
+ props.dataIndexes,
+ props.yAxisUnit,
+ props.decimalPrecision,
+ props.isStackedBarChart,
+ ],
+ );
+
+ return <Tooltip {...props} content={content} />;
+}
diff --git a/frontend/src/lib/uPlotV2/components/Tooltip/utils.ts b/frontend/src/lib/uPlotV2/components/Tooltip/utils.ts
index 394e9e1b75..2f73ef04b0 100644
--- a/frontend/src/lib/uPlotV2/components/Tooltip/utils.ts
+++ b/frontend/src/lib/uPlotV2/components/Tooltip/utils.ts
@@ -25,16 +25,28 @@ export function getTooltipBaseValue({
index,
dataIndex,
isStackedBarChart,
+ series,
}: {
data: AlignedData;
index: number;
dataIndex: number;
isStackedBarChart?: boolean;
+ series?: Series[];
}): number | null {
let baseValue = data[index][dataIndex] ?? null;
- if (isStackedBarChart && index + 1 < data.length && baseValue !== null) {
- const nextValue = data[index + 1][dataIndex] ?? null;
- if (nextValue !== null) {
+ // Top-down stacking (first series at top): raw = stacked[i] - stacked[nextVisible].
+ // When series are hidden, we must use the next *visible* series, not index+1,
+ // since hidden series keep raw values and would produce negative/wrong results.
+ if (isStackedBarChart && baseValue !== null && series) {
+ let nextVisibleIdx = -1;
+ for (let j = index + 1; j < series.length; j++) {
+ if (series[j]?.show) {
+ nextVisibleIdx = j;
+ break;
+ }
+ }
+ if (nextVisibleIdx >= 1) {
+ const nextValue = data[nextVisibleIdx][dataIndex] ?? 0;
baseValue = baseValue - nextValue;
}
}
@@ -80,6 +92,7 @@ export function buildTooltipContent({
index,
dataIndex,
isStackedBarChart,
+ series,
});
const isActive = index === activeSeriesIndex;
diff --git a/frontend/src/lib/uPlotV2/config/UPlotSeriesBuilder.ts b/frontend/src/lib/uPlotV2/config/UPlotSeriesBuilder.ts
index acc3161a83..6508f0a578 100644
--- a/frontend/src/lib/uPlotV2/config/UPlotSeriesBuilder.ts
+++ b/frontend/src/lib/uPlotV2/config/UPlotSeriesBuilder.ts
@@ -1,8 +1,10 @@
+import { PANEL_TYPES } from 'constants/queryBuilder';
import { themeColors } from 'constants/theme';
import { generateColor } from 'lib/uPlotLib/utils/generateColor';
import uPlot, { Series } from 'uplot';
import {
+ BarAlignment,
ConfigBuilder,
DrawStyle,
LineInterpolation,
@@ -43,18 +45,13 @@ export class UPlotSeriesBuilder extends ConfigBuilder<SeriesProps, Series> {
}
private buildLineConfig({
- lineColor,
- lineWidth,
- lineStyle,
- lineCap,
+ resolvedLineColor,
}: {
- lineColor: string;
- lineWidth?: number;
- lineStyle?: LineStyle;
- lineCap?: Series.Cap;
+ resolvedLineColor: string;
}): Partial<Series> {
+ const { lineWidth, lineStyle, lineCap } = this.props;
const lineConfig: Partial<Series> = {
- stroke: lineColor,
+ stroke: resolvedLineColor,
width: lineWidth ?? 2,
};
@@ -65,21 +62,26 @@ export class UPlotSeriesBuilder extends ConfigBuilder<SeriesProps, Series> {
if (lineCap) {
lineConfig.cap = lineCap;
}
+
+ if (this.props.panelType === PANEL_TYPES.BAR) {
+ lineConfig.fill = resolvedLineColor;
+ }
+
return lineConfig;
}
/**
* Build path configuration
*/
- private buildPathConfig({
- pathBuilder,
- drawStyle,
- lineInterpolation,
- }: {
- pathBuilder?: Series.PathBuilder | null;
- drawStyle: DrawStyle;
- lineInterpolation?: LineInterpolation;
- }): Partial<Series> {
+ private buildPathConfig(): Partial<Series> {
+ const {
+ pathBuilder,
+ drawStyle,
+ lineInterpolation,
+ barAlignment,
+ barMaxWidth,
+ barWidthFactor,
+ } = this.props;
if (pathBuilder) {
return { paths: pathBuilder };
}
@@ -96,7 +98,13 @@ export class UPlotSeriesBuilder extends ConfigBuilder<SeriesProps, Series> {
idx0: number,
idx1: number,
): Series.Paths | null => {
- const pathsBuilder = getPathBuilder(drawStyle, lineInterpolation);
+ const pathsBuilder = getPathBuilder({
+ drawStyle,
+ lineInterpolation,
+ barAlignment,
+ barMaxWidth,
+ barWidthFactor,
+ });
return pathsBuilder(self, seriesIdx, idx0, idx1);
},
@@ -110,25 +118,21 @@ export class UPlotSeriesBuilder extends ConfigBuilder<SeriesProps, Series> {
* Build points configuration
*/
private buildPointsConfig({
- lineColor,
- lineWidth,
- pointSize,
- pointsBuilder,
- pointsFilter,
- drawStyle,
- showPoints,
+ resolvedLineColor,
}: {
- lineColor: string;
- lineWidth?: number;
- pointSize?: number;
- pointsBuilder: Series.Points.Show | null;
- pointsFilter: Series.Points.Filter | null;
- drawStyle: DrawStyle;
- showPoints?: VisibilityMode;
+ resolvedLineColor: string;
}): Partial<Series.Points> {
+ const {
+ lineWidth,
+ pointSize,
+ pointsBuilder,
+ pointsFilter,
+ drawStyle,
+ showPoints,
+ } = this.props;
const pointsConfig: Partial<Series.Points> = {
- stroke: lineColor,
- fill: lineColor,
+ stroke: resolvedLineColor,
+ fill: resolvedLineColor,
size: !pointSize || pointSize < (lineWidth ?? 2) ? undefined : pointSize,
filter: pointsFilter || undefined,
};
@@ -162,44 +166,16 @@ export class UPlotSeriesBuilder extends ConfigBuilder<SeriesProps, Series> {
}
getConfig(): Series {
- const {
- drawStyle,
- pathBuilder,
- pointsBuilder,
- pointsFilter,
- lineInterpolation,
- lineWidth,
- lineStyle,
- lineCap,
- showPoints,
- pointSize,
- scaleKey,
- label,
- spanGaps,
- show = true,
- } = this.props;
+ const { scaleKey, label, spanGaps, show = true } = this.props;
- const lineColor = this.getLineColor();
+ const resolvedLineColor = this.getLineColor();
const lineConfig = this.buildLineConfig({
- lineColor,
- lineWidth,
- lineStyle,
- lineCap,
- });
- const pathConfig = this.buildPathConfig({
- pathBuilder,
- drawStyle,
- lineInterpolation,
+ resolvedLineColor,
});
+ const pathConfig = this.buildPathConfig();
const pointsConfig = this.buildPointsConfig({
- lineColor,
- lineWidth,
- pointSize,
- pointsBuilder: pointsBuilder ?? null,
- pointsFilter: pointsFilter ?? null,
- drawStyle,
- showPoints,
+ resolvedLineColor,
});
return {
@@ -227,15 +203,36 @@ interface PathBuilders {
/**
* Get path builder based on draw style and interpolation
*/
-function getPathBuilder(
- style: DrawStyle,
- lineInterpolation?: LineInterpolation,
-): Series.PathBuilder {
+function getPathBuilder({
+ drawStyle,
+ lineInterpolation,
+ barAlignment = BarAlignment.Center,
+ barWidthFactor = 0.6,
+ barMaxWidth = 200,
+}: {
+ drawStyle: DrawStyle;
+ lineInterpolation?: LineInterpolation;
+ barAlignment?: BarAlignment;
+ barMaxWidth?: number;
+ barWidthFactor?: number;
+}): Series.PathBuilder {
if (!builders) {
throw new Error('Required uPlot path builders are not available');
}
- if (style === DrawStyle.Line) {
+ if (drawStyle === DrawStyle.Bar) {
+ const pathBuilders = uPlot.paths;
+ const barsConfigKey = `bars|${barAlignment}|${barWidthFactor}|${barMaxWidth}`;
+ if (!builders[barsConfigKey] && pathBuilders.bars) {
+ builders[barsConfigKey] = pathBuilders.bars({
+ size: [barWidthFactor, barMaxWidth],
+ align: barAlignment,
+ });
+ }
+ return builders[barsConfigKey];
+ }
+
+ if (drawStyle === DrawStyle.Line) {
if (lineInterpolation === LineInterpolation.StepBefore) {
return builders.stepBefore;
}
diff --git a/frontend/src/lib/uPlotV2/config/types.ts b/frontend/src/lib/uPlotV2/config/types.ts
index 7eef681a02..2706803913 100644
--- a/frontend/src/lib/uPlotV2/config/types.ts
+++ b/frontend/src/lib/uPlotV2/config/types.ts
@@ -126,7 +126,45 @@ export enum VisibilityMode {
Never = 'never',
}
-export interface SeriesProps {
+/**
+ * Props for configuring lines
+ */
+export interface LineConfig {
+ lineColor?: string;
+ lineInterpolation?: LineInterpolation;
+ lineStyle?: LineStyle;
+ lineWidth?: number;
+ lineCap?: Series.Cap;
+}
+
+/**
+ * Alignment of bars
+ */
+export enum BarAlignment {
+ After = 1,
+ Before = -1,
+ Center = 0,
+}
+
+/**
+ * Props for configuring bars
+ */
+export interface BarConfig {
+ barAlignment?: BarAlignment;
+ barMaxWidth?: number;
+ barWidthFactor?: number;
+}
+
+/**
+ * Props for configuring points
+ */
+export interface PointsConfig {
+ pointColor?: string;
+ pointSize?: number;
+ showPoints?: VisibilityMode;
+}
+
+export interface SeriesProps extends LineConfig, PointsConfig, BarConfig {
scaleKey: string;
label?: string;
panelType: PANEL_TYPES;
@@ -137,20 +175,7 @@ export interface SeriesProps {
pointsBuilder?: Series.Points.Show;
show?: boolean;
spanGaps?: boolean;
-
isDarkMode?: boolean;
-
- // Line config
- lineColor?: string;
- lineInterpolation?: LineInterpolation;
- lineStyle?: LineStyle;
- lineWidth?: number;
- lineCap?: Series.Cap;
-
- // Points config
- pointColor?: string;
- pointSize?: number;
- showPoints?: VisibilityMode;
}
export interface LegendItem {