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

Is is possible to prevent labels overlapping when using FixedScaleAxis together with ticks option? #1271

Open
wujekbogdan opened this issue Oct 21, 2020 · 1 comment
Labels

Comments

@wujekbogdan
Copy link

wujekbogdan commented Oct 21, 2020

I use FixedScaleAxis together with divisor (I've written some code that determines the optimal divisor value based on labels length and chart container width). This works very well when the number of data points is greater than the number of labels.

But this solution gives confusing results when the number of data points is lower than the number of labels. In such case, I would rather like the labels count to be the same as the number of data points and also I'd like them to be rendered directly under data points. So in such a scenario, I don't use divisor and instead I define ticks explicitly:

const getXAxisOptions = () => {
  const dataSeriesPointsCount = this.series.reduce((currentMax, { data }) => {
    return Math.max(currentMax, data.length);
  }, 0);

  const baseOptions = {
    type: Chartist.FixedScaleAxis,
    high: this.axesBounds.axisX.high,
    low: this.axesBounds.axisX.low,
    labelInterpolationFnc,
  };

  if (dataSeriesPointsCount < this.xAxisDivisor) {
    const ticks = this.series.reduce(
      (acc, { data }) => [...acc, ...data.map(({ x }) => x)],
      []
    );

    return {
      ...baseOptions,
      ticks,
    };
  }

  return {
    ...baseOptions,
    divisor: this.xAxisDivisor,
  };
};

At first glance it works perfectly:

Zrzut ekranu 2020-10-21 o 14 11 38

But unfortunately, in some edge cases, when data points are very close to each other then labels are rendered one over another:

Zrzut ekranu 2020-10-21 o 14 12 38

Is there any way to prevent labels from overlapping? Or do I need to implement my own logic that will exclude overlapping labels from ticks array?

@Arantiryo
Copy link
Collaborator

Hello @wujekbogdan 👋

Chartist has recently been updated to version 1.0 with some new exciting changes like ESM and TypeScript support. The migration guide can be found right here.

Please let me know if the issue is still relevant to you so we can further investigate it!

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

No branches or pull requests

2 participants