Skip to content

Commit 4bfd17e

Browse files
authored
Simplify offset and width calculation for bar chart traces. (#22792)
1 parent 86a9bdf commit 4bfd17e

File tree

5 files changed

+27
-206
lines changed

5 files changed

+27
-206
lines changed

graylog2-web-interface/src/views/components/visualizations/bar/BarVisualization.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ const BarVisualization = makeVisualization(
108108

109109
const getBarChartDataSettingsWithCustomUnits = useBarChartDataSettingsWithCustomUnits({
110110
config,
111-
effectiveTimerange,
112111
barmode,
113112
});
114113

graylog2-web-interface/src/views/components/visualizations/hooks/useBarChartDataSettingsWithCustomUnits.test.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,14 @@ describe('useBarChartDataSettingsWithCustomUnits', () => {
107107
};
108108
asMock(chartLayoutGenerators.generateMappersForYAxis).mockReturnValue(mappers);
109109

110-
asMock(chartLayoutGenerators.getBarChartTraceOffsetSettings).mockReturnValue({
111-
offsetgroup: 1,
112-
width: 0.25,
113-
offset: -0.375,
114-
});
110+
asMock(chartLayoutGenerators.getBarChartTraceOffsetGroup).mockReturnValue(1);
115111
});
116112

117113
it('Runs all related functions and return combined result from them', async () => {
118114
const { result } = renderHook(() =>
119115
useBarChartDataSettingsWithCustomUnits({
120116
config: testConfig,
121117
barmode: 'group',
122-
effectiveTimerange: {
123-
from: '2024-08-11T14:56:10.000Z',
124-
to: '2024-08-12T15:01:10.000Z',
125-
type: 'absolute',
126-
},
127118
}),
128119
);
129120

@@ -141,34 +132,12 @@ describe('useBarChartDataSettingsWithCustomUnits', () => {
141132
});
142133
});
143134

144-
expect(chartLayoutGenerators.generateMappersForYAxis).toHaveBeenCalledWith({
145-
series: testConfig.series,
146-
units,
147-
});
148-
149135
expect(useChartDataSettingsWithCustomUnits).toHaveBeenCalledWith({ config: testConfig });
150-
expect(getFieldNameFromTrace).toHaveBeenCalledWith({ series: testConfig.series, fullPath: 'Name1' });
151-
152-
expect(chartLayoutGenerators.getBarChartTraceOffsetSettings).toHaveBeenCalledWith('group', {
153-
yaxis: 'y1',
154-
totalAxis: 4,
155-
axisNumber: 1,
156-
traceIndex: 1,
157-
totalTraces: 4,
158-
effectiveTimerange: {
159-
from: '2024-08-11T14:56:10.000Z',
160-
to: '2024-08-12T15:01:10.000Z',
161-
type: 'absolute',
162-
},
163-
isTimeline: false,
164-
xAxisItemsLength: 10,
165-
});
136+
expect(chartLayoutGenerators.getBarChartTraceOffsetGroup).toHaveBeenCalledWith('group', 'y1', 1);
166137

167138
expect(barChartDataSettingsWithCustomUnits).toEqual({
168139
fullPath: 'Name1',
169-
offset: -0.375,
170140
offsetgroup: 1,
171-
width: 0.25,
172141
y: [1, 2, 3],
173142
yaxis: 'y1',
174143
});

graylog2-web-interface/src/views/components/visualizations/hooks/useBarChartDataSettingsWithCustomUnits.ts

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,30 @@
1414
* along with this program. If not, see
1515
* <http://www.mongodb.com/licensing/server-side-public-license>.
1616
*/
17-
import { useCallback, useMemo } from 'react';
17+
import { useCallback } from 'react';
1818

1919
import useFeature from 'hooks/useFeature';
20-
import useWidgetUnits from 'views/components/visualizations/hooks/useWidgetUnits';
2120
import type AggregationWidgetConfig from 'views/logic/aggregationbuilder/AggregationWidgetConfig';
22-
import {
23-
generateMappersForYAxis,
24-
getBarChartTraceOffsetSettings,
25-
} from 'views/components/visualizations/utils/chartLayoutGenerators';
21+
import { getBarChartTraceOffsetGroup } from 'views/components/visualizations/utils/chartLayoutGenerators';
2622
import type { BarMode } from 'views/logic/aggregationbuilder/visualizations/BarVisualizationConfig';
27-
import type { AbsoluteTimeRange } from 'views/logic/queries/Query';
28-
import { NO_FIELD_NAME_SERIES, UNIT_FEATURE_FLAG } from 'views/components/visualizations/Constants';
29-
import getFieldNameFromTrace from 'views/components/visualizations/utils/getFieldNameFromTrace';
23+
import { UNIT_FEATURE_FLAG } from 'views/components/visualizations/Constants';
3024
import type { ChartDefinition } from 'views/components/visualizations/ChartData';
3125
import useChartDataSettingsWithCustomUnits from 'views/components/visualizations/hooks/useChartDataSettingsWithCustomUnits';
3226

3327
const useBarChartDataSettingsWithCustomUnits = ({
3428
config,
3529
barmode,
36-
effectiveTimerange,
3730
}: {
3831
config: AggregationWidgetConfig;
3932
barmode?: BarMode;
40-
effectiveTimerange: AbsoluteTimeRange;
4133
}) => {
4234
const unitFeatureEnabled = useFeature(UNIT_FEATURE_FLAG);
43-
const widgetUnits = useWidgetUnits(config);
44-
const { fieldNameToAxisCountMapper, unitTypeMapper } = useMemo(
45-
() => generateMappersForYAxis({ series: config.series, units: widgetUnits }),
46-
[config.series, widgetUnits],
47-
);
4835
const getChartDataSettingsWithCustomUnits = useChartDataSettingsWithCustomUnits({ config });
4936

5037
return useCallback(
5138
({
5239
values,
5340
idx,
54-
total,
55-
xAxisItemsLength,
5641
fullPath,
5742
name,
5843
}: {
@@ -66,44 +51,23 @@ const useBarChartDataSettingsWithCustomUnits = ({
6651
}): Partial<ChartDefinition> => {
6752
if (!unitFeatureEnabled) return {};
6853

69-
const fieldNameKey = getFieldNameFromTrace({ fullPath, series: config.series }) ?? NO_FIELD_NAME_SERIES;
7054
const {
7155
y: convertedValues,
7256
yaxis,
7357
...hoverTemplateSettings
7458
} = getChartDataSettingsWithCustomUnits({ name, fullPath, values });
75-
const axisNumber = fieldNameToAxisCountMapper?.[fieldNameKey];
76-
const totalAxis = Object.keys(unitTypeMapper).length;
7759

78-
const offsetSettings = getBarChartTraceOffsetSettings(barmode, {
79-
yaxis,
80-
totalAxis,
81-
axisNumber,
82-
traceIndex: idx,
83-
totalTraces: total,
84-
effectiveTimerange,
85-
isTimeline: config.isTimeline,
86-
xAxisItemsLength: xAxisItemsLength,
87-
});
60+
const offsetgroup = getBarChartTraceOffsetGroup(barmode, yaxis, idx);
8861

8962
return {
9063
yaxis,
9164
y: convertedValues,
9265
fullPath,
66+
offsetgroup,
9367
...hoverTemplateSettings,
94-
...offsetSettings,
9568
};
9669
},
97-
[
98-
barmode,
99-
config.isTimeline,
100-
config.series,
101-
effectiveTimerange,
102-
fieldNameToAxisCountMapper,
103-
getChartDataSettingsWithCustomUnits,
104-
unitFeatureEnabled,
105-
unitTypeMapper,
106-
],
70+
[barmode, getChartDataSettingsWithCustomUnits, unitFeatureEnabled],
10771
);
10872
};
10973

graylog2-web-interface/src/views/components/visualizations/utils/__tests__/chartLayoutGenerators.test.ts

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
* <http://www.mongodb.com/licensing/server-side-public-license>.
1616
*/
1717
import AggregationWidgetConfig from 'views/logic/aggregationbuilder/AggregationWidgetConfig';
18-
import type {
19-
AdditionalSettings,
20-
GenerateLayoutsParams,
21-
} from 'views/components/visualizations/utils/chartLayoutGenerators';
18+
import type { GenerateLayoutsParams } from 'views/components/visualizations/utils/chartLayoutGenerators';
2219
import {
23-
getBarChartTraceOffsetSettings,
20+
getBarChartTraceOffsetGroup,
2421
generateMappersForYAxis,
2522
generateLayouts,
2623
getHoverTemplateSettings,
@@ -41,77 +38,28 @@ import SeriesConfig from 'views/logic/aggregationbuilder/SeriesConfig';
4138

4239
describe('Chart Layout Generators', () => {
4340
describe('getBarChartTraceOffsetSettings', () => {
44-
const defaultProps: AdditionalSettings = {
45-
yaxis: 'y3',
46-
totalAxis: 4,
47-
axisNumber: 1,
48-
traceIndex: 2,
49-
totalTraces: 4,
50-
effectiveTimerange: {
51-
from: '2024-08-11T14:56:10.000Z',
52-
to: '2024-08-12T15:01:10.000Z',
53-
type: 'absolute',
54-
},
55-
isTimeline: false,
56-
xAxisItemsLength: 10,
57-
};
58-
5941
it('return correct offset for barmode, mode group, non timeline', () => {
60-
const result = getBarChartTraceOffsetSettings('group', defaultProps);
42+
const result = getBarChartTraceOffsetGroup('group', 'y3', 2);
6143

62-
expect(result).toEqual({
63-
offsetgroup: 2,
64-
width: 0.25,
65-
offset: 0.125,
66-
});
44+
expect(result).toEqual(2);
6745
});
6846

6947
it('return correct offset for barmode, mode group, timeline', () => {
70-
const result = getBarChartTraceOffsetSettings('group', {
71-
...defaultProps,
72-
isTimeline: true,
73-
});
48+
const result = getBarChartTraceOffsetGroup('group', 'y3', 2);
7449

75-
expect(result).toEqual({
76-
offsetgroup: 2,
77-
width: 2167500,
78-
offset: 1083750,
79-
});
50+
expect(result).toEqual(2);
8051
});
8152

8253
it('return correct offset for barmode, mode stack, non timeline', () => {
83-
const result = getBarChartTraceOffsetSettings('stack', {
84-
...defaultProps,
85-
yaxis: 'y2',
86-
totalAxis: 2,
87-
axisNumber: 2,
88-
traceIndex: 1,
89-
totalTraces: 2,
90-
});
54+
const result = getBarChartTraceOffsetGroup('stack', 'y2', 1);
9155

92-
expect(result).toEqual({
93-
offsetgroup: 'y2',
94-
width: 0.5,
95-
offset: 0.25,
96-
});
56+
expect(result).toEqual('y2');
9757
});
9858

9959
it('return correct offset for barmode, mode stack, timeline', () => {
100-
const result = getBarChartTraceOffsetSettings('stack', {
101-
...defaultProps,
102-
isTimeline: true,
103-
yaxis: 'y2',
104-
totalAxis: 2,
105-
axisNumber: 2,
106-
traceIndex: 1,
107-
totalTraces: 2,
108-
});
60+
const result = getBarChartTraceOffsetGroup('stack', 'y2', 1);
10961

110-
expect(result).toEqual({
111-
offsetgroup: 'y2',
112-
width: 4335000,
113-
offset: 2167500,
114-
});
62+
expect(result).toEqual('y2');
11563
});
11664
});
11765

graylog2-web-interface/src/views/components/visualizations/utils/chartLayoutGenerators.ts

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import zipWith from 'lodash/zipWith';
1919
import sum from 'lodash/sum';
2020
import flattenDeep from 'lodash/flattenDeep';
21-
import moment from 'moment';
2221
import type { DefaultTheme } from 'styled-components';
2322

2423
import type { FieldUnitType } from 'views/types';
@@ -34,7 +33,6 @@ import {
3433
} from 'views/components/visualizations/utils/unitConverters';
3534
import type { ChartDefinition } from 'views/components/visualizations/ChartData';
3635
import type FieldUnit from 'views/logic/aggregationbuilder/FieldUnit';
37-
import type { AbsoluteTimeRange } from 'views/logic/queries/Query';
3836
import {
3937
DEFAULT_AXIS_KEY,
4038
TIME_AXIS_LABELS_QUANTITY,
@@ -158,79 +156,22 @@ const getUnitLayoutWithData = (
158156
type SeriesName = string;
159157
type AxisName = string;
160158

161-
const getWidth = (total: number, offsetMultiplier: number) => (total <= 1 ? undefined : offsetMultiplier / total);
162-
163-
const getOffset = (offsetNumber: number, totalOffsets: number, offsetMultiplier: number) => {
164-
const width = getWidth(totalOffsets, offsetMultiplier);
165-
if (!width) return undefined;
166-
const firstOffset = (width / 2) * (1 - totalOffsets);
167-
168-
return firstOffset + width * (offsetNumber - 1);
169-
};
170-
171-
export type AdditionalSettings = {
172-
yaxis: string /** y axis name y, y2 etc */;
173-
totalAxis: number /** total number of y-axis */;
174-
axisNumber: number /** number of y-axis (1...N) */;
175-
totalTraces: number /** total number of traces for each x value (in fact total amount of series) */;
176-
traceIndex: number /** number (0...N) */;
177-
effectiveTimerange?: AbsoluteTimeRange;
178-
isTimeline?: boolean;
179-
xAxisItemsLength?: number /** total amount of x values */;
180-
};
181-
182-
type BarChartTraceOffsetSettings = {
183-
/** Needs to group traces. In case if barmode: 'stack' | 'relative' | 'overlay'
184-
* we are grouping by y-axis to join traces into same trace on chart */
185-
offsetgroup: number | string;
186-
/** In case if barmode: 'stack' | 'relative' | 'overlay'we are divide whole possible
187-
* width for traces by total axis. In other case we divide by total traces */
188-
width: number;
189-
/** alignment is relative to the trace center */
190-
offset: number;
191-
};
192-
193-
export const getBarChartTraceOffsetSettings = (
159+
/** Needs to group traces. In case if barmode: 'stack' | 'relative' | 'overlay'
160+
* we are grouping by y-axis to join traces into same trace on chart */
161+
export const getBarChartTraceOffsetGroup = (
194162
barmode: BarMode,
195-
{
196-
yaxis,
197-
totalAxis,
198-
axisNumber,
199-
traceIndex,
200-
totalTraces,
201-
effectiveTimerange,
202-
isTimeline,
203-
xAxisItemsLength,
204-
}: AdditionalSettings,
205-
): BarChartTraceOffsetSettings | {} => {
206-
const offsetMultiplier =
207-
xAxisItemsLength && isTimeline && effectiveTimerange
208-
? moment(effectiveTimerange.to).diff(effectiveTimerange.from) / xAxisItemsLength
209-
: 1;
210-
163+
yaxis: string,
164+
traceIndex: number,
165+
): string | number | undefined => {
211166
if (barmode === 'stack' || barmode === 'relative' || barmode === 'overlay') {
212-
const width = getWidth(totalAxis, offsetMultiplier);
213-
const offset = getOffset(axisNumber, totalAxis, offsetMultiplier);
214-
215-
return {
216-
offsetgroup: yaxis,
217-
width,
218-
offset,
219-
};
167+
return yaxis;
220168
}
221169

222170
if (barmode === 'group') {
223-
const width = getWidth(totalTraces, offsetMultiplier);
224-
const offset = getOffset(traceIndex + 1, totalTraces, offsetMultiplier);
225-
226-
return {
227-
offsetgroup: traceIndex,
228-
width,
229-
offset,
230-
};
171+
return traceIndex;
231172
}
232173

233-
return {};
174+
return undefined;
234175
};
235176

236177
export type UnitTypeMapper = {} | Record<FieldUnitType, { axisKeyName: string; axisCount: number }>;

0 commit comments

Comments
 (0)