footer.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {
  2. DataFrame,
  3. Field,
  4. FieldType,
  5. formattedValueToString,
  6. getDisplayProcessor,
  7. reduceField,
  8. fieldReducers,
  9. } from '@grafana/data';
  10. import { FooterItem } from '@grafana/ui/src/components/Table/types';
  11. import { config } from 'app/core/config';
  12. import { TableFooterCalc } from './models.gen';
  13. export function getFooterCells(frame: DataFrame, options?: TableFooterCalc): FooterItem[] {
  14. return frame.fields.map((field, i) => {
  15. if (field.type !== FieldType.number) {
  16. // show the reducer in the first column
  17. if (i === 0 && options && options.reducer.length > 0) {
  18. const reducer = fieldReducers.get(options.reducer[0]);
  19. return reducer.name;
  20. }
  21. return undefined;
  22. }
  23. if (options?.fields && options.fields.length > 0) {
  24. const f = options.fields.find((f) => f === field.name);
  25. if (f) {
  26. return getFormattedValue(field, options.reducer);
  27. }
  28. return undefined;
  29. }
  30. return getFormattedValue(field, options?.reducer || []);
  31. });
  32. }
  33. function getFormattedValue(field: Field, reducer: string[]) {
  34. const fmt = field.display ?? getDisplayProcessor({ field, theme: config.theme2 });
  35. const calc = reducer[0];
  36. const v = reduceField({ field, reducers: reducer })[calc];
  37. return formattedValueToString(fmt(v));
  38. }