Color: Remove the default color scale logic (#34913)

This commit is contained in:
Torkel Ödegaard 2021-05-28 17:50:25 +02:00 committed by GitHub
parent add1b827ae
commit 179bfecb3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,7 @@ import { getValueFormat, isBooleanUnit } from '../valueFormats/valueFormats';
import { getValueMappingResult } from '../utils/valueMappings';
import { dateTime } from '../datetime';
import { KeyValue, TimeZone } from '../types';
import { getScaleCalculator, ScaleCalculator } from './scale';
import { getScaleCalculator } from './scale';
import { GrafanaTheme2 } from '../themes/types';
import { anyToNumber } from '../utils/anyToNumber';
@ -57,7 +57,6 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
const formatFunc = getValueFormat(unit || 'none');
const scaleFunc = getScaleCalculator(field, options.theme);
const defaultColor = getDefaultColorFunc(field, scaleFunc, options.theme);
return (value: any) => {
const { mappings } = config;
@ -117,7 +116,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
}
if (!color) {
const scaleResult = defaultColor(value);
const scaleResult = scaleFunc(-Infinity);
color = scaleResult.color;
percent = scaleResult.percent;
}
@ -136,32 +135,3 @@ export function getRawDisplayProcessor(): DisplayProcessor {
numeric: (null as unknown) as number,
});
}
function getDefaultColorFunc(field: Field, scaleFunc: ScaleCalculator, theme: GrafanaTheme2) {
if (field.type === FieldType.string) {
return (value: any) => {
if (!value) {
return { color: theme.colors.background.primary, percent: 0 };
}
const hc = strHashCode(value as string);
const color = theme.visualization.getColorByName(
theme.visualization.palette[Math.floor(hc % theme.visualization.palette.length)]
);
return {
color: color,
percent: 0,
};
};
}
return (value: any) => scaleFunc(-Infinity);
}
/**
* Converts a string into a numeric value -- we just need it to be different
* enough so that it has a reasonable distribution across a color pallet
*/
function strHashCode(str: string) {
return str.split('').reduce((prevHash, currVal) => ((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0, 0);
}