grafana/public/app/plugins/panel/gauge/GaugePanel.tsx

117 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-01-09 10:00:10 -06:00
import React, { PureComponent } from 'react';
import { FieldDisplay, getDisplayProcessor, getFieldDisplayValues, PanelProps } from '@grafana/data';
import { BarGaugeSizing, VizOrientation } from '@grafana/schema';
import { DataLinksContextMenu, Gauge, VizRepeater, VizRepeaterRenderValueProps } from '@grafana/ui';
import { DataLinksContextMenuApi } from '@grafana/ui/src/components/DataLinks/DataLinksContextMenu';
import { config } from 'app/core/config';
import { clearNameForSingleSeries } from '../bargauge/BarGaugePanel';
2019-01-09 10:00:10 -06:00
import { defaultOptions, Options } from './panelcfg.gen';
export class GaugePanel extends PureComponent<PanelProps<Options>> {
renderComponent = (
valueProps: VizRepeaterRenderValueProps<FieldDisplay>,
menuProps: DataLinksContextMenuApi
): JSX.Element => {
const { options, fieldConfig } = this.props;
const { width, height, count, value } = valueProps;
const { field, display } = value;
const { openMenu, targetClassName } = menuProps;
return (
<Gauge
value={clearNameForSingleSeries(count, fieldConfig.defaults, display)}
width={width}
height={height}
field={field}
text={options.text}
showThresholdLabels={options.showThresholdLabels}
showThresholdMarkers={options.showThresholdMarkers}
theme={config.theme2}
onClick={openMenu}
className={targetClassName}
orientation={options.orientation}
/>
);
};
renderValue = (valueProps: VizRepeaterRenderValueProps<FieldDisplay>): JSX.Element => {
const { value } = valueProps;
const { getLinks, hasLinks } = value;
if (hasLinks && getLinks) {
return (
<DataLinksContextMenu links={getLinks} style={{ flexGrow: 1 }}>
{(api) => {
return this.renderComponent(valueProps, api);
}}
</DataLinksContextMenu>
);
}
return this.renderComponent(valueProps, {});
2019-03-14 16:47:01 -05:00
};
getValues = (): FieldDisplay[] => {
DateTime: adding support to select preferred timezone for presentation of date and time values. (#23586) * added moment timezone package. * added a qnd way of selecting timezone. * added a first draft to display how it can be used. * fixed failing tests. * made moment.local to be in utc when running tests. * added tests to verify that the timeZone support works as expected. * Fixed so we use the formatter in the graph context menu. * changed so we will format d3 according to timeZone. * changed from class base to function based for easier consumption. * fixed so tests got green. * renamed to make it shorter. * fixed formatting in logRow. * removed unused value. * added time formatter to flot. * fixed failing tests. * changed so history will use the formatting with support for timezone. * added todo. * added so we append the correct abbrivation behind time. * added time zone abbrevation in timepicker. * adding timezone in rangeutil tool. * will use timezone when formatting range. * changed so we use new functions to format date so timezone is respected. * wip - dashboard settings. * changed so the time picker settings is in react. * added force update. * wip to get the react graph to work. * fixed formatting and parsing on the timepicker. * updated snap to be correct. * fixed so we format values properly in time picker. * make sure we pass timezone on all the proper places. * fixed so we use correct timeZone in explore. * fixed failing tests. * fixed so we always parse from local to selected timezone. * removed unused variable. * reverted back. * trying to fix issue with directive. * fixed issue. * fixed strict null errors. * fixed so we still can select default. * make sure we reads the time zone from getTimezone
2020-04-27 08:28:06 -05:00
const { data, options, replaceVariables, fieldConfig, timeZone } = this.props;
for (let frame of data.series) {
for (let field of frame.fields) {
// Set the Min/Max value automatically for percent and percentunit
if (field.config.unit === 'percent' || field.config.unit === 'percentunit') {
const min = field.config.min ?? 0;
const max = field.config.max ?? (field.config.unit === 'percent' ? 100 : 1);
field.state = field.state ?? {};
field.state.range = { min, max, delta: max - min };
field.display = getDisplayProcessor({ field, theme: config.theme2 });
}
}
}
return getFieldDisplayValues({
FieldOverrides: Move FieldConfigSource from fieldOptions to PanelModel.fieldConfig (#22600) * Apply field overrides in PanelChrome * Move applyFieldOverrides to panel query runner * Review updates * Make sure overrides are applied back on souce panel when exiting the new edit mode * TS ignores in est * Make field display work in viz repeater * Review updates * Review and test updates * Change the way overrides and trransformations are retrieved in PQR * Add fieldConfig property to PanelModel * Dashboard migration v1 * Use field config when exiting new panel edit mode * Gauge - use fieldConfig from panel model * FieldDisplayOptions - don's extend FieldConfigSource * Fix fieldDisplay ts * StatPanel updated * Stat panel defaults applied * Table2 panel options update * React graph updates * BarGauge updated * PieChart, Gauge, BarGauge and Stat updates * PieChart - remove field config defaults from options * FieldDisplayEditor - remove unused methos * PanelModel - remove debugger * Remove fieldConfig from field options when migrating dashboard * Update data links migrations * Update fieldDisaplay tests to respect new fieldConfig * Update dashboard schema version in snapshots * Fix BarGaugePanel test * Rebase fixes * Add onFieldConfigChange to PanelProps type * Update shared single stat migration * Pass PanelModel instead of options only for panel type change handler [breaking] * Renames * Don't mutate panel options * Migrations update * Remove obsolete snap * Minor updates after review * Fix null checks * Temporarily (until we decide to switch to new pane edit) bring back old aditors * Temporarily rename ValueMappingEditor and MappingRow to Legacy* * Migrations update * Updae setFieldConfigDefaults API * Update the way field config defaults are applied * Use standard field config for gauge, bar gauge and stat panels * refactoring * Revert dashboard fieldOptions migrations as those are handled by single stat migrator * Fix ts in tests * Strict null fix and some minor fixes Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-03-19 05:50:31 -05:00
fieldConfig,
reduceOptions: options.reduceOptions,
replaceVariables,
theme: config.theme2,
data: data.series,
DateTime: adding support to select preferred timezone for presentation of date and time values. (#23586) * added moment timezone package. * added a qnd way of selecting timezone. * added a first draft to display how it can be used. * fixed failing tests. * made moment.local to be in utc when running tests. * added tests to verify that the timeZone support works as expected. * Fixed so we use the formatter in the graph context menu. * changed so we will format d3 according to timeZone. * changed from class base to function based for easier consumption. * fixed so tests got green. * renamed to make it shorter. * fixed formatting in logRow. * removed unused value. * added time formatter to flot. * fixed failing tests. * changed so history will use the formatting with support for timezone. * added todo. * added so we append the correct abbrivation behind time. * added time zone abbrevation in timepicker. * adding timezone in rangeutil tool. * will use timezone when formatting range. * changed so we use new functions to format date so timezone is respected. * wip - dashboard settings. * changed so the time picker settings is in react. * added force update. * wip to get the react graph to work. * fixed formatting and parsing on the timepicker. * updated snap to be correct. * fixed so we format values properly in time picker. * make sure we pass timezone on all the proper places. * fixed so we use correct timeZone in explore. * fixed failing tests. * fixed so we always parse from local to selected timezone. * removed unused variable. * reverted back. * trying to fix issue with directive. * fixed issue. * fixed strict null errors. * fixed so we still can select default. * make sure we reads the time zone from getTimezone
2020-04-27 08:28:06 -05:00
timeZone,
});
2019-03-14 16:47:01 -05:00
};
2019-01-09 10:00:10 -06:00
calculateGaugeSize = () => {
const { options } = this.props;
const orientation = options.orientation;
const isManualSizing = options.sizing === BarGaugeSizing.Manual;
const isVerticalOrientation = orientation === VizOrientation.Vertical;
const isHorizontalOrientation = orientation === VizOrientation.Horizontal;
const minVizWidth = isManualSizing && isVerticalOrientation ? options.minVizWidth : defaultOptions.minVizWidth;
const minVizHeight = isManualSizing && isHorizontalOrientation ? options.minVizHeight : defaultOptions.minVizHeight;
return { minVizWidth, minVizHeight };
};
2019-03-14 16:47:01 -05:00
render() {
const { height, width, data, renderCounter, options } = this.props;
const { minVizHeight, minVizWidth } = this.calculateGaugeSize();
2019-01-15 11:05:55 -06:00
return (
<VizRepeater
getValues={this.getValues}
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}
autoGrid={true}
renderCounter={renderCounter}
orientation={options.orientation}
minVizHeight={minVizHeight}
minVizWidth={minVizWidth}
2019-03-14 16:47:01 -05:00
/>
2019-01-15 11:05:55 -06:00
);
2019-01-09 10:00:10 -06:00
}
}