HistogramPanel.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { useMemo } from 'react';
  2. import { PanelProps, buildHistogram, getHistogramFields } from '@grafana/data';
  3. import { histogramFieldsToFrame } from '@grafana/data/src/transformations/transformers/histogram';
  4. import { useTheme2 } from '@grafana/ui';
  5. import { Histogram, getBucketSize } from './Histogram';
  6. import { PanelOptions } from './models.gen';
  7. type Props = PanelProps<PanelOptions>;
  8. export const HistogramPanel: React.FC<Props> = ({ data, options, width, height }) => {
  9. const theme = useTheme2();
  10. const histogram = useMemo(() => {
  11. if (!data?.series?.length) {
  12. return undefined;
  13. }
  14. if (data.series.length === 1) {
  15. const info = getHistogramFields(data.series[0]);
  16. if (info) {
  17. return histogramFieldsToFrame(info);
  18. }
  19. }
  20. const hist = buildHistogram(data.series, options);
  21. if (!hist) {
  22. return undefined;
  23. }
  24. return histogramFieldsToFrame(hist, theme);
  25. }, [data.series, options, theme]);
  26. if (!histogram || !histogram.fields.length) {
  27. return (
  28. <div className="panel-empty">
  29. <p>No histogram found in response</p>
  30. </div>
  31. );
  32. }
  33. const bucketSize = getBucketSize(histogram);
  34. return (
  35. <Histogram
  36. options={options}
  37. theme={theme}
  38. legend={options.legend}
  39. structureRev={data.structureRev}
  40. width={width}
  41. height={height}
  42. alignedFrame={histogram}
  43. bucketSize={bucketSize}
  44. >
  45. {(config, alignedFrame) => {
  46. return null; // <TooltipPlugin data={alignedFrame} config={config} mode={options.tooltip.mode} timeZone={timeZone} />;
  47. }}
  48. </Histogram>
  49. );
  50. };