-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: improved heatmap legend #1317
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
packages/charts/package.json
Outdated
@@ -1,7 +1,7 @@ | |||
{ | |||
"name": "@elastic/charts", | |||
"description": "Elastic-Charts data visualization library", | |||
"version": "34.0.0", | |||
"version": "34.0.0a", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"version": "34.0.0a", | |
"version": "34.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done 7a85eef
storybook/stories/heatmap/3_time.tsx
Outdated
bands: [ | ||
{ color: '#ca0020', start: -5, end: -3 }, | ||
{ color: '#f4a582', start: -3, end: -1 }, | ||
{ color: 'rgb(206,206,206)', start: -1, end: 1 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⬛ 🐑
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done 2fb4d76
// filter out wrongly configured bands | ||
if (start > end) { | ||
return acc; | ||
} | ||
acc.push({ start, end, color, label: label ?? labelFormatter(start, end) }); | ||
return acc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of the double return, would like the positive
// admit only proper bands
if(start < end) acc.push({ start, end, color, label: label ?? labelFormatter(start, end) });
return acc;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done 2fb4d76
const scale = getBandScale(bands); | ||
return { scale, bands }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of cramming in the scale too, thus turning a simpler return type into a deeper tuple (object), the bands
could be computed separately where getBandsColorScale
is also used (lower coupling)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
linking your next comment #1317 (comment)
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; | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great for this PR and it's unlikely that there'll be hundreds of bands, though we could at some point harness our monotonicHillClimb
(or derivative) for O(log n) as well as avoidance of a hand rolled loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep definitely, this should anyway be exported, improved and reused as band/categorical scale if we want
export const getColorScale = createCustomCachedSelector([getHeatmapSpecSelector], (spec): { | ||
scale: ColorScale; | ||
bands: Required<ColorBand>[]; | ||
} => getBandsColorScale(spec.colorScale)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay you handle these together in the selector, that's why you wanted to bundle them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, also tried with edge cases: 1 band, 0 bands, 1 band but wrong start-end order
And with incomplete coverage, eg. gap or leaving the lowest or highest part of the domain uncovered with legends
# [34.1.0](v34.0.0...v34.1.0) (2021-08-19) ### Bug Fixes * **goal:** tooltip actual color ([#1302](#1302)) ([dbe9d36](dbe9d36)) * **heatmap:** improve legend item ([#1317](#1317)) ([49c35ce](49c35ce)) * **legend:** no truncation with single value ([#1316](#1316)) ([7ec8a9f](7ec8a9f)) ### Features * **goal:** optional target tick ([#1301](#1301)) ([88adf22](88adf22))
🎉 This PR is included in version 34.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
The heatmap legend now shows the full ranges configured, to improve its readability when using a categorical/band scale.
BREAKING CHANGE
The
<Heatmap>
API is changed: the color scale configuration is self-contained into thecolorScale
prop. Theranges
andcolors
are now described asColorBand
objects, where acolor
is associated with astart
andend
values.The heatmap is currently marked as
@alpha
so the breaking change will not be propagated as a major version bump.Details
colors
,ranges
andcolorScale
props are now replaced byChecklist
packages/charts/src/index.ts
(and stories only import from../src
except for test data & storybook)