BarGaugePanel.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Services & Utils
  4. import { config } from 'app/core/config';
  5. // Components
  6. import { BarGauge, VizRepeater, getFieldDisplayValues, FieldDisplay, DataLinksContextMenu } from '@grafana/ui';
  7. // Types
  8. import { BarGaugeOptions } from './types';
  9. import { PanelProps } from '@grafana/ui';
  10. import { getFieldLinksSupplier } from 'app/features/panel/panellinks/linkSuppliers';
  11. export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> {
  12. renderValue = (value: FieldDisplay, width: number, height: number): JSX.Element => {
  13. const { options } = this.props;
  14. const { field, display } = value;
  15. return (
  16. <DataLinksContextMenu links={getFieldLinksSupplier(value)}>
  17. {({ openMenu, targetClassName }) => {
  18. return (
  19. <BarGauge
  20. value={display}
  21. width={width}
  22. height={height}
  23. orientation={options.orientation}
  24. thresholds={field.thresholds}
  25. theme={config.theme}
  26. itemSpacing={this.getItemSpacing()}
  27. displayMode={options.displayMode}
  28. minValue={field.min}
  29. maxValue={field.max}
  30. onClick={openMenu}
  31. className={targetClassName}
  32. />
  33. );
  34. }}
  35. </DataLinksContextMenu>
  36. );
  37. };
  38. getValues = (): FieldDisplay[] => {
  39. const { data, options, replaceVariables } = this.props;
  40. return getFieldDisplayValues({
  41. ...options,
  42. replaceVariables,
  43. theme: config.theme,
  44. data: data.series,
  45. });
  46. };
  47. getItemSpacing(): number {
  48. if (this.props.options.displayMode === 'lcd') {
  49. return 2;
  50. }
  51. return 10;
  52. }
  53. render() {
  54. const { height, width, options, data, renderCounter } = this.props;
  55. return (
  56. <VizRepeater
  57. source={data}
  58. getValues={this.getValues}
  59. renderValue={this.renderValue}
  60. renderCounter={renderCounter}
  61. width={width}
  62. height={height}
  63. itemSpacing={this.getItemSpacing()}
  64. orientation={options.orientation}
  65. />
  66. );
  67. }
  68. }