SingleStatPanel.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Libraries
  2. import React, { PureComponent, CSSProperties } from 'react';
  3. // Types
  4. import { SingleStatOptions, SingleStatBaseOptions } from './types';
  5. import { DisplayValue, PanelProps, processTimeSeries, NullValueMode, ColumnType } 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. const values: DisplayValue[] = [];
  23. for (const table of data) {
  24. for (let i = 0; i < table.columns.length; i++) {
  25. const column = table.columns[i];
  26. // Show all columns that are not 'time'
  27. if (column.type === ColumnType.number) {
  28. const series = processTimeSeries({
  29. data: [table],
  30. xColumn: i,
  31. yColumn: i,
  32. nullValueMode: NullValueMode.Null,
  33. })[0];
  34. const value = stat !== 'name' ? series.stats[stat] : series.label;
  35. values.push(processor(value));
  36. }
  37. }
  38. }
  39. if (values.length === 0) {
  40. throw { message: 'Could not find numeric data' };
  41. }
  42. return values;
  43. };
  44. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  45. renderValue = (value: DisplayValue, width: number, height: number): JSX.Element => {
  46. const style: CSSProperties = {};
  47. style.margin = '0 auto';
  48. style.fontSize = '250%';
  49. style.textAlign = 'center';
  50. if (value.color) {
  51. style.color = value.color;
  52. }
  53. return (
  54. <div style={{ width, height }}>
  55. <div style={style}>{value.text}</div>
  56. </div>
  57. );
  58. };
  59. getProcessedValues = (): DisplayValue[] => {
  60. return getSingleStatValues(this.props);
  61. };
  62. render() {
  63. const { height, width, options, data, renderCounter } = this.props;
  64. return (
  65. <ProcessedValuesRepeater
  66. getProcessedValues={this.getProcessedValues}
  67. renderValue={this.renderValue}
  68. width={width}
  69. height={height}
  70. source={data}
  71. renderCounter={renderCounter}
  72. orientation={options.orientation}
  73. />
  74. );
  75. }
  76. }