grafana/public/app/plugins/panel/stat/StatPanel.tsx
Jev Forsberg de901560d7
Stat: Add ability to remove default single-color background gradient (#64353)
* baldm0mma/feature/solidOption/ update cue and ts types

* baldm0mma/feature/solidOption/ update data structures

* baldm0mma/feature/solidOption/ vizrepeater investigation

* baldm0mma/feature/solidOption/ add buildGradientBackground

* baldm0mma/feature/solidOption/ remove conlogs from fieldDisplay.ts

* baldm0mma/feature/solidOption/ rem destruct in fielddisplay.ts

* baldm0mma/feature/solidOption/ rem conlogs from bivaluelayout.tsx

* baldm0mma/feature/solidOption/ rem conlogs in visrepeater

* baldm0mma/feature/solidOption/ rem conlogs from statpanel.tsx

* baldm0mma/feature/solidOption/ add annotations in BigValue.tsx

* baldm0mma/feature/solidOption/ update cue

* baldm0mma/feature/solidOption/ remove unused anno

* baldm0mma/feature/solidOption/ update with toggle conditinal

* Update module.tsx

update anno

* baldm0mma/feature/solidOption/ update annos

* baldm0mma/feature/solidOption/ undo change

* baldm0mma/featurew/solidOption/ add anno for boolean toggle in stat module

* baldm0mma/featurew/solidOption/ update anno in BigValue.tsx for hasGradient

* baldm0mma/featurew/solidOption/ updart default logic in bigValueLayout.tsx
2023-03-14 08:59:54 -06:00

139 lines
4.3 KiB
TypeScript

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 { BigValueTextMode, BigValueGraphMode } from '@grafana/schema';
import { BigValue, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
import { config } from 'app/core/config';
import { PanelOptions } from './panelcfg.gen';
export class StatPanel extends PureComponent<PanelProps<PanelOptions>> {
renderComponent = (
valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
menuProps: DataLinksContextMenuApi
): JSX.Element => {
const { timeRange, options } = this.props;
const { value, alignmentFactors, width, height, count, orientation } = 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}
hasGradient={options.hasGradient}
graphMode={options.graphMode}
justifyMode={options.justifyMode}
textMode={this.getTextMode()}
alignmentFactors={alignmentFactors}
parentOrientation={orientation}
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}>
{(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}
/>
);
}
}