useVizHeight.ts 877 B

123456789101112131415161718192021222324252627
  1. import { PanelData } from '@grafana/data';
  2. import { useTheme2 } from '@grafana/ui';
  3. import { STAT, TIMESERIES } from '../utils/constants';
  4. export function useVizHeight(data: PanelData, pluginId: string, frameIndex: number) {
  5. const theme = useTheme2();
  6. if (pluginId === TIMESERIES || pluginId === STAT || dataIsEmpty(data)) {
  7. return 200;
  8. }
  9. const values = data.series[frameIndex].fields[0].values.length;
  10. const rowHeight = theme.spacing.gridSize * 5;
  11. /*
  12. Calculate how if we can make the table smaller than 200px
  13. for when we only have 1-2 values
  14. The extra rowHeight is to accommodate the header.
  15. */
  16. const tableHeight = values * rowHeight + rowHeight;
  17. return tableHeight >= 200 ? 200 : tableHeight;
  18. }
  19. function dataIsEmpty(data: PanelData) {
  20. return !data || !data.series[0] || !data.series[0].fields[0] || !data.series[0].fields[0].values;
  21. }