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

[charts] How to display given time as local time in a LineChart #11374

Closed
jvskriubakken opened this issue Dec 11, 2023 · 9 comments
Closed

[charts] How to display given time as local time in a LineChart #11374

jvskriubakken opened this issue Dec 11, 2023 · 9 comments
Labels
component: charts This is the name of the generic UI component, not the React module! support: commercial Support request from paid users

Comments

@jvskriubakken
Copy link

jvskriubakken commented Dec 11, 2023

The problem in depth 🔍

Hi

I am using charts - line chart, and have set the x-axis to be the time axis (scaleType: "utc"). The time is displayed in UTC, but I want to display the in local time, what is the best way to achieve that? Same question goes for local formatting of the date time.

Your environment 🌎

Browser: Chrome v120

`npx @mui/envinfo`
    @mui/x-license-pro:  6.10.2 
    @mui/x-tree-view: 6.17.0 => 6.17.0 
    @types/react: 18.2.22 => 18.2.22 
    react: 18.2.0 => 18.2.0 
    react-dom: 18.2.0 => 18.2.0 
    styled-components: 6.0.8 => 6.0.8 
    typescript: 5.1.6 => 5.1.6 

Search keywords: charts utc time local
Order ID: 78678

@jvskriubakken jvskriubakken added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels Dec 11, 2023
@jvskriubakken jvskriubakken changed the title [question] [question] How to display given time as local time in a LineChart Dec 11, 2023
@michelengelen
Copy link
Member

Hey @jvskriubakken ... could you provide us with a minimal example of it?
That will help us tremendously to understand your problem. You can use this codesandbox as a starting point.
Thanks! 🙇🏼

@michelengelen michelengelen added status: waiting for author Issue with insufficient information component: charts This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 11, 2023
@jvskriubakken
Copy link
Author

jvskriubakken commented Dec 12, 2023

OK. I have tried to create an example based on your starting point:
https://codesandbox.io/p/sandbox/infallible-chihiro-9t492y

In my browser (Chrome v120), the dates are displayed in UTC time, and the date times are formatted in english locale (12 hour clock with AM/PM). I expect or want the date times formatted in my norwegian locale (24 hour clock, and dates as: day-of-month/month/year)
Screenshot 2023-12-12 at 10 17 14

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 12, 2023
@michelengelen
Copy link
Member

In that case your best bet is to use an external date library like dayjs, since date formatting is a pretty bad experience with the built in Date object.

Check out this little demo i built on top of your example. Is this about the result you are looking for?

For the tooltip header value: @alexfauquette can we change the format of those independently? Like with a custom component (as a last resort)?

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 12, 2023
@alexfauquette
Copy link
Member

Being able to format differently for tootlip and axis is not yet feasible

+1 for using a date library. If you don't use it in the rest of your app, maybe digging into the native toLocaleTimeString could worth it

@michelengelen michelengelen changed the title [question] How to display given time as local time in a LineChart [charts] How to display given time as local time in a LineChart Dec 13, 2023
@michelengelen
Copy link
Member

+1 on what @alexfauquette wrote.

@jvskriubakken the only way you can take to get what you are aiming for is to use the slots, more specifically slots.axisContent

I have updated my previous demo with a very minimalistic approach here.

Let me know if this is sufficient for you. Thanks! 🙇🏼

@jvskriubakken
Copy link
Author

Hi,

thanks for all the input guys.

I managed to change x-axis tick labels by giving the the following valueFormatter in the x-axis config:

const valueFormatter = (dateTimeJs: Date): string => {
   const dateTime = DateTime.fromJSDate(dateTimeJs); // Luxon DateTime
   return dateTime.toLocaleString(DateTime.TIME_24_SIMPLE);
};

I notice that the ChartsTooltip (I am using ResponsiveChartContainer now) displays the given utc date time in local time by it self. But when I override the valueFormatter on x-axis, then ChartsTooltip seems to use that valueFormatter for x-value too.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Dec 18, 2023
@jvskriubakken
Copy link
Author

I understand now, that to be able to display a full date time in the ChartsTooltip I need to override the slots.axisContent :-)

Thanks!

@michelengelen
Copy link
Member

Is there anything else regarding this that we can help you with @jvskriubakken?
If not we can close this issue! Thanks! 🙇🏼

@jvskriubakken
Copy link
Author

No it's fine then! Thanks!

@github-actions github-actions bot removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! support: commercial Support request from paid users
Projects
None yet
Development

No branches or pull requests

3 participants