nivo icon indicating copy to clipboard operation
nivo copied to clipboard

Dynamically Adjust Items Shown in AxisBottom based on width of chart

Open robd003 opened this issue 2 years ago • 4 comments

Does anyone know of a way to dynamically adjust the number of items shown in AxisBottom depending on the width of the chart?

Here's an example of a chart stretched out wider than most displays and you can see every tick: Screen Shot 2022-05-16 at 3 12 28 PM

Here's an example of a chart at a "normal browser width" and the X-axis is unreadable: Screen Shot 2022-05-16 at 3 12 17 PM

I'm aware of the trick described in ticket https://github.com/plouc/nivo/issues/524#issuecomment-628296620 where you can use formatter to omit values, but is there a way to have this vary depending on the width of the chart?

Honestly I wish Nivo would just automatically show the appropriate number of items in the X-axis.

robd003 avatar May 16 '22 19:05 robd003

Could it be that the chart in this example is using a categorical axis rather than a continuous one?

On a categorical axis, elements are independent entities and their positions along the axis are either accidental or a design choice. Omiting any of the labels risks losing meaning, so the chart draws all the labels even if that makes the text fragments overlap.

In the example images, it seems the values on the axis are dates. If you can turn the scale into a time scale, there will be more flexibility with the ticks on the axis. Setting tickValues: 5, for example, should give a small set of labels that fit without overlap.

See here for sample code.

Admittedly, labels could still get squeezed if the display becomes even narrower.

tkonopka avatar May 17 '22 06:05 tkonopka

This is an essential part of the chart. There should be more focus on implementing dynamic labels that do not overlap.

alex20044 avatar May 24 '22 08:05 alex20044

Hi @alex20044. There are already several ways to customize the tick labels. See here for an example. Note how there are more data points on the lines than labels on the axis, and how the labels are well-spaced and legible.

The same approach as in the linked example can probably apply to the data in the screenshots. The key point is to set the horizontal scale to a continuous time scale (not a categorical scale). After that, my guess would be to try tickValues: 5 to set the labels. That should suit both the narrow and the wide formats. For charts that are much smaller than in the screenshot, you can also try to display tick labels at an angle with tickRotation (in degrees) and tickPadding (in pixels).

tkonopka avatar May 24 '22 15:05 tkonopka

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar Sep 20 '22 16:09 stale[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

stale[bot] avatar Sep 27 '22 19:09 stale[bot]

There are already several ways to customize the tick label

@tkonopka Unfortunately, this works in this example because the time scale is supported for this type of chart. Support for any form of responsible labels on the bottom axis is sorely lacking on bar charts for example.

sebastienbarre avatar Dec 23 '23 08:12 sebastienbarre