Skip to content

Timestamp '1970/01/01 00:00:00' formats as 'undefined' in hovertext #6751

Closed

Description

@adamjhawley

I tried to customise the hovertext format of some timestamp data and found that the timestamp '1970/01/01 00:00:00' formats as 'undefined'.

Note: please hover over the first data point in the example below to see the issue I am describing.

Minimal example to recreate the issue (CodePen here):

var dates = [0,1].map(x => `1970-01-01 00:00:0${x}`);

var data = [
  {
    type: 'scatter',
    x: dates,
    y: [5,2,3,1,4],
    hovertemplate: "%{x}, %{y}"
  }]

var layout = {
  xaxis: {
    tickformat: '%H:%M:%S',
    hoverformat: '%H:%M:%S'
  },
}

Plotly.newPlot('myDiv', data, layout);

I am happy to try and fix this issue myself but could benefit from someone pointing me in the right direction. I presume that the issue is caused by something like "0 seconds since epoch" is wrongly resolved as falsey at some point.

Thanks in advance for any pointers/help!

Activity

  1. archmoj commented on Oct 18, 2023

    @archmoj
    Contributor

    Nice find 🥇
    After long investigation,
    it looks like the bug is coming from this block:

    // can we remove the whole time part?
    if(dateStr === '00:00:00' || dateStr === '00:00') {
    dateStr = headStr;
    headStr = '';
    } else if(dateStr.length === 8) {
    // strip off seconds if they're zero (zero fractional seconds
    // are already omitted)
    // but we never remove minutes and leave just hours
    dateStr = dateStr.replace(/:00$/, '');
    }

    Perhaps we should modify the first if statement to be something like:

    if(headStr !== undefined && (dateStr === '00:00:00' || dateStr === '00:00')) {

    I am looking forward to reviewing your pull request.
    FYI - Our contributing information is at https://github.com/plotly/plotly.js/blob/master/CONTRIBUTING.md

  2. adamjhawley commented on Oct 18, 2023

    @adamjhawley
    ContributorAuthor

    Thanks @archmoj for the pointers! I have now created PR #6752.

    In addition to your suggestion, I had to add another change which prevents the function from trailing stripping zeroes when they were not added because of 'extraPrecision'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsomething broken

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions