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