SingleStatPanel.tsx 2.6 KB

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