2019-03-28 08:57:49 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-03-27 06:38:46 -05:00
|
|
|
import {
|
|
|
|
BigValue,
|
|
|
|
BigValueGraphMode,
|
2020-05-05 08:26:42 -05:00
|
|
|
DataLinksContextMenu,
|
|
|
|
VizRepeater,
|
|
|
|
VizRepeaterRenderValueProps,
|
2020-07-28 00:28:50 -05:00
|
|
|
BigValueTextMode,
|
2020-03-27 06:38:46 -05:00
|
|
|
} from '@grafana/ui';
|
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,
|
2019-12-01 10:02:44 -06:00
|
|
|
} from '@grafana/data';
|
|
|
|
|
2020-05-05 08:26:42 -05:00
|
|
|
import { config } from 'app/core/config';
|
|
|
|
import { StatPanelOptions } from './types';
|
|
|
|
import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
|
|
|
|
|
2019-11-25 18:13:14 -06:00
|
|
|
export class StatPanel extends PureComponent<PanelProps<StatPanelOptions>> {
|
2020-05-05 08:26:42 -05:00
|
|
|
renderComponent = (
|
|
|
|
valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>,
|
|
|
|
menuProps: DataLinksContextMenuApi
|
|
|
|
): JSX.Element => {
|
2019-10-04 05:01:42 -05:00
|
|
|
const { timeRange, options } = this.props;
|
2020-07-07 03:16:06 -05:00
|
|
|
const { value, alignmentFactors, width, height, count } = valueProps;
|
2020-05-05 08:26:42 -05:00
|
|
|
const { openMenu, targetClassName } = menuProps;
|
2020-12-08 10:13:12 -06:00
|
|
|
let sparkline = value.sparkline;
|
|
|
|
if (sparkline) {
|
|
|
|
sparkline.timeRange = timeRange;
|
2019-05-04 03:08:48 -05:00
|
|
|
}
|
2019-03-20 11:07:11 -05:00
|
|
|
|
2019-08-28 01:50:43 -05:00
|
|
|
return (
|
2020-05-05 08:26:42 -05:00
|
|
|
<BigValue
|
|
|
|
value={value.display}
|
2020-07-07 03:16:06 -05:00
|
|
|
count={count}
|
2020-05-05 08:26:42 -05:00
|
|
|
sparkline={sparkline}
|
|
|
|
colorMode={options.colorMode}
|
|
|
|
graphMode={options.graphMode}
|
|
|
|
justifyMode={options.justifyMode}
|
2020-07-28 00:28:50 -05:00
|
|
|
textMode={this.getTextMode()}
|
2020-05-05 08:26:42 -05:00
|
|
|
alignmentFactors={alignmentFactors}
|
2020-12-04 12:03:59 -06:00
|
|
|
text={options.text}
|
2020-05-05 08:26:42 -05:00
|
|
|
width={width}
|
|
|
|
height={height}
|
2021-04-29 05:44:06 -05:00
|
|
|
theme={config.theme2}
|
2020-05-05 08:26:42 -05:00
|
|
|
onClick={openMenu}
|
|
|
|
className={targetClassName}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2020-07-28 00:28:50 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:26:42 -05:00
|
|
|
renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay, DisplayValueAlignmentFactors>): JSX.Element => {
|
|
|
|
const { value } = valueProps;
|
|
|
|
const { getLinks, hasLinks } = value;
|
|
|
|
|
2020-06-17 05:26:01 -05:00
|
|
|
if (hasLinks && getLinks) {
|
|
|
|
return (
|
2021-03-05 03:29:19 -06:00
|
|
|
<DataLinksContextMenu links={getLinks} config={value.field}>
|
2021-01-20 00:59:48 -06:00
|
|
|
{(api) => {
|
2020-06-17 05:26:01 -05:00
|
|
|
return this.renderComponent(valueProps, api);
|
|
|
|
}}
|
|
|
|
</DataLinksContextMenu>
|
|
|
|
);
|
2020-05-05 08:26:42 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:26:01 -05:00
|
|
|
return this.renderComponent(valueProps, {});
|
2019-05-04 03:08:48 -05:00
|
|
|
};
|
2019-03-28 08:57:49 -05: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-10-04 05:01:42 -05:00
|
|
|
|
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-05-04 03:08:48 -05:00
|
|
|
replaceVariables,
|
2021-04-29 05:44:06 -05:00
|
|
|
theme: config.theme2,
|
2019-05-04 03:08:48 -05:00
|
|
|
data: data.series,
|
2019-12-01 10:02:44 -06:00
|
|
|
sparkline: options.graphMode !== BigValueGraphMode.None,
|
2020-04-27 08:28:06 -05:00
|
|
|
timeZone,
|
2019-03-22 09:25:33 -05:00
|
|
|
});
|
2019-03-14 16:47:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2019-12-01 10:02:44 -06:00
|
|
|
const { height, options, width, data, renderCounter } = this.props;
|
|
|
|
|
2019-03-14 16:47:01 -05:00
|
|
|
return (
|
2019-04-05 05:59:29 -05:00
|
|
|
<VizRepeater
|
|
|
|
getValues={this.getValues}
|
2019-12-01 10:02:44 -06:00
|
|
|
getAlignmentFactors={getDisplayValueAlignmentFactors}
|
2019-03-14 16:47:01 -05:00
|
|
|
renderValue={this.renderValue}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
2019-03-15 17:37:56 -05:00
|
|
|
source={data}
|
2020-04-06 01:22:49 -05:00
|
|
|
itemSpacing={3}
|
2019-03-19 08:07:48 -05:00
|
|
|
renderCounter={renderCounter}
|
2020-04-04 09:57:06 -05:00
|
|
|
autoGrid={true}
|
|
|
|
orientation={options.orientation}
|
2019-03-14 16:47:01 -05:00
|
|
|
/>
|
|
|
|
);
|
2019-03-14 15:20:24 -05:00
|
|
|
}
|
|
|
|
}
|