Fixed getFontColor, added tests and fixed thresholds logic

This commit is contained in:
Hugo Häggmark
2019-01-18 06:40:30 +01:00
parent 533b938fcd
commit 9dcf3d58ea
2 changed files with 39 additions and 24 deletions
+22 -6
View File
@@ -38,17 +38,33 @@ const setup = (propOverrides?: object) => {
}; };
describe('Get font color', () => { describe('Get font color', () => {
it('should get base color if no threshold', () => { it('should get first threshold color when only one threshold', () => {
const { instance } = setup(); const { instance } = setup({ thresholds: [{ index: 0, value: -Infinity, color: '#7EB26D' }] });
expect(instance.getFontColor(40)).toEqual('#7EB26D'); expect(instance.getFontColor(49)).toEqual('#7EB26D');
}); });
it('should be f2f2f2', () => { it('should get the next threshold color if value is same as a threshold', () => {
const { instance } = setup({ const { instance } = setup({
thresholds: [{ index: 0, value: -Infinity, color: '#7EB26D' }, { index: 1, value: 59, color: '#f2f2f2' }], thresholds: [
{ index: 2, value: 75, color: '#6ED0E0' },
{ index: 1, value: 50, color: '#EAB839' },
{ index: 0, value: -Infinity, color: '#7EB26D' },
],
}); });
expect(instance.getFontColor(58)).toEqual('#f2f2f2'); expect(instance.getFontColor(50)).toEqual('#6ED0E0');
});
it('should get the nearest threshold color', () => {
const { instance } = setup({
thresholds: [
{ index: 2, value: 75, color: '#6ED0E0' },
{ index: 1, value: 50, color: '#EAB839' },
{ index: 0, value: -Infinity, color: '#7EB26D' },
],
});
expect(instance.getFontColor(6.5)).toEqual('#EAB839');
}); });
}); });
+17 -18
View File
@@ -82,14 +82,14 @@ export class Gauge extends PureComponent<Props> {
} }
if (isNaN(value)) { if (isNaN(value)) {
return '-'; return value;
} }
return `${prefix} ${formattedValue} ${suffix}`; return `${prefix} ${formattedValue} ${suffix}`;
} }
getFontColor(value) { getFontColor(value: string | number) {
const { maxValue, thresholds } = this.props; const { thresholds } = this.props;
if (thresholds.length === 1) { if (thresholds.length === 1) {
return thresholds[0].color; return thresholds[0].color;
@@ -98,12 +98,11 @@ export class Gauge extends PureComponent<Props> {
const atThreshold = thresholds.filter(threshold => value < threshold.value); const atThreshold = thresholds.filter(threshold => value < threshold.value);
if (atThreshold.length > 0) { if (atThreshold.length > 0) {
return atThreshold[0].color; const nearestThreshold = atThreshold.sort((t1, t2) => t1.value - t2.value)[0];
} else if (value <= maxValue) { return nearestThreshold.color;
return BasicGaugeColor.Red;
} }
return ''; return BasicGaugeColor.Red;
} }
draw() { draw() {
@@ -136,16 +135,16 @@ export class Gauge extends PureComponent<Props> {
const thresholdMarkersWidth = gaugeWidth / 5; const thresholdMarkersWidth = gaugeWidth / 5;
const thresholdLabelFontSize = fontSize / 2.5; const thresholdLabelFontSize = fontSize / 2.5;
// const formattedThresholds = [ const formattedThresholds = [
// { value: minValue, color: BasicGaugeColor.Green }, { value: minValue, color: thresholds.length === 1 ? thresholds[0].color : BasicGaugeColor.Green },
// ...thresholds.map((threshold, index) => { ...thresholds.map((threshold, index) => {
// return { return {
// value: threshold.value, value: threshold.value,
// color: index === 0 ? threshold.color : thresholds[index].color, color: thresholds[index].color,
// }; };
// }), }),
// { value: maxValue, color: thresholds.length > 0 ? BasicGaugeColor.Red : baseColor }, { value: maxValue, color: thresholds.length === 1 ? thresholds[0].color : BasicGaugeColor.Red },
// ]; ];
const options = { const options = {
series: { series: {
@@ -163,7 +162,7 @@ export class Gauge extends PureComponent<Props> {
layout: { margin: 0, thresholdWidth: 0 }, layout: { margin: 0, thresholdWidth: 0 },
cell: { border: { width: 0 } }, cell: { border: { width: 0 } },
threshold: { threshold: {
values: thresholds, values: formattedThresholds,
label: { label: {
show: showThresholdLabels, show: showThresholdLabels,
margin: thresholdMarkersWidth + 1, margin: thresholdMarkersWidth + 1,