Merge pull request #1071 from buster-so/big-nate-bus-1877-drop-deliminator-if-one-is-a-date

common handler for join characters
This commit is contained in:
Nate Kelley 2025-09-23 12:05:50 -06:00 committed by GitHub
commit 0d393792f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 22 additions and 21 deletions

View File

@ -2,7 +2,7 @@ import { type ColumnLabelFormat, DEFAULT_COLUMN_LABEL_FORMAT } from '@buster/ser
import type { BarElement } from 'chart.js';
import type { Context } from 'chartjs-plugin-datalabels';
import type { Options } from 'chartjs-plugin-datalabels/types/options';
import { JOIN_CHARACTER } from '@/lib/axisFormatter';
import { JOIN_CHARACTER, JOIN_CHARACTER_DATE } from '@/lib/axisFormatter';
import { formatLabel } from '@/lib/columnFormatter';
import type { BusterChartProps } from '../../../BusterChart.types';
import type { DatasetOption } from '../../../chartHooks';
@ -351,9 +351,15 @@ const getFormattedValueAndSetBarDataLabels = (
export const barSeriesBuilder_labels = ({
datasetOptions,
columnLabelFormats,
}: LabelBuilderProps) => {
}: Pick<LabelBuilderProps, 'datasetOptions' | 'columnLabelFormats'>) => {
const ticksKey = datasetOptions.ticksKey;
const containsADateStyle = datasetOptions.ticksKey.some((tick) => {
const selectedColumnLabelFormat = columnLabelFormats[tick.key];
return selectedColumnLabelFormat?.style === 'date';
});
const selectedJoinCharacter = containsADateStyle ? JOIN_CHARACTER_DATE : JOIN_CHARACTER;
const labels = datasetOptions.ticks.flatMap((item) => {
return item
.map<string>((item, index) => {
@ -361,7 +367,7 @@ export const barSeriesBuilder_labels = ({
const columnLabelFormat = columnLabelFormats[key];
return formatLabel(item, columnLabelFormat);
})
.join(JOIN_CHARACTER);
.join(selectedJoinCharacter);
});
return labels;

View File

@ -86,6 +86,4 @@ const renderBuilder: Record<
dot: dotSeriesBuilder,
};
export const comboSeriesBuilder_labels = (props: LabelBuilderProps): (string | Date)[] => {
return lineSeriesBuilder_labels(props);
};
export const comboSeriesBuilder_labels = lineSeriesBuilder_labels;

View File

@ -42,6 +42,7 @@ vi.mock('@/lib/columnFormatter', () => ({
vi.mock('@/lib/axisFormatter', () => ({
JOIN_CHARACTER: ' | ',
JOIN_CHARACTER_DATE: ' ',
}));
vi.mock('../../helpers', () => ({
@ -329,9 +330,9 @@ describe('lineSeriesBuilder', () => {
const labels = lineSeriesBuilder_labels(props);
expect(labels).toHaveLength(3);
expect(labels[0]).toBe('formatted-2023-01-01 | formatted-A');
expect(labels[1]).toBe('formatted-2023-01-02 | formatted-B');
expect(labels[2]).toBe('formatted-2023-01-03 | formatted-A');
expect(labels[0]).toBe('formatted-2023-01-01 formatted-A');
expect(labels[1]).toBe('formatted-2023-01-02 formatted-B');
expect(labels[2]).toBe('formatted-2023-01-03 formatted-A');
expect(formatLabel).toHaveBeenCalledWith('2023-01-01', props.columnLabelFormats.date);
expect(formatLabel).toHaveBeenCalledWith('A', props.columnLabelFormats.category);
expect(formatLabel).toHaveBeenCalledWith('B', props.columnLabelFormats.category);
@ -342,9 +343,9 @@ describe('lineSeriesBuilder', () => {
const labels = lineSeriesBuilder_labels(props);
expect(labels).toHaveLength(3);
expect(labels[0]).toBe('formatted-2023-01-01 | formatted-A');
expect(labels[1]).toBe('formatted-2023-01-02 | formatted-B');
expect(labels[2]).toBe('formatted-2023-01-03 | formatted-A');
expect(labels[0]).toBe('formatted-2023-01-01 formatted-A');
expect(labels[1]).toBe('formatted-2023-01-02 formatted-B');
expect(labels[2]).toBe('formatted-2023-01-03 formatted-A');
expect(formatLabel).toHaveBeenCalledWith('A', props.columnLabelFormats.category);
expect(formatLabel).toHaveBeenCalledWith('B', props.columnLabelFormats.category);
expect(formatLabel).toHaveBeenCalledWith('2023-01-01', props.columnLabelFormats.date);

View File

@ -13,6 +13,7 @@ import { formatLabelForDataset } from '../../../commonHelpers';
import type { ChartProps } from '../../core';
import { formatBarAndLineDataLabel } from '../../helpers';
import { defaultLabelOptionConfig } from '../useChartSpecificOptions/labelOptionConfig';
import { barSeriesBuilder_labels } from './barSeriesBuilder';
import { createTrendlineOnSeries } from './createTrendlines';
import type { SeriesBuilderProps } from './interfaces';
import type { LabelBuilderProps } from './useSeriesOptions';
@ -219,7 +220,6 @@ export const lineSeriesBuilder_labels = ({
datasetOptions.ticks[0]?.length === 1 &&
xColumnLabelFormat.columnType === 'date' &&
xColumnLabelFormat.style === 'date';
const ticksKey = datasetOptions.ticksKey;
if (useDateLabels) {
return datasetOptions.ticks.flatMap((item) => {
@ -229,13 +229,8 @@ export const lineSeriesBuilder_labels = ({
});
}
return datasetOptions.ticks.flatMap((item) => {
return item
.map<string>((item, index) => {
const key = ticksKey[index]?.key || '';
const columnLabelFormat = columnLabelFormats[key];
return formatLabel(item, columnLabelFormat);
})
.join(JOIN_CHARACTER);
return barSeriesBuilder_labels({
datasetOptions,
columnLabelFormats,
});
};

View File

@ -1,2 +1,3 @@
export const JOIN_CHARACTER = ' | ';
export const AXIS_TITLE_SEPARATOR = JOIN_CHARACTER;
export const JOIN_CHARACTER_DATE = ' ';