question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Misaligned ticks for week-based time scale

See original GitHub issue

Hi,

I’m trying to plot weeks in a year to a line chart (see this codesandbox) My input data point looks like:

[
  {
     x: "2019-W33",
     y: 618940
  },...
]

The xAxis:

xScale={{
  type: "time",
  format: "%Y-W%V"
}}
axisBottom={{
  format: "%Y-W%V",
  tickValues: "every week"
}}

But the axis ticks are not aligned with the line’s points and the grid columns are not equally spaced. I’m filling this as a bug but please let me know if I’m doing something wrong…

Thank you

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

6reactions
wyzecommented, Dec 17, 2019

I set useUTC: false on the xScale property.

3reactions
swihartacommented, May 3, 2020

If it helps anyone, I currently addressed my issue with a function that produces the tick values explicitly. In my case, point.x values are Date objects, which are specific to a given time interval. See a CodeSandbox above for an example. start and end are the min and max dates included in the chart data, intervalType is ‘week’, 'month, 'day, or ‘year’, and intervalNum is the number of intervals (ex. 3 months) per data point.

getIntervalTicks = (data, maxTicks=12) => {
  if (!(data && data.length)) {
    console.log('not able to explicitly generate ticks')
      return 8 // fallback to automatically generated ticks of this number
  }
  const {start, end, intervalNum, intervalType} = this.state
  const intervalCount = Math.floor(moment(end).diff(start, intervalType) / intervalNum)
  return data[0].data.reduce((accum, point, index) => {
      const divisor = Math.ceil(intervalCount / maxTicks)
      if (index % divisor === 0)
          accum.push(point.x)
      return accum
  }, [])
}

Then I call this function from the chart parameters as below:

axisBottom: {
  // blah, blah, blah...
  tickValues: this.getIntervalTicks(data),
},
gridXValues: this.getIntervalTicks(data),

This results in limiting the number of ticks to the maxTicks value. So if you have maxTicks=12, and 12 months of data you get 12 ticks, but if you have 13 data points, you’ll drop your number of ticks down to 6, then with 14 data points, it goes to 7 ticks, etc…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Perspective charts time scale tick marks not aligned as expected
Ignition 8.1.4 (and earlier): Time scale tick marks that don't display full time with minutes (and seconds on shorter scales) do not align ......
Read more >
Data points and ticks in the scaleBand axis are not aligned
The band scale is the wrong tool in this situation. The main reason is that a band scale has an associated bandwidth.
Read more >
Using a date/time scale - JpGraph
The easiest way to get a date time scale for the X-axis is to use the pre-defined " dat " scale. ... Manually...
Read more >
pgfplots aligning subplots causes misaligned y-tick labels - TeX
This solution works for fixing the slight misalignment between the two plots, but it also messes up the y-tick labels, making them overlap ......
Read more >
2015 Canadian Market Structure Forecast - Markets Media
the U.S. over the past several months – the time frame we are lead to ... and marketplaces are currently so misaligned that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found