Skip to content

Commit

Permalink
Fix all comments
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Jan 21, 2022
1 parent 9e0ddd9 commit fc02977
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/plugins/charts/public/services/palettes/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ function findColorSegment(

// what about values in range
const index = colors.findIndex((c, i) => comparison(value, rangeMin + (1 + i) * step) <= 0);
// see comment below in function 'findColorsByStops'
return (
colors[index] ??
(value > rangeMin + colors.length * step ? colors[colors.length - 1] : colors[0])
(value >= rangeMin + colors.length * step ? colors[colors.length - 1] : colors[0])
);
}

Expand All @@ -34,7 +35,14 @@ function findColorsByStops(
stops: number[]
) {
const index = stops.findIndex((s) => comparison(value, s) < 0);
return colors[index] ?? (value > stops[stops.length - 1] ? colors[colors.length - 1] : colors[0]);
// as we now we can provide 'rangeMax' as end for last interval (iterval [lastStop, rangeMax]),
// value can be more that last stop but will be valid
// because of this we should provide for that value the last color.
// (For example, value = 100, last stop = 80, rangeMax = 120, before we was return the first color,
// but now we will return the last one)
return (
colors[index] ?? (value >= stops[stops.length - 1] ? colors[colors.length - 1] : colors[0])
);
}

function getNormalizedValueByRange(
Expand Down
21 changes: 14 additions & 7 deletions x-pack/plugins/lens/public/metric_visualization/expression.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ function getColorStyling(
const cssProp = colorMode === ColorMode.Background ? 'backgroundColor' : 'color';
let rawIndex = stops.findIndex((v) => v > value);

if (!isFinite(rangeMax) && value > stops[stops.length - 1]) {
rawIndex = stops.length - 1;
}

// in this case first stop is -Infinity
if (!isFinite(rangeMin) && value < stops[1]) {
rawIndex = 0;
// we should check it only if we don't find color index
if (rawIndex !== -1) {
if (!isFinite(rangeMax) && value > stops[stops.length - 1]) {
rawIndex = stops.length - 1;
}

// in this case first stop is -Infinity
if (
!isFinite(rangeMin) &&
value < (isFinite(stops[0]) ? stops[0] : stops[1])
) {
rawIndex = 0;
}
}


const colorIndex = rawIndex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export const distributeEqually = (
const getValueForIndex = (index: number) => roundValue(min + step * index);
const getStartValue = (index: number) => {
if (index === 0) {
return checkIsMinContinuity(continuity) ? Number.NEGATIVE_INFINITY : min;
return checkIsMinContinuity(continuity) ? Number.NEGATIVE_INFINITY : roundValue(min);
}
return getValueForIndex(index);
};
const getEndValue = (index: number) => {
if (index === lastIndex) {
return checkIsMaxContinuity(continuity) ? Number.POSITIVE_INFINITY : max;
return checkIsMaxContinuity(continuity) ? Number.POSITIVE_INFINITY : roundValue(max);
}
return getValueForIndex(index + 1);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ export function changeColorPalette(
reverse: false, // restore the reverse flag
};

const newColorStops = getColorStops(palettes, [], activePalette, dataBounds);
// we should pass colorStops so that correct calculate new color stops (if there was before) for custom palette
const newColorStops = getColorStops(
palettes,
activePalette.params?.colorStops || [],
activePalette,
dataBounds
);

if (isNewPaletteCustom) {
newParams.colorStops = newColorStops;
Expand Down

0 comments on commit fc02977

Please sign in to comment.