-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Allow series.label
property to receive a function with the "location" it is going to be displayed on
#12830
[charts] Allow series.label
property to receive a function with the "location" it is going to be displayed on
#12830
Conversation
@alexfauquette what do you think about having a https://codesandbox.io/p/sandbox/12482-labelformatter-dj3d9c?file=%2Fsrc%2Fdemo.tsx%3A8%2C32 |
Deploy preview: https://deploy-preview-12830--material-ui-x.netlify.app/ Updated pages: |
IMHO the formatter is useful to define a rendering once and use it multiple times. For example with valueFormatter, you define it once for the series, and it's applied to all the values. Since series have one label that can be displayed in the tooltip or the legend, you end up with only two cases. From a DX point of view, this syntax seems easier to read and document: label: 'Duration',
- labelFormatter: (v, { location }) => location === 'tooltip' ? `${v} (HH:MM:SS)` : v,
+ labelTooltip: 'Duration (HH:MM:SS)", |
I kind of agree, but we have Pie charts which would mean we need to add So pretty much |
Always a pain to support pie charts :) What about having It would be two formatter with type |
Yeah, Pie charts are also odd that we use their dataset as "series" and that messes with my feelings a lot 😛 |
So thought a bit more about this, and I don't see a lot of value in having all these different formatters when one with a context would do 🤔 This PR implements series[].label: string
series[].labelFormatter: ((context: SeriesLabelFormatterContext) => string)
series[type=pie].data[].label = string
series[type=pie].data[].arcLabel = 'formattedValue' | 'label' | 'value'| ((item: DefaultizedPieValueType) => string) Your suggestion would be series[].legend = 'string'
series[].tooltipLabel = (item: DefaultizedPieValueType) => string
series[].legendLabel = (item: DefaultizedPieValueType) => string
series[type=pie].data[].label = 'string'
series[type=pie].data[].arcLabel = 'formattedValue' | 'label' | 'value'| ((item: DefaultizedPieValueType) => string)
series[type=pie].data[].tooltipLabel = (item: DefaultizedPieValueType) => string
series[type=pie].data[].legendLabel = (item: DefaultizedPieValueType) => string Is this correct? I think we could build on the first case to make it smaller, like, if formatting the label is important, rather than having label be a a string, we could also allow functions series[].label: string | ((context: SeriesLabelFormatterContext) => string);
series[type=pie].data[].label = string | ((context: SeriesLabelFormatterContext) => string)
series[type=pie].data[].arcLabel = 'formattedValue' | 'label' | 'value'| ((item: DefaultizedPieValueType) => string) Would this be a good approach? |
Yes, that looks even better 👍 What about replacing the context with just the location. and we will add the context if necessary latter as a second argument: - series[].label: string | ((context: SeriesLabelFormatterContext) => string);
+ series[].label: string | (location: "tooltip" | "axis") => string; |
Sure that can work |
fad377e
to
de79be7
Compare
labelFormatter
property to series
to allow formatting the label based on where it is displayedlabel
property to receive a function with the "location" it is going to be displayed on
label
property to receive a function with the "location" it is going to be displayed onseries.label
property to receive a function with the "location" it is going to be displayed on
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.
As said in our weekly, this looks pretty nice. I'm still wondering if we could improve the DX on the pie chart
About the docs it looks good. Maybe adding links from the components overviw pages to the one you created, like we do for pickers
The pie chart page will also require a particular update depending on the choice made
https://mui.com/x/react-charts/pie/#labels
series.type === 'pie' | ||
? { | ||
...series.data[itemData.dataIndex], | ||
label: getLabel(series.data[itemData.dataIndex].label, 'tooltip'), |
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.
Not sure about why we need to do the computation of the label here. This value is just here to let the user format the value
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.
Else the user receives a function as the label, which they will then have to check if it is a function before calling, and has to call with the correct params. We just streamline the process, now they can always expect a string there.
return arcLabel({ | ||
...item, | ||
label: getLabel(item.label, 'arc'), | ||
}); |
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.
Feels weird to have two levels of resolving function.
A solution could be to keep label
as a string, and have a labelFormatter: (item, context) => string
The demo would then be labelFormatter: (item, context) => `${context.location}+${item.label}`
assuming items labels are A, B, and C.
In that case, we should deprecate the arcLabel
and remove it in the next major version to keep only on way for formatting arc labels.
The other option is to create tooltipLabel
and legendLabel
properties. It's less elegant, but do the job
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.
Do you mean we should have a labelFormatter
for the other series types as well?
I think the issue with arc
is that its inner text
can actually represent values, not only the labels.
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.
On a related note, it would probably be very useful for us to unify the pie-charts data structure in relation to the other data structures. Though I'm not sure it is possible...
977ebaa
to
93ce861
Compare
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
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.
Looks like a really nice improvement for label formatting. 👍
The API seems elegant and powerful. 💯
This reverts commit bb2b31078b4031e7706f6058a07e9be484e927dd.
This reverts commit 70dc0941ec8cf5124bc1decd88d69d4e157bbe97.
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Lukas <llukas.tyla@gmail.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
ef71abe
to
a870a02
Compare
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.
Looks nice. Only few proposals to make the docs page more straight forward
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
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.
Looks good 👍
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
… "location" it is going to be displayed on (mui#12830) Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com> Co-authored-by: Lukas <llukas.tyla@gmail.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
… "location" it is going to be displayed on (mui#12830) Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com> Co-authored-by: Lukas <llukas.tyla@gmail.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
… "location" it is going to be displayed on (mui#12830) Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com> Co-authored-by: Lukas <llukas.tyla@gmail.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
series.label
property to receive a function with the "location" it is going to be displayed onlocation
options arelegend
|tooltip
for Series, Scatter and Linelegend
|tooltip
|arc
for Pielabel
documentation as a standalone pageresolves #12482
Todo: