StateTimelineTooltip.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React from 'react';
  2. import { DataFrame, FALLBACK_COLOR, getDisplayProcessor, getFieldDisplayName, TimeZone } from '@grafana/data';
  3. import { SeriesTableRow, useTheme2 } from '@grafana/ui';
  4. import { findNextStateIndex, fmtDuration } from './utils';
  5. interface StateTimelineTooltipProps {
  6. data: DataFrame[];
  7. alignedData: DataFrame;
  8. seriesIdx: number;
  9. datapointIdx: number;
  10. timeZone: TimeZone;
  11. }
  12. export const StateTimelineTooltip: React.FC<StateTimelineTooltipProps> = ({
  13. data,
  14. alignedData,
  15. seriesIdx,
  16. datapointIdx,
  17. timeZone,
  18. }) => {
  19. const theme = useTheme2();
  20. const xField = alignedData.fields[0];
  21. const xFieldFmt = xField.display || getDisplayProcessor({ field: xField, timeZone, theme });
  22. const field = alignedData.fields[seriesIdx!];
  23. const dataFrameFieldIndex = field.state?.origin;
  24. const fieldFmt = field.display || getDisplayProcessor({ field, timeZone, theme });
  25. const value = field.values.get(datapointIdx!);
  26. const display = fieldFmt(value);
  27. const fieldDisplayName = dataFrameFieldIndex
  28. ? getFieldDisplayName(
  29. data[dataFrameFieldIndex.frameIndex].fields[dataFrameFieldIndex.fieldIndex],
  30. data[dataFrameFieldIndex.frameIndex],
  31. data
  32. )
  33. : null;
  34. const nextStateIdx = findNextStateIndex(field, datapointIdx!);
  35. let nextStateTs;
  36. if (nextStateIdx) {
  37. nextStateTs = xField.values.get(nextStateIdx!);
  38. }
  39. const stateTs = xField.values.get(datapointIdx!);
  40. let toFragment = null;
  41. let durationFragment = null;
  42. if (nextStateTs) {
  43. const duration = nextStateTs && fmtDuration(nextStateTs - stateTs);
  44. durationFragment = (
  45. <>
  46. <br />
  47. <strong>Duration:</strong> {duration}
  48. </>
  49. );
  50. toFragment = (
  51. <>
  52. {' to'} <strong>{xFieldFmt(xField.values.get(nextStateIdx!)).text}</strong>
  53. </>
  54. );
  55. }
  56. return (
  57. <div style={{ fontSize: theme.typography.bodySmall.fontSize }}>
  58. {fieldDisplayName}
  59. <br />
  60. <SeriesTableRow label={display.text} color={display.color || FALLBACK_COLOR} isActive />
  61. From <strong>{xFieldFmt(xField.values.get(datapointIdx!)).text}</strong>
  62. {toFragment}
  63. {durationFragment}
  64. </div>
  65. );
  66. };
  67. StateTimelineTooltip.displayName = 'StateTimelineTooltip';