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

[question] About ResponsiveChartContainer, is there a way to get staggered tick labels on the axis. #16433

Open
ZaidLinkEZ opened this issue Feb 3, 2025 · 4 comments
Assignees
Labels
component: charts This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature support: commercial Support request from paid users support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/

Comments

@ZaidLinkEZ
Copy link

ZaidLinkEZ commented Feb 3, 2025

The problem in depth

I want to get staggered tick labels on my ResponsiveChartContainer, mainly on bar charts. Check the following image for reference.

Image

Your environment

`npx @mui/envinfo`
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: x-charts, tick labels, axis labels, ticks

Order ID: 95670

@ZaidLinkEZ ZaidLinkEZ added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels Feb 3, 2025
@github-actions github-actions bot added component: charts This is the name of the generic UI component, not the React module! support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/ labels Feb 3, 2025
@alexfauquette alexfauquette added enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 3, 2025
@alexfauquette
Copy link
Member

That's not currently supported. The solution would be to create your custom x-axis component.

The code of our x-axis is here, but it can be simplified a lot if you only plan to support bar chart.
https://github.com/mui/mui-x/blob/master/packages/x-charts/src/ChartsXAxis/ChartsXAxis.tsx

Something that is not clear for me is if in your usecase the axis label should always be staggered or if it should only occurs when labels are to close from each over

@ZaidLinkEZ
Copy link
Author

That's not currently supported. The solution would be to create your custom x-axis component.

The code of our x-axis is here, but it can be simplified a lot if you only plan to support bar chart. https://github.com/mui/mui-x/blob/master/packages/x-charts/src/ChartsXAxis/ChartsXAxis.tsx

Something that is not clear for me is if in your usecase the axis label should always be staggered or if it should only occurs when labels are to close from each over

Okay, for me the usecase is basically we want the staggering to be controllable from server side, and I control it using a flag. Multiple instances of the charts exist and some having longer tick labels. Thus I want to force the tick labels and also make them staggered

@alexfauquette alexfauquette self-assigned this Feb 3, 2025
@JCQuintas
Copy link
Member

@ZaidLinkEZ a quick solution is to use an axis value formatter like this:

const axisFormatter = () => {
  let count = 0;
  return (value: string, context: AxisValueFormatterContext) => {
    count++;
    return context.location === "tick"
      ? `${count % 2 === 0 ? " \n" : ""}${value.slice(0, 3)} \n2023`
      : `${value} 2023`;
  };
};

the " \n" uses a NO-BREAK SPACE.

And you can see it in action here: https://codesandbox.io/p/sandbox/intelligent-mopsa-ygwjrd

@ZaidLinkEZ
Copy link
Author

Okay, this is a pretty cool workaround. Thanks a lot for this @JCQuintas , also let me know if there will be a possibility of supporting these staggered tick label feature in the future, or this will be it. Anyways, thanks again for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature support: commercial Support request from paid users support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/
Projects
None yet
Development

No branches or pull requests

3 participants