SingleStatPanel.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Libraries
  2. import React, { PureComponent, CSSProperties } from 'react';
  3. // Types
  4. import { SingleStatOptions } from './types';
  5. import { processSingleStatPanelData, DisplayValue, PanelProps } from '@grafana/ui';
  6. import { config } from 'app/core/config';
  7. import { getDisplayProcessor } from '@grafana/ui';
  8. import { ProcessedValuesRepeater } from './ProcessedValuesRepeater';
  9. export const getSingleStatValues = (props: PanelProps<SingleStatOptions>): DisplayValue[] => {
  10. const { panelData, replaceVariables, options } = props;
  11. const { valueOptions, valueMappings } = options;
  12. const processor = getDisplayProcessor({
  13. unit: valueOptions.unit,
  14. decimals: valueOptions.decimals,
  15. mappings: valueMappings,
  16. thresholds: options.thresholds,
  17. prefix: replaceVariables(valueOptions.prefix),
  18. suffix: replaceVariables(valueOptions.suffix),
  19. theme: config.theme,
  20. });
  21. return processSingleStatPanelData({
  22. panelData: panelData,
  23. stat: valueOptions.stat,
  24. }).map(stat => processor(stat.value));
  25. };
  26. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  27. renderValue = (value: DisplayValue, width: number, height: number): JSX.Element => {
  28. const style: CSSProperties = {};
  29. style.margin = '0 auto';
  30. style.fontSize = '250%';
  31. style.textAlign = 'center';
  32. if (value.color) {
  33. style.color = value.color;
  34. }
  35. return (
  36. <div style={{ width, height }}>
  37. <div style={style}>{value.text}</div>
  38. </div>
  39. );
  40. };
  41. getProcessedValues = (): DisplayValue[] => {
  42. return getSingleStatValues(this.props);
  43. };
  44. render() {
  45. const { height, width, options, panelData } = this.props;
  46. const { orientation } = options;
  47. return (
  48. <ProcessedValuesRepeater
  49. getProcessedValues={this.getProcessedValues}
  50. renderValue={this.renderValue}
  51. width={width}
  52. height={height}
  53. source={panelData}
  54. orientation={orientation}
  55. />
  56. );
  57. }
  58. }