SingleStatPanel.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 {
  8. PanelProps,
  9. getFieldDisplayValues,
  10. VizRepeater,
  11. FieldDisplay,
  12. BigValue,
  13. DataLinksContextMenu,
  14. } from '@grafana/ui';
  15. import { BigValueSparkline } from '@grafana/ui/src/components/BigValue/BigValue';
  16. import { getFieldLinksSupplier } from 'app/features/panel/panellinks/linkSuppliers';
  17. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  18. renderValue = (value: FieldDisplay, width: number, height: number): JSX.Element => {
  19. let sparkline: BigValueSparkline;
  20. if (value.sparkline) {
  21. const { timeRange, options } = this.props;
  22. sparkline = {
  23. ...options.sparkline,
  24. data: value.sparkline,
  25. minX: timeRange.from.valueOf(),
  26. maxX: timeRange.to.valueOf(),
  27. };
  28. }
  29. return (
  30. <DataLinksContextMenu links={getFieldLinksSupplier(value)}>
  31. {({ openMenu, targetClassName }) => {
  32. return (
  33. <BigValue
  34. value={value.display}
  35. sparkline={sparkline}
  36. width={width}
  37. height={height}
  38. theme={config.theme}
  39. onClick={openMenu}
  40. className={targetClassName}
  41. />
  42. );
  43. }}
  44. </DataLinksContextMenu>
  45. );
  46. };
  47. getValues = (): FieldDisplay[] => {
  48. const { data, options, replaceVariables } = this.props;
  49. return getFieldDisplayValues({
  50. ...options,
  51. replaceVariables,
  52. theme: config.theme,
  53. data: data.series,
  54. sparkline: options.sparkline.show,
  55. });
  56. };
  57. render() {
  58. const { height, width, options, data, renderCounter } = this.props;
  59. return (
  60. <VizRepeater
  61. getValues={this.getValues}
  62. renderValue={this.renderValue}
  63. width={width}
  64. height={height}
  65. source={data}
  66. renderCounter={renderCounter}
  67. orientation={options.orientation}
  68. />
  69. );
  70. }
  71. }