diff --git a/packages/grafana-ui/src/utils/floatPairs.test.ts b/packages/grafana-ui/src/utils/floatPairs.test.ts new file mode 100644 index 00000000000..6e95eb7df78 --- /dev/null +++ b/packages/grafana-ui/src/utils/floatPairs.test.ts @@ -0,0 +1,24 @@ +import { getFlotPairs } from './flotPairs'; + +describe('getFlotPairs', () => { + const table = { + rows: [[1, 100, 'a'], [2, 200, 'b'], [3, 300, 'c']], + }; + it('should get X and y', () => { + const pairs = getFlotPairs({ rows: table.rows, xIndex: 0, yIndex: 1 }); + + expect(pairs.length).toEqual(3); + expect(pairs[0].length).toEqual(2); + expect(pairs[0][0]).toEqual(1); + expect(pairs[0][1]).toEqual(100); + }); + + it('should work with strings', () => { + const pairs = getFlotPairs({ rows: table.rows, xIndex: 0, yIndex: 2 }); + + expect(pairs.length).toEqual(3); + expect(pairs[0].length).toEqual(2); + expect(pairs[0][0]).toEqual(1); + expect(pairs[0][1]).toEqual('a'); + }); +}); diff --git a/packages/grafana-ui/src/utils/flotPairs.ts b/packages/grafana-ui/src/utils/flotPairs.ts new file mode 100644 index 00000000000..2583ee3340d --- /dev/null +++ b/packages/grafana-ui/src/utils/flotPairs.ts @@ -0,0 +1,38 @@ +// Types +import { NullValueMode } from '../types/index'; + +export interface FloatPairsOptions { + rows: any[][]; + xIndex: number; + yIndex: number; + nullValueMode?: NullValueMode; +} + +export function getFlotPairs({ rows, xIndex, yIndex, nullValueMode }: FloatPairsOptions): any[][] { + const ignoreNulls = nullValueMode === NullValueMode.Ignore; + const nullAsZero = nullValueMode === NullValueMode.AsZero; + + const pairs: any[][] = []; + + for (let i = 0; i < rows.length; i++) { + const x = rows[i][xIndex]; + let y = rows[i][yIndex]; + + if (y === null) { + if (ignoreNulls) { + continue; + } + if (nullAsZero) { + y = 0; + } + } + + // X must be a value + if (x === null) { + continue; + } + + pairs.push([x, y]); + } + return pairs; +} diff --git a/packages/grafana-ui/src/utils/processTimeSeries.ts b/packages/grafana-ui/src/utils/processTimeSeries.ts index 9a6aeac6e69..2574ae84ac4 100644 --- a/packages/grafana-ui/src/utils/processTimeSeries.ts +++ b/packages/grafana-ui/src/utils/processTimeSeries.ts @@ -4,6 +4,7 @@ import isNumber from 'lodash/isNumber'; import { colors } from './colors'; // Types +import { getFlotPairs } from './flotPairs'; import { TimeSeriesVMs, NullValueMode, TimeSeriesValue, TableData } from '../types'; interface Options { @@ -34,7 +35,14 @@ export function processTimeSeries({ data, xColumn, yColumn, nullValueMode }: Opt const colorIndex = index % colors.length; const label = item.columns[yColumn].text; - const result = []; + + // Use external calculator just to make sure it works :) + const result = getFlotPairs({ + rows: item.rows, + xIndex: xColumn, + yIndex: yColumn, + nullValueMode, + }); // stat defaults let total = 0; @@ -137,8 +145,6 @@ export function processTimeSeries({ data, xColumn, yColumn, nullValueMode }: Opt allIsZero = false; } } - - result.push([currentTime, currentValue]); } if (max === -Number.MAX_VALUE) {