fix(scales): use bisect to handle invertWithStep#200
fix(scales): use bisect to handle invertWithStep#200markov00 merged 4 commits intoelastic:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #200 +/- ##
=========================================
Coverage ? 97.09%
=========================================
Files ? 35
Lines ? 1862
Branches ? 256
=========================================
Hits ? 1808
Misses ? 47
Partials ? 7
Continue to review full report at Codecov.
|
emmacunningham
left a comment
There was a problem hiding this comment.
tested locally & code lgtm, just a couple of minor comments about types
| }); | ||
| test('check if a scale is log scale', () => { | ||
| const domain = [0, 2]; | ||
| const domain: any[] = [0, 2]; |
There was a problem hiding this comment.
I think we have a Domain type that could be used here.
src/state/crosshair_utils.ts
Outdated
| const isHorizontalRotated = isHorizontalRotation(chartRotation); | ||
| const snappedPosition = getSnapPosition( | ||
| xScale.invertWithStep(isHorizontalRotated ? x : y), | ||
| xScale.invertWithStep(isHorizontalRotated ? x : y, data), |
There was a problem hiding this comment.
Since invertWithStep may return undefined for continuous scales, do we need to update the type signature for getSnapPosition? Currently getSnapPosition defines value: string | number for its first parameter, but if the scale is a continuous scale, it's possible that the return value of invertWithStep is undefined. (I think we already handle if the scaled value is undefined, so the implementation doesn't need to change but rather just the type signature to more accurately reflect the possible values) Further, should we tighten the return type of invertWithStep in the Scale interface? Currently it is defined as (value: number, data: any[]) => any but getSnapPosition expects string | number.
There was a problem hiding this comment.
As in its Scale parent class, invertWithScale return type is any because at runtime we can be never sure that the data object passed to the invertWithScale function contains only numbers. I've add few more checks on d42bf42 for getCursorBandPosition and for invertWithStep to avoid problems with undefined/null values. We should however check all the scales and the data types to have a correct handling of null/undefined values
There was a problem hiding this comment.
👍 we could decode runtime values with something like io-ts but for now manually validating the data types within these functions works
Codecov Report
@@ Coverage Diff @@
## master #200 +/- ##
=========================================
Coverage ? 96.99%
=========================================
Files ? 35
Lines ? 1865
Branches ? 256
=========================================
Hits ? 1809
Misses ? 49
Partials ? 7
Continue to review full report at Codecov.
|
|
🎉 This PR is included in version 4.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
## [4.0.1](elastic/elastic-charts@v4.0.0...v4.0.1) (2019-05-02) ### Bug Fixes * **scales:** use bisect to handle invertWithStep ([opensearch-project#200](elastic/elastic-charts#200)) ([d988e8b](elastic/elastic-charts@d988e8b)), closes [opensearch-project#195](elastic/elastic-charts#195) [opensearch-project#183](elastic/elastic-charts#183)
Summary
close #195
close #183
We actually relay on the fact that data is evenly distributed among our x axis. We also rely on the
minIntervalvalue (the minimum interval between two consecutive x values on your dataset) to determine the current interval between x values.Those wrong assumptions leads to issues when interacting with a chart: the interaction invert the pixel coordinate of the mouse to a scale value: the
invertWithStepfunction to get the exactxvalue of the dataset for a bar chart with linear x axis, using the minInterval and the bandwidth to compute it. If we have odd placed x values or a domain that is larger than the current max x value, the tooltip and the highlighter are misaligned and wrongly displayed as in the closing issues #195 #183Instead of relying on the minInterval, I've refactored the code to use a
bisectfunction oninvertWithStep, to get back the right x value from the chart.I've also pixel adjusted few tests, that use the previous
invertWithStepfunction.Checklist
Use
strikethroughsto remove checklist items you don't feel are applicable to this PR.src/index.ts(and stories only import from../srcexcept for test data & storybook)