GaugePanel.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { PureComponent } from 'react';
  2. import { FieldDisplay, getFieldDisplayValues, PanelProps } from '@grafana/data';
  3. import { DataLinksContextMenu, Gauge, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
  4. import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
  5. import { config } from 'app/core/config';
  6. import { clearNameForSingleSeries } from '../bargauge/BarGaugePanel';
  7. import { GaugeOptions } from './types';
  8. export class GaugePanel extends PureComponent<PanelProps<GaugeOptions>> {
  9. renderComponent = (
  10. valueProps: VizRepeaterRenderValueProps<FieldDisplay>,
  11. menuProps: DataLinksContextMenuApi
  12. ): JSX.Element => {
  13. const { options, fieldConfig } = this.props;
  14. const { width, height, count, value } = valueProps;
  15. const { field, display } = value;
  16. const { openMenu, targetClassName } = menuProps;
  17. return (
  18. <Gauge
  19. value={clearNameForSingleSeries(count, fieldConfig.defaults, display)}
  20. width={width}
  21. height={height}
  22. field={field}
  23. text={options.text}
  24. showThresholdLabels={options.showThresholdLabels}
  25. showThresholdMarkers={options.showThresholdMarkers}
  26. theme={config.theme}
  27. onClick={openMenu}
  28. className={targetClassName}
  29. />
  30. );
  31. };
  32. renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay>): JSX.Element => {
  33. const { value } = valueProps;
  34. const { getLinks, hasLinks } = value;
  35. if (hasLinks && getLinks) {
  36. return (
  37. <DataLinksContextMenu links={getLinks} config={value.field}>
  38. {(api) => {
  39. return this.renderComponent(valueProps, api);
  40. }}
  41. </DataLinksContextMenu>
  42. );
  43. }
  44. return this.renderComponent(valueProps, {});
  45. };
  46. getValues = (): FieldDisplay[] => {
  47. const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
  48. return getFieldDisplayValues({
  49. fieldConfig,
  50. reduceOptions: options.reduceOptions,
  51. replaceVariables,
  52. theme: config.theme2,
  53. data: data.series,
  54. timeZone,
  55. });
  56. };
  57. render() {
  58. const { height, width, data, renderCounter, options } = this.props;
  59. return (
  60. <VizRepeater
  61. getValues={this.getValues}
  62. renderValue={this.renderValue}
  63. width={width}
  64. height={height}
  65. source={data}
  66. autoGrid={true}
  67. renderCounter={renderCounter}
  68. orientation={options.orientation}
  69. />
  70. );
  71. }
  72. }