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

No padding between rotated x-axis labels #2750

Closed
airblade opened this issue Jun 10, 2016 · 9 comments
Closed

No padding between rotated x-axis labels #2750

airblade opened this issue Jun 10, 2016 · 9 comments

Comments

@airblade
Copy link

Hello! I'm using Chart.js v2.1.4. Thank you for the great code :)

I have a bar chart that looks like this:

screen shot 2016-06-10 at 11 29 29 am

The x-axis labels are hard to read because there is just about no padding between them.

Here is the same chart produced by Chart.js v1.0.2:

screen shot 2016-06-10 at 11 31 06 am

The labels are easier to read because they are rotated more to give more padding.

Is the v2.1.4 behaviour expected? How can I have the labels rotated more so they are easier to read?

I tried adding 15° rotation to the labels like this:

var options = {
    scales: {
        xAxes: [{
            afterCalculateTickRotation: function(scale) { scale.labelRotation += 15; };
        }],
    }
};

This worked but of course it always adds extra rotation so it looks silly on a chart with only a few bars where the labels don't need to be rotated at all.

Many thanks!

@airblade
Copy link
Author

airblade commented Jun 10, 2016

P.S. I also noticed the bars are wider in v2.1.4 than in v1.0.2. Is there a way to control the width in v2.1.4?

Update: never mind, I found the categoryPercentage and barPercentage options.

@etimberg
Copy link
Member

@airblade you could try using the minRotation option but it's going to force the rotation to be that amount always.

options: {
  scales: {
    xAxes: [{
      ticks: {
        minRotation: 45 // angle in degrees
      }
    }]
  }
}

You could also try a different implementation of afterCalculateTickRotation

var options = {
    scales: {
        xAxes: [{
            afterCalculateTickRotation: function(scale) { 
              if (scale.labelRotation !== 0) {
                scale.labelRotation += 15; 
              }
            }
        }],
    }
};

@airblade
Copy link
Author

I prefer your second suggestion though, like my original workaround, it doesn't address the actual spacing between labels.

Am I right in thinking that's controlled by the conditionals in this loop?

while (labelWidth > tickWidth && this.labelRotation < optionTicks.maxRotation) {

Would an amendment like this be possible?

while (labelWidth > tickWidth + 6 && this.labelRotation < optionTicks.maxRotation) {

@etimberg
Copy link
Member

Looks like the line where the tickWidth is calculated has an error: https://github.com/chartjs/Chart.js/blob/v2.1.4/src/core/core.scale.js#L219

I think the -6 should be +6

@airblade
Copy link
Author

I'm not familiar with the code but, going by the code comment, I think you might be right.

By the way, where do the 2 lots of 3px padding apply? Is it above and below the rotated label, at right angles to the rotated baseline?

@etimberg
Copy link
Member

It looks like the 3px are added to the width, so it would be along the rotated baseline. We should probably add a configurable padding that is perepndicular to the baseline

@madmoizo
Copy link

+1 for label padding
Is there a way to avoid this kind of render in 2.5 ?
image

@Arkemlar
Copy link

Arkemlar commented Jun 8, 2017

+1 for label padding, even when it is rotated to 90* - it is still hard to read when there are a lot of data lines:
image

@kurkle
Copy link
Member

kurkle commented Jan 25, 2019

Closing as this issue has been in #5938 and/or #5922

@kurkle kurkle closed this as completed Jan 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants