SingleStatPanel.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Utils & Services
  4. import { config } from 'app/core/config';
  5. // Types
  6. import { SingleStatOptions } from './types';
  7. import { PanelProps, getFieldDisplayValues, VizRepeater, FieldDisplay, BigValue } from '@grafana/ui';
  8. import { BigValueSparkline } from '@grafana/ui/src/components/BigValue/BigValue';
  9. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  10. renderValue = (value: FieldDisplay, width: number, height: number): JSX.Element => {
  11. let sparkline: BigValueSparkline;
  12. if (value.sparkline) {
  13. const { timeRange, options } = this.props;
  14. sparkline = {
  15. ...options.sparkline,
  16. data: value.sparkline,
  17. minX: timeRange.from.valueOf(),
  18. maxX: timeRange.to.valueOf(),
  19. };
  20. }
  21. return <BigValue value={value.display} sparkline={sparkline} width={width} height={height} theme={config.theme} />;
  22. };
  23. getValues = (): FieldDisplay[] => {
  24. const { data, options, replaceVariables } = this.props;
  25. return getFieldDisplayValues({
  26. ...options,
  27. replaceVariables,
  28. theme: config.theme,
  29. data: data.series,
  30. sparkline: options.sparkline.show,
  31. });
  32. };
  33. render() {
  34. const { height, width, options, data, renderCounter } = this.props;
  35. return (
  36. <VizRepeater
  37. getValues={this.getValues}
  38. renderValue={this.renderValue}
  39. width={width}
  40. height={height}
  41. source={data}
  42. renderCounter={renderCounter}
  43. orientation={options.orientation}
  44. />
  45. );
  46. }
  47. }