grafana/public/app/plugins/panel/bargauge/BarGaugePanel.tsx
Torkel Ödegaard 73ce20ab48
Table Panel: Add ability to use text color for value or hide value in gauge cell (#61477)
* BarGauge: New value options

* Fix typings for cell options, add new value mode option for bar gauge cells

* Add BarGauge panel option, tests, and update test dashboard

* Updated

* Added default

* Goodbye trusty console.log

* Update

* Merge changes from main

* Update docs

* Add valuemode doc changes

* Update gdev dashboard

* Update valueMode symbol name to valueDisplayMode

* Use Enums as Opposed to literals, don't calculate values when hidden

* Remove double import

* Fix tests

* One more test fix

* Remove erroneous targets field, fix type of maxDataPoints

* Strip nulls and add index field to Thresholds

* Gen cue

* remove bad targets again

* Fixes

---------

Co-authored-by: Kyle Cunningham <kyle@codeincarnate.com>
Co-authored-by: sam boyer <sdboyer@grafana.com>
2023-03-10 14:41:46 +01:00

125 lines
3.7 KiB
TypeScript

import { isNumber } from 'lodash';
import React, { PureComponent } from 'react';
import {
DisplayValueAlignmentFactors,
FieldDisplay,
getDisplayValueAlignmentFactors,
getFieldDisplayValues,
PanelProps,
FieldConfig,
DisplayProcessor,
DisplayValue,
VizOrientation,
} from '@grafana/data';
import { BarGauge, 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 BarGaugePanel extends PureComponent<BarGaugePanelProps> {
renderComponent = (
valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
menuProps: DataLinksContextMenuApi
): JSX.Element => {
const { options, fieldConfig } = this.props;
const { value, alignmentFactors, orientation, width, height, count } = valueProps;
const { field, display, view, colIndex } = value;
const { openMenu, targetClassName } = menuProps;
let processor: DisplayProcessor | undefined = undefined;
if (view && isNumber(colIndex)) {
processor = view.getFieldDisplayProcessor(colIndex);
}
return (
<BarGauge
value={clearNameForSingleSeries(count, fieldConfig.defaults, display)}
width={width}
height={height}
orientation={orientation}
field={field}
text={options.text}
display={processor}
theme={config.theme2}
itemSpacing={this.getItemSpacing()}
displayMode={options.displayMode}
onClick={openMenu}
className={targetClassName}
alignmentFactors={count > 1 ? alignmentFactors : undefined}
showUnfilled={options.showUnfilled}
valueDisplayMode={options.valueMode}
/>
);
};
renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>): JSX.Element => {
const { value, orientation } = valueProps;
const { hasLinks, getLinks } = value;
if (hasLinks && getLinks) {
return (
<div style={{ width: '100%', display: orientation === VizOrientation.Vertical ? 'flex' : 'initial' }}>
<DataLinksContextMenu links={getLinks}>{(api) => this.renderComponent(valueProps, api)}</DataLinksContextMenu>
</div>
);
}
return this.renderComponent(valueProps, {});
};
getValues = (): FieldDisplay[] => {
const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
return getFieldDisplayValues({
fieldConfig,
reduceOptions: options.reduceOptions,
replaceVariables,
theme: config.theme2,
data: data.series,
timeZone,
});
};
getItemSpacing(): number {
if (this.props.options.displayMode === 'lcd') {
return 2;
}
return 10;
}
render() {
const { height, width, options, data, renderCounter } = this.props;
return (
<VizRepeater
source={data}
getAlignmentFactors={getDisplayValueAlignmentFactors}
getValues={this.getValues}
renderValue={this.renderValue}
renderCounter={renderCounter}
width={width}
height={height}
minVizWidth={options.minVizWidth}
minVizHeight={options.minVizHeight}
itemSpacing={this.getItemSpacing()}
orientation={options.orientation}
/>
);
}
}
export type BarGaugePanelProps = PanelProps<PanelOptions>;
export function clearNameForSingleSeries(count: number, field: FieldConfig, display: DisplayValue): DisplayValue {
if (count === 1 && !field.displayName) {
return {
...display,
title: undefined,
};
}
return display;
}