Chart.js: Time axes creates overlapping labels when data doesn't match standard interval

Created on 14 Jun 2016  ·  13Comments  ·  Source: chartjs/Chart.js

With Chart.js v2.1.5 and a time axes, my last two x-axis tick labels always overlap, causing a jumble of text. My data points are every five minutes, but they don't start exactly on 00, 05, etc.

image
(see bottom right)

Code and data demonstrating this issue:
https://jsfiddle.net/w37nvpju/

Thanks!

time scale bug

All 13 comments

Any developments here? I'm having the same issue.

I'm running into this issue as well, I think there is a bug in how the time scale selects a max tick if you don't define one yourself. See this solution that adds time.max to fix the issue:

https://jsfiddle.net/sbdfucjb/1/

I'm currently working on a solution to this. Here is the suspect code: https://github.com/chartjs/Chart.js/blob/0c07b1eeda07c9fc648c5012dbd2c4ad510bf0fc/src/scales/scale.time.js#L240-L247

This code was changed in #3064. Maybe it could have fixed this too.

Hey, I've built master (as at 8a161a3bef706060afd9b28cf5d60313486bf964) and tried it with my original code. Unfortunately #3064 doesn't seem to change the overlapping behaviour for my case.

image

I've been able to get by thanks to @freewil's suggestion:

# maximum_timestamp = moment.max [ ... ]
x_axis_max = if maximum_timestamp.minutes() <= 30
    maximum_timestamp.startOf 'hour'
  else
    maximum_timestamp.endOf('hour').add(1, 'second')
scales: {
  xAxes: [{
    type: 'time',
    display: true,
    time: {
      max: x_axis_max
    }
  }],
}

It's a bit of a hack though, and very particular to my data, but forces the axis to render like this...

If the last point is 21:05:21:
image

If I add 30 minutes to everything, to make the last point 21:35:21:
image

My last charting library, Flot, handled this data very well — just trying to wrap my head around how it figures out the ticks to see if a similar approach could help.

Thanks for the help so far!

@mcoms I think flot (from what little I know about it's internal structure) just keeps the times as unix seconds and then it becomes more straightforward. Maybe we should do the same and only use moment for the parsing and display sections

Unforuntately the max hack does not work so well, since it starts cutting off my data toward the end of the X axis.

Imgur

I'm surprised this isn't higher priority to people considering this was opened over a year ago now. I'm new to the library but going to check this out today as it's currently ruining a demo. :(

@buddyp450 I think it's been fixed by #4556 (updated reported fiddle). Can you please try this development build and confirm if it fixed the issue for you?

@simonbrunel looks good! The decision being made on what the interval between ticks should be seems a little odd but I'm willing to bet I'm just missing an api option to specify it. Thanks, eagerly awaiting 2.7 release.

We are currently working on that interval between ticks, so any feedback or ideas to make it better are really welcome: what are your thoughts?

Fixed by #4556, closing.

Is there any way to remove space between ticks when there is no data(or data value is zero/NULL) for a long time?

Was this page helpful?
0 / 5 - 0 ratings