echarts icon indicating copy to clipboard operation
echarts copied to clipboard

Possibility to set specific timezone for time axis

Open throrin19 opened this issue 3 years ago • 10 comments

What problem does this feature solve?

In our case, we have many clients arround the world. We want have the ability to show their data using their timezone. Actually, Echarts use GMT dates to set xAxis intervals labels and current browser timezone for date format.

We correctly set the correct timezone in formatter but, for intervals, we have already the problem of GMT.

It's a really important feature for us.

What does the proposed API look like?

An option, in xAxis to set timezone should be great to fix the problem :

xAxis : {
    type : 'time',
    timezone : 'Europe/Paris',  // with that, all xAxis times are manipulated using this timestamp
}

throrin19 avatar Mar 11 '21 07:03 throrin19

Hi! We've received your issue and please be patient to get responded. 🎉 The average response time is expected to be within one day for weekdays.

In the meanwhile, please make sure that you have posted enough image to demo your request. You may also check out the API and chart option to get the answer.

If you don't get helped for a long time (over a week) or have an urgent question to ask, you may also send an email to [email protected]. Please attach the issue link if it's a technical question.

If you are interested in the project, you may also subscribe our mailing list.

Have a nice day! 🍵

echarts-bot[bot] avatar Mar 11 '21 07:03 echarts-bot[bot]

In our case, we set the formatter as function to convert date to specific timezone and, in the showed labels, we see that when the interval is one day or more, the corresponding day is based on UTC dates.

In the following example, here is a 4-day display with dates displayed in GMT+1 :

image

Normally, and if I understand the logic of Echarts correctly, in this case the interval should make dates every 12 hours, i.e. 00:00 and 12:00 for each label. But here, we can see that it is the case, but on the UTC date

throrin19 avatar Mar 11 '21 08:03 throrin19

I found how to resolve this with date-fns-tz :

I force the dateTime in UTC but converted in specific timezone using function utcToZonedTime. And with that, all showed dates are correctly showed in specific timezone without any problems :

import { utcToZonedTime, format } from 'date-fns-tz';

const timezone = 'America/Boise';
const time = [
    1615868012847,
    1615889612847,
];

const data = [
    // data array with [time, value] items
];

const options = {
    xAxis   : {
        type        : 'time',
        min         : utcToZonedTime(time[0], timezone),
        max         : utcToZonedTime(time[1], timezone),
    },
    tooltip : {
        trigger     : 'axis',
        confine     : true,
        axisPointer : {
            animation   : false,
            label       : {
                formatter({ value }) {
                    // used to already show datetime completelly
                   return format(value, 'yyyy-MM-dd HH:mm:ss', { timeZone : timezone });
                },
            },
        },
    },
    series : [{
        type : 'line',
        name : 'test',
        data : data.map(item => [utcToZonedTime(item[0], timezone) ,item[1]]),
    }],
};

throrin19 avatar Mar 16 '21 11:03 throrin19

We are also looking for something like this. We allow users to set the time zone they want to see and that time zone may be different from the browser's time. We're passing in ISO strings for the times and it seems whether I pass in UTC, or specific time zones echarts always (correctly) converts them to the browser's time zone. We tried to work around this by leaving off the time zone information which mostly works except it does not handle the transition to/from DST as one might expect.

What we would like would be a way to set a time zone for the whole chart, or perhaps per series. All times passed into the data would be displayed and calculated in that time zone rather than the browser's time zone.

Our fallback solution in the interim is to continue to pass in UTC times and override every tooltip and axis formatter to convert to the time zone the user specified to make it appear like the data is in the right time zone. The only draw back to this approach that I've found is that the axis ticks that are chosen are not nice round times like there would normally be (i.e. it chooses to put a tick at what appears to be 3 am instead of midnight).

ptulou avatar Oct 15 '21 19:10 ptulou

We would be interested into this functionality as well, being able to specify a timezone and have everything rendered as if we were in that timezone (instead of the current browser timezone) would be great.

Right now, similarly to the hack that was suggested, we do the same thing but with moment.tz.

/**
 * Will hard-shift a timestamp so that, if rendered in current timezone, it will look as it is instead
 * into the desired timezone.
 */
export function utcToZonedTime(utcTime: number, timezone: string) {
  const ourTimezone = moment.tz.guess();
  const ourOffsetInMillis = moment.utc(utcTime).tz(ourTimezone).utcOffset() * 60000;
  const givenTimezoneOffsetInMillis = moment.utc(utcTime).tz(timezone).utcOffset() * 60000;

  return utcTime + givenTimezoneOffsetInMillis - ourOffsetInMillis;
}

/**
 * Will revert what utcToZonedTime had done.
 */
export function zonedTimeToUtc(zonedTime: number, timezone: string) {
  const ourTimezone = moment.tz.guess();
  const ourOffsetInMillis = moment.utc(zonedTime).tz(ourTimezone).utcOffset() * 60000;
  const givenTimezoneOffsetInMillis = moment.utc(zonedTime).tz(timezone).utcOffset() * 60000;

  return zonedTime - givenTimezoneOffsetInMillis + ourOffsetInMillis;
}

It kinda works.. but it would be way better if ECharts could do this on its own so that dates do not need to be modified.

NotAndD avatar Nov 26 '21 08:11 NotAndD

We also are in the same situation: we need to show charts localized in the timezone of where the data has been collected, not the browser time.

Actually we are using the formatter function of xAxis to localize the time, but in this way we are loosing all the cool automatic formatting feature of echart.

having a timezone option somewhere would be really usefull

DaveMDS avatar Dec 06 '22 08:12 DaveMDS

It's worth noting that the convert UTC times to their "corrected" timezone technique works, apart from days with daylight savings.

  • At the point of switching to daylight savings, the graph includes the skipped time (which means no data for that period)
  • At the point of exiting daylight savings the graph has to repeat the values for the "double hour" (see attached picture)
image

freshollie avatar Mar 22 '23 11:03 freshollie

Any update on this ? :)

pierrefaidherbe avatar Nov 27 '23 09:11 pierrefaidherbe

+1 on this one! We work in UTC for all of our data.

darksidelemm avatar Jan 11 '24 22:01 darksidelemm

+1...

rvillette avatar Feb 09 '24 10:02 rvillette

Any update on this ?

ktx-abhay avatar Mar 04 '24 13:03 ktx-abhay