-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(heatmap): improve legend item (#1317)
This fixed duplicated legend items in the heatmap legend, refactoring the current API and logic for handling color bands
- Loading branch information
Showing
20 changed files
with
152 additions
and
197 deletions.
There are no files selected for viewing
Binary file modified
BIN
-2.74 KB
(94%)
...ual-tests-for-all-stories-heatmap-alpha-basic-visually-looks-correct-1-snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+5.04 KB
(110%)
...sts-for-all-stories-heatmap-alpha-categorical-visually-looks-correct-1-snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+5.24 KB
(160%)
...sual-tests-for-all-stories-heatmap-alpha-time-visually-looks-correct-1-snap.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed
BIN
-53.7 KB
...tests/__image_snapshots__/heatmap-test-ts-heatmap-color-scale-linear-1-snap.png
Binary file not shown.
Binary file removed
BIN
-41.8 KB
...sts/__image_snapshots__/heatmap-test-ts-heatmap-color-scale-quantile-1-snap.png
Binary file not shown.
Binary file removed
BIN
-40.9 KB
...sts/__image_snapshots__/heatmap-test-ts-heatmap-color-scale-quantize-1-snap.png
Binary file not shown.
Binary file removed
BIN
-40 KB
...ts/__image_snapshots__/heatmap-test-ts-heatmap-color-scale-threshold-1-snap.png
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/charts/src/chart_types/heatmap/scales/band_color_scale.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getPredicateFn } from '../../../common/predicate'; | ||
import { Color } from '../../../utils/common'; | ||
import { ColorBand, HeatmapBandsColorScale } from '../specs/heatmap'; | ||
import { ColorScale } from '../state/selectors/get_color_scale'; | ||
|
||
const TRANSPARENT_COLOR: Color = 'rgba(0, 0, 0, 0)'; | ||
|
||
function defaultColorBandFormatter(start: number, end: number) { | ||
const finiteStart = Number.isFinite(start); | ||
const finiteEnd = Number.isFinite(end); | ||
return !finiteStart && finiteEnd ? `< ${end}` : finiteStart && !finiteEnd ? `≥ ${start}` : `${start} - ${end}`; | ||
} | ||
|
||
/** @internal */ | ||
export function getBandsColorScale( | ||
colorScale: HeatmapBandsColorScale, | ||
): { scale: ColorScale; bands: Required<ColorBand>[] } { | ||
const labelFormatter = colorScale.labelFormatter ?? defaultColorBandFormatter; | ||
const ascendingSortFn = getPredicateFn('numAsc', 'start'); | ||
const bands = colorScale.bands | ||
.reduce<Required<ColorBand>[]>((acc, { start, end, color, label }) => { | ||
// admit only proper bands | ||
if (start < end) acc.push({ start, end, color, label: label ?? labelFormatter(start, end) }); | ||
return acc; | ||
}, []) | ||
.sort(ascendingSortFn); | ||
|
||
const scale = getBandScale(bands); | ||
return { scale, bands }; | ||
} | ||
|
||
function getBandScale(bands: ColorBand[]): ColorScale { | ||
return (value) => { | ||
for (let i = 0; i < bands.length; i++) { | ||
const { start, end, color } = bands[i]; | ||
if (start <= value && value < end) { | ||
return color; | ||
} | ||
} | ||
return TRANSPARENT_COLOR; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.