mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
flot pairs
This commit is contained in:
parent
f69f0c2c31
commit
2f8dd898b0
24
packages/grafana-ui/src/utils/floatPairs.test.ts
Normal file
24
packages/grafana-ui/src/utils/floatPairs.test.ts
Normal file
@ -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');
|
||||||
|
});
|
||||||
|
});
|
38
packages/grafana-ui/src/utils/flotPairs.ts
Normal file
38
packages/grafana-ui/src/utils/flotPairs.ts
Normal file
@ -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;
|
||||||
|
}
|
@ -5,6 +5,7 @@ import { colors } from './colors';
|
|||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { TimeSeries, TimeSeriesVMs, NullValueMode, TimeSeriesValue } from '../types';
|
import { TimeSeries, TimeSeriesVMs, NullValueMode, TimeSeriesValue } from '../types';
|
||||||
|
import { getFlotPairs } from './flotPairs';
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
timeSeries: TimeSeries[];
|
timeSeries: TimeSeries[];
|
||||||
@ -15,7 +16,14 @@ export function processTimeSeries({ timeSeries, nullValueMode }: Options): TimeS
|
|||||||
const vmSeries = timeSeries.map((item, index) => {
|
const vmSeries = timeSeries.map((item, index) => {
|
||||||
const colorIndex = index % colors.length;
|
const colorIndex = index % colors.length;
|
||||||
const label = item.target;
|
const label = item.target;
|
||||||
const result = [];
|
|
||||||
|
// Use external calculator just to make sure it works :)
|
||||||
|
const result = getFlotPairs({
|
||||||
|
rows: item.datapoints,
|
||||||
|
xIndex: 1,
|
||||||
|
yIndex: 0,
|
||||||
|
nullValueMode,
|
||||||
|
});
|
||||||
|
|
||||||
// stat defaults
|
// stat defaults
|
||||||
let total = 0;
|
let total = 0;
|
||||||
@ -118,8 +126,6 @@ export function processTimeSeries({ timeSeries, nullValueMode }: Options): TimeS
|
|||||||
allIsZero = false;
|
allIsZero = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push([currentTime, currentValue]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (max === -Number.MAX_VALUE) {
|
if (max === -Number.MAX_VALUE) {
|
||||||
|
Loading…
Reference in New Issue
Block a user