123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { isNumber } from 'lodash';
- import React, { PureComponent } from 'react';
- import {
- DisplayValueAlignmentFactors,
- FieldDisplay,
- FieldType,
- getDisplayValueAlignmentFactors,
- getFieldDisplayValues,
- NumericRange,
- PanelProps,
- } from '@grafana/data';
- import { findNumericFieldMinMax } from '@grafana/data/src/field/fieldOverrides';
- import {
- BigValue,
- BigValueGraphMode,
- DataLinksContextMenu,
- VizRepeater,
- VizRepeaterRenderValueProps,
- BigValueTextMode,
- } from '@grafana/ui';
- import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
- import { config } from 'app/core/config';
- import { StatPanelOptions } from './types';
- export class StatPanel extends PureComponent<PanelProps<StatPanelOptions>> {
- renderComponent = (
- valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
- menuProps: DataLinksContextMenuApi
- ): JSX.Element => {
- const { timeRange, options } = this.props;
- const { value, alignmentFactors, width, height, count } = valueProps;
- const { openMenu, targetClassName } = menuProps;
- let sparkline = value.sparkline;
- if (sparkline) {
- sparkline.timeRange = timeRange;
- }
- return (
- <BigValue
- value={value.display}
- count={count}
- sparkline={sparkline}
- colorMode={options.colorMode}
- graphMode={options.graphMode}
- justifyMode={options.justifyMode}
- textMode={this.getTextMode()}
- alignmentFactors={alignmentFactors}
- text={options.text}
- width={width}
- height={height}
- theme={config.theme2}
- onClick={openMenu}
- className={targetClassName}
- />
- );
- };
- getTextMode() {
- const { options, fieldConfig, title } = this.props;
- // If we have manually set displayName or panel title switch text mode to value and name
- if (options.textMode === BigValueTextMode.Auto && (fieldConfig.defaults.displayName || !title)) {
- return BigValueTextMode.ValueAndName;
- }
- return options.textMode;
- }
- renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>): JSX.Element => {
- const { value } = valueProps;
- const { getLinks, hasLinks } = value;
- if (hasLinks && getLinks) {
- return (
- <DataLinksContextMenu links={getLinks} config={value.field}>
- {(api) => {
- return this.renderComponent(valueProps, api);
- }}
- </DataLinksContextMenu>
- );
- }
- return this.renderComponent(valueProps, {});
- };
- getValues = (): FieldDisplay[] => {
- const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
- let globalRange: NumericRange | undefined = undefined;
- for (let frame of data.series) {
- for (let field of frame.fields) {
- let { config } = field;
- // mostly copied from fieldOverrides, since they are skipped during streaming
- // Set the Min/Max value automatically
- if (field.type === FieldType.number) {
- if (field.state?.range) {
- continue;
- }
- if (!globalRange && (!isNumber(config.min) || !isNumber(config.max))) {
- globalRange = findNumericFieldMinMax(data.series);
- }
- const min = config.min ?? globalRange!.min;
- const max = config.max ?? globalRange!.max;
- field.state = field.state ?? {};
- field.state.range = { min, max, delta: max! - min! };
- }
- }
- }
- return getFieldDisplayValues({
- fieldConfig,
- reduceOptions: options.reduceOptions,
- replaceVariables,
- theme: config.theme2,
- data: data.series,
- sparkline: options.graphMode !== BigValueGraphMode.None,
- timeZone,
- });
- };
- render() {
- const { height, options, width, data, renderCounter } = this.props;
- return (
- <VizRepeater
- getValues={this.getValues}
- getAlignmentFactors={getDisplayValueAlignmentFactors}
- renderValue={this.renderValue}
- width={width}
- height={height}
- source={data}
- itemSpacing={3}
- renderCounter={renderCounter}
- autoGrid={true}
- orientation={options.orientation}
- />
- );
- }
- }
|