SingleStatPanel.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Libraries
  2. import React, { PureComponent, CSSProperties } from 'react';
  3. // Types
  4. import { SingleStatOptions, SingleStatBaseOptions } from './types';
  5. import { DisplayValue, PanelProps, processTimeSeries, NullValueMode } 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<SingleStatBaseOptions>): DisplayValue[] => {
  10. const { data, replaceVariables, options } = props;
  11. const { valueOptions, valueMappings } = options;
  12. const { unit, decimals, stat } = valueOptions;
  13. const processor = getDisplayProcessor({
  14. unit,
  15. decimals,
  16. mappings: valueMappings,
  17. thresholds: options.thresholds,
  18. prefix: replaceVariables(valueOptions.prefix),
  19. suffix: replaceVariables(valueOptions.suffix),
  20. theme: config.theme,
  21. });
  22. return processTimeSeries({
  23. data,
  24. nullValueMode: NullValueMode.Null,
  25. }).map((series, index) => {
  26. const value = stat !== 'name' ? series.stats[stat] : series.label;
  27. return processor(value);
  28. });
  29. };
  30. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  31. renderValue = (value: DisplayValue, width: number, height: number): JSX.Element => {
  32. const style: CSSProperties = {};
  33. style.margin = '0 auto';
  34. style.fontSize = '250%';
  35. style.textAlign = 'center';
  36. if (value.color) {
  37. style.color = value.color;
  38. }
  39. return (
  40. <div style={{ width, height }}>
  41. <div style={style}>{value.text}</div>
  42. </div>
  43. );
  44. };
  45. getProcessedValues = (): DisplayValue[] => {
  46. return getSingleStatValues(this.props);
  47. };
  48. render() {
  49. const { height, width, options, data, renderCounter } = this.props;
  50. return (
  51. <ProcessedValuesRepeater
  52. getProcessedValues={this.getProcessedValues}
  53. renderValue={this.renderValue}
  54. width={width}
  55. height={height}
  56. source={data}
  57. renderCounter={renderCounter}
  58. orientation={options.orientation}
  59. />
  60. );
  61. }
  62. }