d3-axis icon indicating copy to clipboard operation
d3-axis copied to clipboard

axis.tickSize accepts a function

Open IPWright83 opened this issue 2 years ago • 7 comments

  • axis.tickSize accepts a function to allow changing the tickSize dynamically on a per tick basis - resolves #https://github.com/d3/d3-axis/issues/74

IPWright83 avatar Apr 06 '22 17:04 IPWright83

The aim behind this is to help support more customised axis such as, which I believe should now be possible without having to rely on CSS nth selectors.

image

IPWright83 avatar Apr 06 '22 17:04 IPWright83

Thanks for the comments @mbostock , committed all those suggestions

IPWright83 avatar Apr 06 '22 19:04 IPWright83

What do you think about positioning the tick labels relatively to the ticks? Currently if using the function, the ticks "crash" into the axis, and I'm not sure how to fix that.

Capture d’écran 2022-04-06 à 23 14 51

Should we not also allow tickPadding to be a function, and set it to be the result of the tickSize function + a constant padding (unless it's already been set)?

You can build on the demo notebook by forking it: https://observablehq.com/@d3/d3-axis-ticksize-function-84

To update the build, just run yarn rollup -c in the repo and replace the d3-axis.js file in the file attachments.

It would be great to add various examples making use the new feature.

Fil avatar Apr 06 '22 21:04 Fil

@Fil thanks, I think you'd mentioned on Slack how to setup an Observable but I hadn't had much time. Decided I could just whip this PR together last night off the cuff. Yeah I can certainly add the example that I'm trying to build.

I think you're right about tickPadding. I think we should default it to exactly what you've suggested. I'll be honest when I've been using it, I've been disabling/moving labels too - which might be something useful for the future.

IPWright83 avatar Apr 07 '22 06:04 IPWright83

Sorry, taken me a little while to get back to this. I've not added tickPadding as a function, as I couldn't foresee when you'd need to do that (and it started requiring nested ternaries which I'm not a fan of). But I've updated tickPadding to take the tickSizeFunction into account @Fil.

I've forked your example and add my use case in there (just showing major tick labels) https://observablehq.com/@ipwright83/d3-axis-ticksize-function-84 and thought of a crazy use case where you might want to move "longer labels" to prevent overlap.

IPWright83 avatar Apr 25 '22 08:04 IPWright83

Sorry to message you direct @mbostock, but would love to be able to use this if possible without maintaining a fork.

Is there anything you'd like to change to get this in? Thanks!

IPWright83 avatar Jul 03 '22 18:07 IPWright83

@mbostock This looks like a great PR. Any way we can help get this accepted?

denisemauldin avatar Sep 15 '22 16:09 denisemauldin