TimeSeries: Don't re-init chart with bars style on data updates (#83355)

This commit is contained in:
Leon Sorokin 2024-02-26 19:41:39 -06:00 committed by GitHub
parent 2c3596854f
commit 93fef224ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 24 deletions

View File

@ -61,6 +61,11 @@ export interface JoinOptions {
*/
keepDisplayNames?: boolean;
/**
* @internal -- Optionally specify how to treat null values
*/
nullMode?: (field: Field) => JoinNullMode;
/**
* @internal -- Optionally specify a join mode (outer or inner)
*/
@ -95,6 +100,9 @@ export function joinDataFrames(options: JoinOptions): DataFrame | undefined {
return;
}
const nullMode =
options.nullMode ?? ((field: Field) => (field.config.custom?.spanNulls === true ? NULL_REMOVE : NULL_EXPAND));
if (options.frames.length === 1) {
let frame = options.frames[0];
let frameCopy = frame;
@ -186,8 +194,7 @@ export function joinDataFrames(options: JoinOptions): DataFrame | undefined {
}
// Support the standard graph span nulls field config
let spanNulls = field.config.custom?.spanNulls;
nullModesFrame.push(spanNulls === true ? NULL_REMOVE : spanNulls === -1 ? NULL_RETAIN : NULL_EXPAND);
nullModesFrame.push(nullMode(field));
let labels = field.labels ?? {};
let name = field.name;
@ -374,9 +381,9 @@ export type AlignedData =
| [xValues: number[] | TypedArray, ...yValues: Array<Array<number | null | undefined> | TypedArray>];
// nullModes
const NULL_REMOVE = 0; // nulls are converted to undefined (e.g. for spanGaps: true)
const NULL_RETAIN = 1; // nulls are retained, with alignment artifacts set to undefined (default)
const NULL_EXPAND = 2; // nulls are expanded to include any adjacent alignment artifacts
export const NULL_REMOVE = 0; // nulls are converted to undefined (e.g. for spanGaps: true)
export const NULL_RETAIN = 1; // nulls are retained, with alignment artifacts set to undefined (default)
export const NULL_EXPAND = 2; // nulls are expanded to include any adjacent alignment artifacts
type JoinNullMode = number; // NULL_IGNORE | NULL_RETAIN | NULL_EXPAND;

View File

@ -353,7 +353,6 @@ describe('GraphNG utils', () => {
"config": {
"custom": {
"drawStyle": "bars",
"spanNulls": -1,
},
},
"labels": {
@ -386,7 +385,6 @@ describe('GraphNG utils', () => {
"config": {
"custom": {
"drawStyle": "bars",
"spanNulls": -1,
},
},
"labels": {

View File

@ -1,4 +1,5 @@
import { DataFrame, Field, FieldType, outerJoinDataFrames, TimeRange } from '@grafana/data';
import { NULL_EXPAND, NULL_REMOVE, NULL_RETAIN } from '@grafana/data/src/transformations/transformers/joinDataFrames';
import { applyNullInsertThreshold } from '@grafana/data/src/transformations/transformers/nulls/nullInsertThreshold';
import { nullToUndefThreshold } from '@grafana/data/src/transformations/transformers/nulls/nullToUndefThreshold';
import { GraphDrawStyle } from '@grafana/schema';
@ -68,23 +69,10 @@ export function preparePlotFrame(frames: DataFrame[], dimFields: XYFieldMatchers
}
});
let numBarSeries = 0;
frames.forEach((frame) => {
frame.fields.forEach((f) => {
if (isVisibleBarField(f)) {
// prevent minesweeper-expansion of nulls (gaps) when joining bars
// since bar width is determined from the minimum distance between non-undefined values
// (this strategy will still retain any original pre-join nulls, though)
f.config.custom = {
...f.config.custom,
spanNulls: -1,
};
numBarSeries++;
}
});
});
let numBarSeries = frames.reduce(
(acc, frame) => acc + frame.fields.reduce((acc, field) => acc + (isVisibleBarField(field) ? 1 : 0), 0),
0
);
// to make bar widths of all series uniform (equal to narrowest bar series), find smallest distance between x points
let minXDelta = Infinity;
@ -115,6 +103,12 @@ export function preparePlotFrame(frames: DataFrame[], dimFields: XYFieldMatchers
// https://github.com/grafana/grafana/pull/31121
// https://github.com/grafana/grafana/pull/71806
keepDisplayNames: true,
// prevent minesweeper-expansion of nulls (gaps) when joining bars
// since bar width is determined from the minimum distance between non-undefined values
// (this strategy will still retain any original pre-join nulls, though)
nullMode: (field) =>
isVisibleBarField(field) ? NULL_RETAIN : field.config.custom?.spanNulls === true ? NULL_REMOVE : NULL_EXPAND,
});
if (alignedFrame) {