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

Add the ability to round bar index scale #1282

Merged
merged 11 commits into from
Nov 18, 2020

Conversation

ericsoco
Copy link
Contributor

I noticed that ResponsiveBar charts with many, narrow bars were varying in width. The svg container was the correct width, but the bars within it were offset from the left/right edges.

Screencap:
nivo-bar-nicing

After some digging, I narrowed it down to the use of d3-scale.scaleBand.rangeRound() within Bar's common.js::getIndexedScale(). This diff puts this nicing behavior behind a nice prop, which defaults to true to maintain current functionality unless a developer opts out with nice=false.

The above test with nice=false:
nivo-bar-nicing-off

I didn't add anything to the Bar demo site; I can if you like, but this prop feels niche / edge-case enough that it may not need to be surfaced in the demo site.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 16, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 785cec5:

Sandbox Source
nivo Configuration
nivo-website Configuration

@plouc
Copy link
Owner

plouc commented Nov 16, 2020

@ericsoco seems interesting to have the option, however I'm not sure about the naming, having nice as a prop on the top level component is hard to figure out I think, maybe something like roundIndexScale? This should also be added to the TypeScript definitions, ideally I'd like to have everything documented, but the way the website works is not documented, so I can definitely understand if you don't want to :) It would also be nice to add a unit test for it.

@wyze
Copy link
Contributor

wyze commented Nov 16, 2020

I agree about the prop name. Something like indexScale={{ nice?: boolean, round?: boolean }} to be more flexible to accept future options if needed.

@ericsoco
Copy link
Contributor Author

@plouc -- I can look into added to TS defs and a unit test; can also take a look at adding to the website if it's not too complex!

Also, I realize now that nice is slightly different semantically than rangeRound (docs), so agreed on naming; can take @wyze's approach and refactor to indexScale={round}. I'll leave out "nice" for now, can add in the future if useful.

@plouc
Copy link
Owner

plouc commented Nov 17, 2020

Thank you @ericsoco! Yes @wyze approach is more flexible and align with the valueScale we already have.

@ericsoco ericsoco force-pushed the add-nice-prop-to-bar branch from eb5be3c to 1bf8cae Compare November 18, 2020 05:22
packages/scales/index.d.ts Outdated Show resolved Hide resolved
packages/scales/src/index.js Outdated Show resolved Hide resolved
packages/scales/src/indexedScale.js Outdated Show resolved Hide resolved
packages/bar/index.d.ts Outdated Show resolved Hide resolved
@plouc
Copy link
Owner

plouc commented Nov 18, 2020

@ericsoco Thank you for the adjustments, maybe what we suggested was confusing, the property should be indexScale, to be consistent with valueScale (we cannot really use x/y scale for names as the chart supports different layouts), but the scale type is a band scale.

@ericsoco
Copy link
Contributor Author

No worries, I can make these changes now (and fix the failing lint).

@ericsoco ericsoco requested a review from plouc November 18, 2020 06:07
packages/bar/src/compute/common.js Outdated Show resolved Hide resolved
packages/bar/src/compute/stacked.js Outdated Show resolved Hide resolved
packages/bar/src/props.js Outdated Show resolved Hide resolved
packages/bar/index.d.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@wyze wyze left a comment

Choose a reason for hiding this comment

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

Also need to update the canvas implementation to pass the value through to the generate bars methods or it will crash. I made the mistake with valueScale :O.

packages/bar/src/compute/common.js Outdated Show resolved Hide resolved
@ericsoco
Copy link
Contributor Author

ericsoco commented Nov 18, 2020

nivo-bar-indexScale

@ericsoco ericsoco requested review from wyze and plouc November 18, 2020 16:53
Copy link
Contributor

@wyze wyze left a comment

Choose a reason for hiding this comment

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

Code looks really good! https://dlqum.sse.codesandbox.io/bar/

Only minor change is I think we should change the website values to default to the default values of the component.

website/src/pages/bar/api.js Outdated Show resolved Hide resolved
website/src/pages/bar/canvas.js Outdated Show resolved Hide resolved
website/src/pages/bar/index.js Outdated Show resolved Hide resolved
@ericsoco ericsoco force-pushed the add-nice-prop-to-bar branch from 667781e to 785cec5 Compare November 18, 2020 17:36
@ericsoco
Copy link
Contributor Author

@wyze oof good catch! Fixed.

@ericsoco ericsoco requested a review from wyze November 18, 2020 17:39
Copy link
Contributor

@wyze wyze left a comment

Choose a reason for hiding this comment

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

Copy link
Owner

@plouc plouc left a comment

Choose a reason for hiding this comment

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

LGTM

@plouc plouc changed the title Add nice prop to bar Add round prop to bar Nov 18, 2020
@plouc plouc changed the title Add round prop to bar Add the ability to round bar index scale Nov 18, 2020
@plouc plouc merged commit 1ab1257 into plouc:master Nov 18, 2020
@plouc
Copy link
Owner

plouc commented Nov 18, 2020

Thank you @ericsoco!

@ericsoco
Copy link
Contributor Author

Thank you @plouc for the lovely library, and @wyze as well 😄

@jamesdh
Copy link
Contributor

jamesdh commented Jan 17, 2021

@ericsoco I'm looking back at this issue and I see your bar chart uses time series data for its X-axis but I don't see any way to do so without converting their value to a string (which causes every bar to have a tick). Can any one of you guys clarify how that was done?

@ericsoco
Copy link
Contributor Author

ericsoco commented Jan 17, 2021

@jamesdh I have string values for x-axis formatted as YYYY-MM, and I'm passing xFormat: 'time:%Y-%m' to ResponsiveBar.

I don't have a tick on every bar because I'm not using Nivo to render the x-axis labels / ticks, this is a hybrid component that functions as a slider (using Material-UI) with a bar chart overlaid.

I think what I just told you is probably not helpful for your use case...sorry!

ddavydov pushed a commit to netronixgroup/nivo that referenced this pull request Apr 8, 2021
* Rebase

* Fix lint

* Refactor from `nice` to `indexScale: {round}` per comments

* Add unit tests

* Add TypeScript defs

* Add `indexedScale` to website

* Rebase; update tests to match style

* Address comments

* Tiny typo fix

* Pick up more comments

* Correct website default values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📊 bar @nivo/bar package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants