-
Notifications
You must be signed in to change notification settings - Fork 19.7k
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
[FEATURE] [5.0] time axis label formatter #12859
Conversation
Thanks for your contribution! |
@Ovilia Add something that could be considered: (I)The callback of xAxis: {
type: 'time'
formatter: function (value, idx, params) {
// need to know here:
// (1) this label is on a primary level tick or a secondary level tick
// (2) what the level is ('day' or 'month' or ...)
}
} (II)
Follow the current way of "reference the variable" like (III)
If users intend to use If (a), nothing need to considered in this feature design. (IV)For i18n, probably we could simply add some new built-in notations? (not sure)
|
@Ovilia Some additional thoughts based on this topic: Extend to Major/Minor axis ticks instead of use Primary/Secondary styleThis will help general customization on all geo coord axis ticks/labels not only simply specified to time axis. For time axis, user can use two formatter if they need. (more like the ways build charts in Microsoft Office) For i18nI would prefer to use any of external formatter (Moment for example) in callback, as various external time formatter have already provided enough functionality on culture/date preference/lanuange etc. This would also help seperate on format part and render part, which I think in axis component render is what need to be focused on. |
@quillblue Thanks for your comments. Here are my thoughts on this. Primary & Secondary TicksI think secondary ticks may not be a minor tick. Consider the following case:
By our definition, Also, primary and secondary are two default rich text styles we provide. There may also be a tertiary level like week info in the above case. So major and minor ticks may not deal with situations like this. i18nFirst, we have to provide some frequently used formats like Here are my proposed formatters supported:
|
(1)In the column of (2)How about the current |
@100pah I think that makes sense. I would suggest break change it to 1~12 because it's more universal and it cannot provide extra help to |
@Ovilia for the case you mention I treat 2020 as a major tick on the level of year (though from straight understanding it is on the same monthly level of Feb, Mar), and minor tick on the level of month. For i18n, I agree with that common and widely used date formatter is provided by ECharts self maybe in util since format will be used not only in axis but also maybe in series and etc. |
Most of the functions above have been implemented, but not week of year. Leaving it for another PR. |
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', | ||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' | ||
], | ||
dayOfWeek: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be Chinese month and monthAbbr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Hi @Ovilia is |
Week of year is not supported. Please follow the doc rather than the PR description because the PR description is written before the implementation. If you wish to implement week of year, you can use third party libs like moment and then use callback formatter to return the value. |
Brief Information
This pull request is in the type of:
What does this PR do?
Improves label formatting for time axes.
Fixed issues
Details
Before & After
The "After" parts are at proposals stage by now.
1. Default time axis labels don't have nice format and display information not so important
Before:
For example, in the above case, labels of each day are all in the format of
MM-DD\nYYYY
, but the months and years are all the same so they should not be displayed so many times to distract users for the real important information: date.After:
The default result of the above case have the interval of 6 hours, thus default hour formatter (may be
'hh:mm'
) should be used to display labels in hours and day formatter ('YYYY-MM-DD'
) be used when day changes. This is the expected result:And the default style of the primary level (like day in this case) and secondary level (hour in this case) are different (maybe we can use a lighter text color for secondary level labels) and can be customized though axisLabel.rich
.primaryStyle
andsecondaryStyle
.2. Multiple labels with the same text may be displayed by default
Before:
Also with the above example, we can see that there are three
05-12 2012
labels, which is confusing. Especially, this should not be the default behavior of labels.After:
There won't be labels with the same date and hour labels will be displayed in the above case.
3. Custom style is too complex to write with rich text
Before:
To get the above result, the user has to write the following long code of formatter. Users may not know they could do this, even with clear documentation.
After:
Current callback form of formatter will also be available, but we also provide a much simpler way to write if the logic is not so complex.
We will support formatters for varied levels like year, month, day, hour, minute and etc., and each of them supports the formatter in either forms of
{hh}:{mm}:{ss}
for simple plain text or{xxx | {hh}:{mm}:ss}
for rich text, whose style can be configured in axisLabel.rich.4. Cannot use different formatters for different intervals
Consider the "2020 Feb" and "Feb" in the following example. The user want to use different formatters (
'YYYY-MMM'
in case A and'MMM'
in case B.Before:
It cannot be done.
After:
formatter.month
defines formatters when the tick value is a month value (by checking if it's the first day of a month). And it can be in the form of an array of formatters like:['YYYY-MMM', 'MMM']
.This means, if the label is a first-level (also called primary level) as in case A, where the levels of the labels are day, month, day, day, it uses the first element in the array for the second tick, which is
'YYYY-MMM'
and gets "2020 Feb". Similarly, in case B, where the levels of the labels are year, month, month, month, it uses the second element in the array for the second tick and gets "Feb".5. Cannot display ticks exactly on the first day of a month or week
Before:
There are different days in each month, e.g., 28/29 for Feb and 31 for July. There is no way to make sure the first day is displayed.
This looks a bit arbitrary when choosing the days to be displayed with ticks. And it cannot be changed even with configuration.
After:
If the months are different with min and max data (but years are the same), the primary level will be month and we make sure that the first day of each month is displayed with ticks. Note that in this case, the space between each ticks are not the same because days in months are not.
6. i18n
Before:
Using formatter in the callback form like
echarts.format.formatTime('M月', value)
. It is quite challenging for developers not so familiar with JavaScript.After:
We can provide frequently used time formats like
YYYY
for full year,MM
for month number and so on. Moment.js and can be served as references.i18n of the time axis should be considered along with other i18n requirements in ECharts, like toolbox and so on.
We are considering embed with English and Chinese language by default with ECharts and other languages can be used as extensions. We are still discuss about the details on this. But the bottom line is, it won't be necessary for developers to implement by using code as low level as
echarts.format.formatTime
.Usage
Are there any API changes?
Before:
After:
LevelFormats
is defined as:The
string
can either be a plain text like'{hh}:{mm}:{ss}'
or a rich text likestrong | '{hh}:{mm}'
and define the rich stylestrong
informatter
.Well... I know this sounds a little complex right now. But it's actually turns easier when you see the real world examples.
Example of Usage
1. Default
No
formatter
is required. Label text will display as what we think work for general cases.2. Custom primary and secondary styles
If the user wants to use red color for
2020 Feb
in case A and2020
in case B, this is what required:3. Display only weeks of year
Related test cases or examples to use the new APIs
NA.
Others
Merging options
Other information