Skip to content
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

refactor: scales #1410

Merged
merged 151 commits into from
Oct 5, 2021
Merged

refactor: scales #1410

merged 151 commits into from
Oct 5, 2021

Conversation

monfera
Copy link
Contributor

@monfera monfera commented Sep 25, 2021

Summary

Refactoring toward making scales more independent of a preconceived tick count, so the future version of getAvailableTicks can try various tick counts, and possibly even extend the domain. This is needed by both the hierarchical time axis and the solution for avoiding duplicate or misplaced ticks. Miscellaneous additional simplifications

Details

  • Scale.scale() and Scale.pureScale() always return a number instead of the union type (emittingNaN instead of null and downstream checks as before but with Number.isNaN, Number.isFinite, scale(val) || 0 etc. so no behavioral change is expected)
  • removed the undocumented sortSeriesBy which wasn't exercised by unit tests, VRT or Kibana, yet its former presence required multiple @ts-ignores which are now also removed
  • solved four type assertions
  • replaced all the isNaNs with clearer alternatives (it coerces, eg. isNaN(null) and isNaN("42") yield false)
  • BREAKING: renamed showOverlappingTicks to ticksForCulledLabels
  • BREAKING: LogScaleOptions.logBase is now a number instead of the object enum LogBase; duplicate enum->number mappers removed too. Shorter, and allows other bases (octal, hex, 1024 etc.)
  • BREAKING potentially: (not through an API change though): discontinued scaleOrThrow and all other ...orThrow functions, now just handling certain NaN/non-finiteness cases, except for geom string generation. These used to silently swallow errors in past (hypothetical? IRL experienced?) cases where some D3 scale were to return a non-finite value, or even, throw an error, so geoms silently don't show up. The new behavior is that if the D3 scale were to throw, there's an explicit, clear error, and if there's a NaN or similar, then the geom path will have it, and an explicit console log line will be written. There are pros&cons so this may be an item up for debate, as the nature and type of our defensiveness, and swallow vs. expose errors are concerned
  • reworked some functions which were shallowly but verbosely wrapped in selectors
  • reworked ScaleContinuous, for example, the constuctor is split to tick count independent part vs tick count dependent part
  • combined getAvailableTicks and getVisibleTicks into a new getVisibleTicks (for this reason, unit tests that exercised these separately are currently commented out)
  • renamed some functions
  • removed syllogisms (where one of the ternary branches is a simple true or false, the ternary is superfluous)
  • avoid object creation, code noise and line inflation via changing the signature of mergePartial
  • numerous other code simplifications (esp. in axis_utils), type updates (fewer optional parameters, fewer union types etc.) and minor fixes (eg. non-integer desired tick count)

I also had to backtrack on (stash) more ambitious changes for now, such as improving the scale typing, I timeboxed it and didn't reach the goal in time

Issues

