BarGaugePanel.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { isNumber } from 'lodash';
  2. import React, { PureComponent } from 'react';
  3. import {
  4. DisplayValueAlignmentFactors,
  5. FieldDisplay,
  6. getDisplayValueAlignmentFactors,
  7. getFieldDisplayValues,
  8. PanelProps,
  9. FieldConfig,
  10. DisplayProcessor,
  11. DisplayValue,
  12. VizOrientation,
  13. } from '@grafana/data';
  14. import { BarGauge, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
  15. import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
  16. import { config } from 'app/core/config';
  17. import { BarGaugeOptions } from './types';
  18. export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> {
  19. renderComponent = (
  20. valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
  21. menuProps: DataLinksContextMenuApi
  22. ): JSX.Element => {
  23. const { options, fieldConfig } = this.props;
  24. const { value, alignmentFactors, orientation, width, height, count } = valueProps;
  25. const { field, display, view, colIndex } = value;
  26. const { openMenu, targetClassName } = menuProps;
  27. let processor: DisplayProcessor | undefined = undefined;
  28. if (view && isNumber(colIndex)) {
  29. processor = view.getFieldDisplayProcessor(colIndex);
  30. }
  31. return (
  32. <BarGauge
  33. value={clearNameForSingleSeries(count, fieldConfig.defaults, display)}
  34. width={width}
  35. height={height}
  36. orientation={orientation}
  37. field={field}
  38. text={options.text}
  39. display={processor}
  40. theme={config.theme2}
  41. itemSpacing={this.getItemSpacing()}
  42. displayMode={options.displayMode}
  43. onClick={openMenu}
  44. className={targetClassName}
  45. alignmentFactors={count > 1 ? alignmentFactors : undefined}
  46. showUnfilled={options.showUnfilled}
  47. />
  48. );
  49. };
  50. renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>): JSX.Element => {
  51. const { value, orientation } = valueProps;
  52. const { hasLinks, getLinks } = value;
  53. if (hasLinks && getLinks) {
  54. return (
  55. <div style={{ width: '100%', display: orientation === VizOrientation.Vertical ? 'flex' : 'initial' }}>
  56. <DataLinksContextMenu links={getLinks} config={value.field}>
  57. {(api) => this.renderComponent(valueProps, api)}
  58. </DataLinksContextMenu>
  59. </div>
  60. );
  61. }
  62. return this.renderComponent(valueProps, {});
  63. };
  64. getValues = (): FieldDisplay[] => {
  65. const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
  66. return getFieldDisplayValues({
  67. fieldConfig,
  68. reduceOptions: options.reduceOptions,
  69. replaceVariables,
  70. theme: config.theme2,
  71. data: data.series,
  72. timeZone,
  73. });
  74. };
  75. getItemSpacing(): number {
  76. if (this.props.options.displayMode === 'lcd') {
  77. return 2;
  78. }
  79. return 10;
  80. }
  81. render() {
  82. const { height, width, options, data, renderCounter } = this.props;
  83. return (
  84. <VizRepeater
  85. source={data}
  86. getAlignmentFactors={getDisplayValueAlignmentFactors}
  87. getValues={this.getValues}
  88. renderValue={this.renderValue}
  89. renderCounter={renderCounter}
  90. width={width}
  91. height={height}
  92. minVizWidth={options.minVizWidth}
  93. minVizHeight={options.minVizHeight}
  94. itemSpacing={this.getItemSpacing()}
  95. orientation={options.orientation}
  96. />
  97. );
  98. }
  99. }
  100. export function clearNameForSingleSeries(count: number, field: FieldConfig<any>, display: DisplayValue): DisplayValue {
  101. if (count === 1 && !field.displayName) {
  102. return {
  103. ...display,
  104. title: undefined,
  105. };
  106. }
  107. return display;
  108. }