2019-02-15 12:55:35 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
2019-12-01 17:02:44 +01:00
|
|
|
import {
|
2020-05-05 15:26:42 +02:00
|
|
|
DisplayValueAlignmentFactors,
|
2019-12-01 17:02:44 +01:00
|
|
|
FieldDisplay,
|
|
|
|
|
getDisplayValueAlignmentFactors,
|
2020-05-05 15:26:42 +02:00
|
|
|
getFieldDisplayValues,
|
|
|
|
|
PanelProps,
|
2020-07-01 11:06:21 +02:00
|
|
|
FieldConfig,
|
2020-07-03 08:49:29 -07:00
|
|
|
DisplayProcessor,
|
2020-07-01 11:06:21 +02:00
|
|
|
DisplayValue,
|
2019-12-01 17:02:44 +01:00
|
|
|
} from '@grafana/data';
|
2020-05-05 15:26:42 +02:00
|
|
|
import { BarGauge, DataLinksContextMenu, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
|
|
|
|
|
|
|
|
|
|
import { config } from 'app/core/config';
|
|
|
|
|
import { BarGaugeOptions } from './types';
|
|
|
|
|
import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
|
2020-07-03 08:49:29 -07:00
|
|
|
import { isNumber } from 'lodash';
|
2019-03-13 08:26:06 +01:00
|
|
|
|
2019-03-14 14:47:01 -07:00
|
|
|
export class BarGaugePanel extends PureComponent<PanelProps<BarGaugeOptions>> {
|
2020-05-05 15:26:42 +02:00
|
|
|
renderComponent = (
|
|
|
|
|
valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
|
|
|
|
|
menuProps: DataLinksContextMenuApi
|
|
|
|
|
): JSX.Element => {
|
2020-07-27 20:26:22 +02:00
|
|
|
const { options, fieldConfig } = this.props;
|
2020-07-01 11:06:21 +02:00
|
|
|
const { value, alignmentFactors, orientation, width, height, count } = valueProps;
|
2019-12-28 17:32:58 -08:00
|
|
|
const { field, display, view, colIndex } = value;
|
2020-05-05 15:26:42 +02:00
|
|
|
const { openMenu, targetClassName } = menuProps;
|
|
|
|
|
|
2020-07-03 08:49:29 -07:00
|
|
|
let processor: DisplayProcessor | undefined = undefined;
|
|
|
|
|
if (view && isNumber(colIndex)) {
|
|
|
|
|
processor = view!.getFieldDisplayProcessor(colIndex as number);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 15:26:42 +02:00
|
|
|
return (
|
|
|
|
|
<BarGauge
|
2020-07-27 20:26:22 +02:00
|
|
|
value={clearNameForSingleSeries(count, fieldConfig.defaults, display)}
|
2020-05-05 15:26:42 +02:00
|
|
|
width={width}
|
|
|
|
|
height={height}
|
|
|
|
|
orientation={orientation}
|
|
|
|
|
field={field}
|
2020-12-04 10:03:59 -08:00
|
|
|
text={options.text}
|
2020-07-03 08:49:29 -07:00
|
|
|
display={processor}
|
2021-04-29 12:44:06 +02:00
|
|
|
theme={config.theme2}
|
2020-05-05 15:26:42 +02:00
|
|
|
itemSpacing={this.getItemSpacing()}
|
|
|
|
|
displayMode={options.displayMode}
|
|
|
|
|
onClick={openMenu}
|
|
|
|
|
className={targetClassName}
|
2020-07-09 09:18:05 +02:00
|
|
|
alignmentFactors={count > 1 ? alignmentFactors : undefined}
|
2020-05-05 15:26:42 +02:00
|
|
|
showUnfilled={options.showUnfilled}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>): JSX.Element => {
|
|
|
|
|
const { value } = valueProps;
|
|
|
|
|
const { hasLinks, getLinks } = value;
|
|
|
|
|
|
2020-06-17 04:26:01 -06:00
|
|
|
if (hasLinks && getLinks) {
|
|
|
|
|
return (
|
2021-03-05 10:29:19 +01:00
|
|
|
<DataLinksContextMenu links={getLinks} config={value.field}>
|
2021-01-20 07:59:48 +01:00
|
|
|
{(api) => {
|
2020-06-17 04:26:01 -06:00
|
|
|
return this.renderComponent(valueProps, api);
|
|
|
|
|
}}
|
|
|
|
|
</DataLinksContextMenu>
|
|
|
|
|
);
|
2020-05-05 15:26:42 +02:00
|
|
|
}
|
2020-06-17 04:26:01 -06:00
|
|
|
return this.renderComponent(valueProps, {});
|
2019-03-14 14:47:01 -07:00
|
|
|
};
|
2019-02-15 12:55:35 +01:00
|
|
|
|
2019-05-04 01:08:48 -07:00
|
|
|
getValues = (): FieldDisplay[] => {
|
2020-04-27 15:28:06 +02:00
|
|
|
const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
|
2019-05-04 01:08:48 -07:00
|
|
|
return getFieldDisplayValues({
|
2020-03-19 11:50:31 +01:00
|
|
|
fieldConfig,
|
2020-03-28 23:11:50 +01:00
|
|
|
reduceOptions: options.reduceOptions,
|
2019-04-16 13:23:34 -07:00
|
|
|
replaceVariables,
|
2021-04-29 12:44:06 +02:00
|
|
|
theme: config.theme2,
|
2019-04-16 13:23:34 -07:00
|
|
|
data: data.series,
|
2020-04-27 15:28:06 +02:00
|
|
|
timeZone,
|
2019-03-28 06:57:49 -07:00
|
|
|
});
|
2019-03-14 14:47:01 -07:00
|
|
|
};
|
2019-02-15 12:55:35 +01:00
|
|
|
|
2019-04-05 12:59:29 +02:00
|
|
|
getItemSpacing(): number {
|
|
|
|
|
if (this.props.options.displayMode === 'lcd') {
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 10;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 14:47:01 -07:00
|
|
|
render() {
|
2019-03-19 09:26:15 -07:00
|
|
|
const { height, width, options, data, renderCounter } = this.props;
|
2019-04-16 13:23:34 -07:00
|
|
|
|
2019-02-23 11:31:35 +01:00
|
|
|
return (
|
2019-04-05 12:59:29 +02:00
|
|
|
<VizRepeater
|
|
|
|
|
source={data}
|
2019-12-01 17:02:44 +01:00
|
|
|
getAlignmentFactors={getDisplayValueAlignmentFactors}
|
2019-04-05 12:59:29 +02:00
|
|
|
getValues={this.getValues}
|
2019-03-14 14:47:01 -07:00
|
|
|
renderValue={this.renderValue}
|
2019-04-05 12:59:29 +02:00
|
|
|
renderCounter={renderCounter}
|
2019-03-14 14:47:01 -07:00
|
|
|
width={width}
|
|
|
|
|
height={height}
|
2020-08-12 16:26:18 +01:00
|
|
|
minVizHeight={10}
|
2019-04-05 12:59:29 +02:00
|
|
|
itemSpacing={this.getItemSpacing()}
|
2019-03-19 14:07:48 +01:00
|
|
|
orientation={options.orientation}
|
2019-03-14 14:47:01 -07:00
|
|
|
/>
|
2019-02-23 11:31:35 +01:00
|
|
|
);
|
2019-02-15 12:55:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-01 11:06:21 +02:00
|
|
|
|
|
|
|
|
export function clearNameForSingleSeries(count: number, field: FieldConfig<any>, display: DisplayValue): DisplayValue {
|
|
|
|
|
if (count === 1 && !field.displayName) {
|
|
|
|
|
return {
|
|
|
|
|
...display,
|
|
|
|
|
title: undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return display;
|
|
|
|
|
}
|