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

Adding invert method to ordinal scale #1046

Merged
merged 14 commits into from
Nov 27, 2018

Conversation

bognikol
Copy link
Contributor

Ordinal scale does not have 'invert' method implemented in d3-scale, thus we are not able to zoom charts with ordinal axes using Highlight component.

I opened a pull request to d3-scale (d3/d3-scale#151); however as d3-scale is slow moving I had to implement temporary workaround.

@CLAassistant
Copy link

CLAassistant commented Nov 21, 2018

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@mcnuttandrew mcnuttandrew left a comment

Choose a reason for hiding this comment

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

🎉!!!
This is wonderful! Thanks for doing this. I have a bunch of code style comments, but substantively this looks great. You're a champ

scale.invert = function invert(value) {
let domainIndex = 0;
const n = scale.domain().length;
const reverse = scale.range()[1] < scale.range()[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe cleaner to pull the start/end values out rather than to keep calling range()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not completely sure what do you mean here; we need to figure out whether range is ascending or descending in order to see what is 'start' (lower range) and 'end' (higher range).

Copy link
Contributor

Choose a reason for hiding this comment

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

you call scale.range() fourish times, it might be better to do something like:

const [lower, upper] = scale.range();
const start = Math.min(lower, upper);
const end = Math.max(lower, upper);

That way there is less index manipulation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

const start = scale.range()[reverse - 0];
const stop = scale.range()[1 - reverse];

if (value < start + scale.padding() * scale.step()) domainIndex = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty surprised that lint isn't complaining about this, but we prefer using explicit curly braces on if statements.

else if (value > stop - scale.padding() * scale.step()) domainIndex = n - 1;
else domainIndex = Math.floor((value - start - scale.padding() * scale.step()) / scale.step());

return scale.domain()[domainIndex];
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than updating the domainIndex in each of the branches, why don't you just return the scale.domain()[VALUE] from one of the arms of the conditional?

@@ -505,6 +505,22 @@ test('scales-utils #getScaleFnFromScaleObject', t => {
[-1, 1],
'should build a generic domain that reflects about zero'
);

const ordinalScale = getScaleFnFromScaleObject({
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add a test for this that also capture padding? or does this do that already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are already testing padding of 0.5 (which is padding we use in react-vis).
This is so-called 'outer' padding (padding before first bar and after last bar).
Inner padding is set to 1.0 in d3-scale, as we are really using 'point-scale' from d3-scale.

Copy link
Contributor

Choose a reason for hiding this comment

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

👌

*/

function addInvertFunctionToOrdinalScaleObject(scale) {
if (scale.invert) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

curly brace here too?

Copy link
Contributor

@mcnuttandrew mcnuttandrew left a comment

Choose a reason for hiding this comment

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

Thanks again for doing this!

@mcnuttandrew mcnuttandrew merged commit 322ff24 into uber:master Nov 27, 2018
@bognikol
Copy link
Contributor Author

bognikol commented Nov 29, 2018

Hey, no problem :-) Thanks for your help!
(Sorry for my archaic style!)

PS. I closed #987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants