GaugePanelEditor.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import {
  4. PanelEditorProps,
  5. ThresholdsEditor,
  6. Threshold,
  7. PanelOptionsGrid,
  8. ValueMappingsEditor,
  9. ValueMapping,
  10. FieldDisplayOptions,
  11. FieldDisplayEditor,
  12. Field,
  13. FieldPropertiesEditor,
  14. Switch,
  15. PanelOptionsGroup,
  16. } from '@grafana/ui';
  17. import { GaugeOptions } from './types';
  18. export class GaugePanelEditor extends PureComponent<PanelEditorProps<GaugeOptions>> {
  19. labelWidth = 6;
  20. onToggleThresholdLabels = () =>
  21. this.props.onOptionsChange({ ...this.props.options, showThresholdLabels: !this.props.options.showThresholdLabels });
  22. onToggleThresholdMarkers = () =>
  23. this.props.onOptionsChange({
  24. ...this.props.options,
  25. showThresholdMarkers: !this.props.options.showThresholdMarkers,
  26. });
  27. onThresholdsChanged = (thresholds: Threshold[]) =>
  28. this.onDisplayOptionsChanged({
  29. ...this.props.options.fieldOptions,
  30. thresholds,
  31. });
  32. onValueMappingsChanged = (mappings: ValueMapping[]) =>
  33. this.onDisplayOptionsChanged({
  34. ...this.props.options.fieldOptions,
  35. mappings,
  36. });
  37. onDisplayOptionsChanged = (fieldOptions: FieldDisplayOptions) =>
  38. this.props.onOptionsChange({
  39. ...this.props.options,
  40. fieldOptions,
  41. });
  42. onDefaultsChange = (field: Partial<Field>) => {
  43. this.onDisplayOptionsChanged({
  44. ...this.props.options.fieldOptions,
  45. defaults: field,
  46. });
  47. };
  48. render() {
  49. const { options } = this.props;
  50. const { fieldOptions, showThresholdLabels, showThresholdMarkers } = options;
  51. return (
  52. <>
  53. <PanelOptionsGrid>
  54. <PanelOptionsGroup title="Display">
  55. <FieldDisplayEditor
  56. onChange={this.onDisplayOptionsChanged}
  57. value={fieldOptions}
  58. labelWidth={this.labelWidth}
  59. />
  60. <Switch
  61. label="Labels"
  62. labelClass={`width-${this.labelWidth}`}
  63. checked={showThresholdLabels}
  64. onChange={this.onToggleThresholdLabels}
  65. />
  66. <Switch
  67. label="Markers"
  68. labelClass={`width-${this.labelWidth}`}
  69. checked={showThresholdMarkers}
  70. onChange={this.onToggleThresholdMarkers}
  71. />
  72. </PanelOptionsGroup>
  73. <PanelOptionsGroup title="Field">
  74. <FieldPropertiesEditor showMinMax={true} onChange={this.onDefaultsChange} value={fieldOptions.defaults} />
  75. </PanelOptionsGroup>
  76. <ThresholdsEditor onChange={this.onThresholdsChanged} thresholds={fieldOptions.thresholds} />
  77. </PanelOptionsGrid>
  78. <ValueMappingsEditor onChange={this.onValueMappingsChanged} valueMappings={fieldOptions.mappings} />
  79. </>
  80. );
  81. }
  82. }