flotPairs.ts 944 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Types
  2. import { NullValueMode, GraphSeriesValue, Field } from '@grafana/data';
  3. export interface FlotPairsOptions {
  4. xField: Field;
  5. yField: Field;
  6. nullValueMode?: NullValueMode;
  7. }
  8. export function getFlotPairs({ xField, yField, nullValueMode }: FlotPairsOptions): GraphSeriesValue[][] {
  9. const vX = xField.values;
  10. const vY = yField.values;
  11. const length = vX.length;
  12. if (vY.length !== length) {
  13. throw new Error('Unexpected field length');
  14. }
  15. const ignoreNulls = nullValueMode === NullValueMode.Ignore;
  16. const nullAsZero = nullValueMode === NullValueMode.AsZero;
  17. const pairs: any[][] = [];
  18. for (let i = 0; i < length; i++) {
  19. const x = vX.get(i);
  20. let y = vY.get(i);
  21. if (y === null) {
  22. if (ignoreNulls) {
  23. continue;
  24. }
  25. if (nullAsZero) {
  26. y = 0;
  27. }
  28. }
  29. // X must be a value
  30. if (x === null) {
  31. continue;
  32. }
  33. pairs.push([x, y]);
  34. }
  35. return pairs;
  36. }