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

WIP: Rework timeStuffs to allow time formats to be specified #517

Closed
wants to merge 1 commit into from

Conversation

bytesnz
Copy link

@bytesnz bytesnz commented May 19, 2021

I was wanting to be able to format dates differently from the default american style. After having a look into the code, I decided to:

  • Rework opts.js genTimeStuffs() to allow formats to be passed as an opts object
  • Change uPlot to generate time axes functions on initialisation.

It allows users to give the time component formats in a timeFormat object. For example, to use DD/MM/YYYY.

                        ...
			const opts = {
				width: 1920,
				height: 600,
				title: "Super Data",
				timeFormats: {
				    md: "{D}/{M}"
				}
				series: [
					{},
					{
						stroke: "red",
					},
				]
			};

			let u = new uPlot(opts, data, document.body);

To use YYYY-MM-DD

    ...
    timeFormats: {
        md: "{M}-{D}",
        MLmdyy: "{YYYY}-{M}-{D}"
    }

If not specified in timeFormats the original defaults will be used.

This is a bit more basic than the request in #474, but it is a start for allowing different formats (I currently don't have time to dig into the Intl date format stuff atm).

One thing I was wondering about was also allow you to specify the months and days in the timeFormat object. In the example case of Russian names, this could mean you don't need to call uPlot.fmtDate and just give it the arrays in the timeFormat object

- Rework `genTimeStuffs()` to allow formats to be passed as an opts object
- Change uPlot to generate time axes functions on initialisation
@leeoniya
Copy link
Owner

hi @bytesnz , thanks for the PR.

i don't think this is a good idea, since it is already possible to specify the entire time formatting table, which gives you 100% control. i would like to avoid offering simplified/partial versions of this, since this can easily be accomplished with external helper functions that assemble this table. is there a specific format you cannot modify with the existing api?

@bytesnz
Copy link
Author

bytesnz commented May 20, 2021

Ah. Can you? I couldn't see a way of doing it when I looked at the code.. (looking at https://github.com/leeoniya/uPlot/blob/master/src/uPlot.js#L463 and

function timeAxisSplits(tzDate) {
)?

@leeoniya
Copy link
Owner

axis.values can redefine the whole formatting table: https://github.com/leeoniya/uPlot/tree/master/docs#axis--grid-opts

if you want only a single, unconditional tick format, axis.values can also be a template string.

@leeoniya leeoniya closed this May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants