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] Allow series.label property to receive a function with the "location" it is going to be displayed on #12830

Conversation

JCQuintas
Copy link
Member

@JCQuintas JCQuintas commented Apr 18, 2024

  • Allow series.label property to receive a function with the "location" it is going to be displayed on
  • location options are legend | tooltip for Series, Scatter and Line
  • And legend | tooltip | arc for Pie
  • Small typos fixed
  • Added label documentation as a standalone page

resolves #12482

Todo:

  • Example usage
  • Typings documentation

@JCQuintas
Copy link
Member Author

JCQuintas commented Apr 18, 2024

@alexfauquette what do you think about having a labelFormatter on the series in order to solve #12482?

https://codesandbox.io/p/sandbox/12482-labelformatter-dj3d9c?file=%2Fsrc%2Fdemo.tsx%3A8%2C32

@mui-bot
Copy link

mui-bot commented Apr 18, 2024

Deploy preview: https://deploy-preview-12830--material-ui-x.netlify.app/

Updated pages:

Generated by 🚫 dangerJS against 40eb3e9

@alexfauquette
Copy link
Member

what do you think about having a labelFormatter on the series in order to solve #12482?

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)",

@JCQuintas
Copy link
Member Author

From a DX point of view, this syntax seems easier to read and document:

I kind of agree, but we have Pie charts which would mean we need to add labelTooltip to the data as well.

So pretty much series.labelTooltip and when type=pie series[].data[].labelTooltip. If that seems better, should I also change arcLabel to accept 'labelTooltip'?

@alexfauquette
Copy link
Member

I kind of agree, but we have Pie charts which would mean we need to add labelTooltip to the data as well.

Always a pain to support pie charts :)

What about having tooltipLabel and legendLabel (on the same idea as arcLabel).

It would be two formatter with type ((item: DefaultizedPieValueType) => string). For those two new ones, we don't have to add 'formattedValue' | 'label' | 'value' because only 'label' makes sense for the tooltip/legend.

@alexfauquette alexfauquette added the component: charts This is the name of the generic UI component, not the React module! label Apr 18, 2024
@JCQuintas JCQuintas added enhancement This is not a bug, nor a new feature new feature New feature or request and removed enhancement This is not a bug, nor a new feature labels Apr 18, 2024
@JCQuintas
Copy link
Member Author

Yeah, Pie charts are also odd that we use their dataset as "series" and that messes with my feelings a lot 😛

@JCQuintas
Copy link
Member Author

JCQuintas commented Apr 22, 2024

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?

@alexfauquette
Copy link
Member

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;

@JCQuintas
Copy link
Member Author

What about replacing the context with just the location. and we will add the context if necessary latter as a second argument:

Sure that can work

@JCQuintas JCQuintas force-pushed the 12482-charts-allow-to-have-specific-labels-in-legendstooltip branch from fad377e to de79be7 Compare April 25, 2024 13:35
@JCQuintas JCQuintas changed the title [charts] Add labelFormatter property to series to allow formatting the label based on where it is displayed [charts] Allow label property to receive a function with the "location" it is going to be displayed on Apr 25, 2024
@JCQuintas JCQuintas changed the title [charts] Allow label property to receive a function with the "location" it is going to be displayed on [charts] Allow series.label property to receive a function with the "location" it is going to be displayed on Apr 25, 2024
@JCQuintas JCQuintas marked this pull request as ready for review April 25, 2024 15:52
Copy link
Member

@alexfauquette alexfauquette left a 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'),
Copy link
Member

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

Copy link
Member Author

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.

packages/x-charts/src/PieChart/PieArcLabelPlot.tsx Outdated Show resolved Hide resolved
Comment on lines 46 to 49
return arcLabel({
...item,
label: getLabel(item.label, 'arc'),
});
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member Author

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...

@JCQuintas JCQuintas force-pushed the 12482-charts-allow-to-have-specific-labels-in-legendstooltip branch from 977ebaa to 93ce861 Compare April 29, 2024 13:45
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 30, 2024
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Apr 30, 2024
Copy link
Member

@LukasTy LukasTy left a 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. 💯

packages/x-charts/src/models/seriesType/pie.ts Outdated Show resolved Hide resolved
docs/package.json Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/PieLabel.tsx Outdated Show resolved Hide resolved
JCQuintas and others added 10 commits May 9, 2024 17:31
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>
Co-authored-by: Lukas <llukas.tyla@gmail.com>
Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
@JCQuintas JCQuintas force-pushed the 12482-charts-allow-to-have-specific-labels-in-legendstooltip branch from ef71abe to a870a02 Compare May 9, 2024 15:31
Copy link
Member

@alexfauquette alexfauquette left a 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

docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
docs/data/charts/label/label.md Outdated Show resolved Hide resolved
JCQuintas and others added 9 commits May 13, 2024 09:58
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>
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

docs/data/charts/label/label.md Outdated Show resolved Hide resolved
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
@JCQuintas JCQuintas enabled auto-merge (squash) May 13, 2024 09:06
@JCQuintas JCQuintas merged commit 128f082 into mui:master May 13, 2024
15 checks passed
@JCQuintas JCQuintas deleted the 12482-charts-allow-to-have-specific-labels-in-legendstooltip branch May 13, 2024 09:24
arthurbalduini pushed a commit to arthurbalduini/mui-x that referenced this pull request May 23, 2024
… "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>
joakimtveter pushed a commit to joakimtveter/mui-x that referenced this pull request Jun 6, 2024
… "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>
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! new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[charts] Allow to have specific labels in legends/tooltip
4 participants