Checklist

  • The proper chart type label has been added (e.g. :xy, :partition)
  • The proper feature labels have been added (e.g. :interactions, :axis)
  • The :theme label has been added and the @elastic/eui-design team has been pinged when there are Theme API changes
  • All related issues have been linked (i.e. closes #123, fixes #123)
  • New public API exports have been added to packages/charts/src/index.ts
  • Unit tests have been added or updated to match the most common scenarios
  • The proper documentation and/or storybook story has been added or updated
  • The code has been checked for cross-browser compatibility (Chrome, Firefox, Safari, Edge)

@monfera monfera marked this pull request as draft September 25, 2021 11:58
@monfera monfera changed the title tmp refactor: scales Sep 25, 2021
Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've gone through all the code changes with the exceptions of the scale_continuous.ts. I need to dig a little deeper into that file and start playing around with storybook.

Copy link
Member

@markov00 markov00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, really a lot of changes, but I see the benefits on returning always a number from the scales and all the clean up makes a lot of sense!
Thanks Robert for gardening our code so well

image

@@ -128,7 +126,7 @@ export function computeSeriesDomains(
// fill series with missing x values
const filledDataSeries = fillSeries(dataSeries, xValues, xDomain.type);

const seriesSortFn = getRenderingCompareFn(sortSeriesBy, (a: SeriesIdentifier, b: SeriesIdentifier) => {
const seriesSortFn = getRenderingCompareFn(void 0, (a: SeriesIdentifier, b: SeriesIdentifier) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remember to grep all void 0

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or enable no-void eslint rule

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there was no current demand for retaining sortSeriesBy, its removal got completed: the void 0s are removed altogether f2f27e9

Copy link
Contributor Author

@monfera monfera Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added ec90371 which turns voids into warnings. It's legit JS and there's no fundamental reason for avoiding it, other than maybe uniform greppability, which isn't very valuable as undefined values can legitimately generated in all kinds of ways like optionals, so not sure when we'd grep for undefined. So, on the fence about discouraging perfectly good JS

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea to me is to prevent uncommon javascript syntax, so when 3rd party contributions come in they are not confused. I bet you 1 in 5 javascript engineers know what void 0 is. If statements are also perfectly fine JS but yet this PR removes dozens 😉, just saying....

packages/charts/src/mocks/series/series.ts Show resolved Hide resolved
Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still waiting on some file changes, lmk when you want me to take a look.

@monfera
Copy link
Contributor Author

monfera commented Oct 5, 2021

Pls let's preformat our suggestions in our IDE, otherwise our hypersensitive CI will yield a bunch of big red crosses due to a couple of spaces 🤣 Also, we get the "red card" for incomplete test runs, which is awkward, b/c it disincentivizes the otherwise normal process of applying suggestions one after the other, such that there's no time for CO to complete

@monfera monfera requested a review from nickofthyme October 5, 2021 12:00
@monfera
Copy link
Contributor Author

monfera commented Oct 5, 2021

I ended up reverting the "chore: untangle continuous scales file" commit, because it has served its purpose (allowing side by side comparison) and I'd like to avoid merge conflicts in that code. Also, the new order is I think more in line with what Cartesian code seems to follow, which is, put the main thing (class or function) atop, and place most utilities and nuisance bits (looking at you, loong type description) below

Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New changes LGTM.

@monfera monfera merged commit a53a2ba into elastic:master Oct 5, 2021
@monfera
Copy link
Contributor Author

monfera commented Oct 5, 2021 via email

nickofthyme pushed a commit that referenced this pull request Oct 15, 2021
# [38.0.0](v37.0.0...v38.0.0) (2021-10-15)

### Bug Fixes

* **deps:** update dependency @elastic/eui to v39 ([#1422](#1422)) ([2ee97aa](2ee97aa))
* **goal:** reduce whitespace for circular charts ([#1413](#1413)) ([6517523](6517523))
* **interactions:** change allowBrushingLastHistogramBin to true ([#1396](#1396)) ([9fa9783](9fa9783))
* **xy:** remove wrongly represented null/missing values in tooltip ([#1415](#1415)) ([e5963a3](e5963a3)), closes [#1414](#1414)

### Code Refactoring

* scales ([#1410](#1410)) ([a53a2ba](a53a2ba))

### Features

* **scales:** add `LinearBinary` scale type ([#1389](#1389)) ([9f2e427](9f2e427))
* **xy:** adaptive tick raster ([#1420](#1420)) ([200577b](200577b))
* **xy:** apply the data value formatter to data values over bars ([#1419](#1419)) ([e673fc7](e673fc7))

### BREAKING CHANGES

* **interactions:** allowBrushingLastHistogramBucket renamed to allowBrushingLastHistogramBin on the Settings component defaults true and is only applied for histogram type charts
* LogScaleOptions.logBase` is now a `number` instead of the object enum `LogBase`. Some edge case data or configuration _might_, with a deemed low likelihood, lead to a situation where the earlier version would have silently not rendered a bar, line or point, while the new code doesn't `catch`, therefore throw an exception (see the last item). General risk of regressions due to the quantity of code changes (altogether 3.5k)
@nickofthyme
Copy link
Collaborator

🎉 This PR is included in version 38.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nickofthyme nickofthyme added the released Issue released publicly label Oct 15, 2021
@markov00 markov00 mentioned this pull request Jan 3, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Changes the external API types :axis Axis related issue code quality :heatmap Heatmap/Swimlane chart related issue :refactor Code refactor released Issue released publicly :xy Bar/Line/Area chart related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants