Enable new color picker in Gauge and Thresholds editor, use ThemeProvider instead of ContextSrv

This commit is contained in:
Dominik Prokop
2019-01-21 16:00:09 +01:00
parent ed48ecfe1d
commit 93bb30a561
6 changed files with 53 additions and 48 deletions

View File

@@ -4,15 +4,15 @@ import $ from 'jquery';
import {
ValueMapping,
Threshold,
ThemeName,
MappingType,
BasicGaugeColor,
ThemeNames,
ValueMap,
RangeMap,
} from '../../types/panel';
import { TimeSeriesVMs } from '../../types/series';
import { getValueFormat } from '../../utils/valueFormats/valueFormats';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
import { GrafanaTheme } from '@grafana/ui/src/types';
type TimeSeriesValue = string | number | null;
@@ -31,7 +31,7 @@ export interface Props {
suffix: string;
unit: string;
width: number;
theme?: ThemeName;
theme?: GrafanaTheme;
}
export class Gauge extends PureComponent<Props> {
@@ -48,7 +48,7 @@ export class Gauge extends PureComponent<Props> {
thresholds: [],
unit: 'none',
stat: 'avg',
theme: ThemeNames.Dark,
theme: GrafanaTheme.Dark,
};
componentDidMount() {
@@ -144,29 +144,29 @@ export class Gauge extends PureComponent<Props> {
}
getFontColor(value: TimeSeriesValue) {
const { thresholds } = this.props;
const { thresholds, theme } = this.props;
if (thresholds.length === 1) {
return thresholds[0].color;
return getColorFromHexRgbOrName(thresholds[0].color, theme);
}
const atThreshold = thresholds.filter(threshold => (value as number) === threshold.value)[0];
if (atThreshold) {
return atThreshold.color;
return getColorFromHexRgbOrName(atThreshold.color, theme);
}
const belowThreshold = thresholds.filter(threshold => (value as number) > threshold.value);
if (belowThreshold.length > 0) {
const nearestThreshold = belowThreshold.sort((t1, t2) => t2.value - t1.value)[0];
return nearestThreshold.color;
return getColorFromHexRgbOrName(nearestThreshold.color, theme);
}
return BasicGaugeColor.Red;
}
getFormattedThresholds() {
const { maxValue, minValue, thresholds } = this.props;
const { maxValue, minValue, thresholds, theme } = this.props;
const thresholdsSortedByIndex = [...thresholds].sort((t1, t2) => t1.index - t2.index);
const lastThreshold = thresholdsSortedByIndex[thresholdsSortedByIndex.length - 1];
@@ -174,13 +174,13 @@ export class Gauge extends PureComponent<Props> {
const formattedThresholds = [
...thresholdsSortedByIndex.map(threshold => {
if (threshold.index === 0) {
return { value: minValue, color: threshold.color };
return { value: minValue, color: getColorFromHexRgbOrName(threshold.color, theme) };
}
const previousThreshold = thresholdsSortedByIndex[threshold.index - 1];
return { value: threshold.value, color: previousThreshold.color };
return { value: threshold.value, color: getColorFromHexRgbOrName(previousThreshold.color, theme) };
}),
{ value: maxValue, color: lastThreshold.color },
{ value: maxValue, color: getColorFromHexRgbOrName(lastThreshold.color, theme) },
];
return formattedThresholds;
@@ -208,7 +208,7 @@ export class Gauge extends PureComponent<Props> {
}
const dimension = Math.min(width, height * 1.3);
const backgroundColor = theme === ThemeNames.Light ? 'rgb(230,230,230)' : 'rgb(38,38,38)';
const backgroundColor = theme === GrafanaTheme.Light ? 'rgb(230,230,230)' : 'rgb(38,38,38)';
const fontScale = parseInt('80', 10) / 100;
const fontSize = Math.min(dimension / 5, 100) * fontScale;
const gaugeWidthReduceRatio = showThresholdLabels ? 1.5 : 1;

View File

@@ -1,10 +1,11 @@
import React, { PureComponent } from 'react';
import { Threshold } from '../../types';
import { Threshold, Themeable } from '../../types';
import { ColorPicker } from '../ColorPicker/ColorPicker';
import { PanelOptionsGroup } from '../PanelOptionsGroup/PanelOptionsGroup';
import { colors } from '../../utils';
import { getColorFromHexRgbOrName } from '@grafana/ui/src/utils/colorsPalette';
export interface Props {
export interface Props extends Themeable {
thresholds: Threshold[];
onChange: (thresholds: Threshold[]) => void;
}
@@ -187,6 +188,7 @@ export class ThresholdsEditor extends PureComponent<Props, State> {
render() {
const { thresholds } = this.state;
const { theme } = this.props;
return (
<PanelOptionsGroup title="Thresholds">
@@ -197,7 +199,10 @@ export class ThresholdsEditor extends PureComponent<Props, State> {
<div className="thresholds-row-add-button" onClick={() => this.onAddThreshold(threshold.index + 1)}>
<i className="fa fa-plus" />
</div>
<div className="thresholds-row-color-indicator" style={{ backgroundColor: threshold.color }} />
<div
className="thresholds-row-color-indicator"
style={{ backgroundColor: getColorFromHexRgbOrName(threshold.color, theme) }}
/>
<div className="thresholds-row-input">{this.renderInput(threshold)}</div>
</div>
);

View File

@@ -36,7 +36,7 @@ export interface PanelMenuItem {
export interface Threshold {
index: number;
value: number;
color?: string;
color: string;
}
export enum BasicGaugeColor {
@@ -66,10 +66,3 @@ export interface RangeMap extends BaseMap {
from: string;
to: string;
}
export type ThemeName = 'dark' | 'light';
export enum ThemeNames {
Dark = 'dark',
Light = 'light',
}