-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Pie legend and showlegend per slice #7580
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
base: master
Are you sure you want to change the base?
Conversation
…ice legend visibility.
…legend assignment.
…pies with showlegend and/or legend as arrays
|
@alexshoe Hi Alex! Do you possibly have an estimate when this PR could be tackled? Also, let me know if you have any questions :) |
|
Hey @my-tien! Sorry, just working through a backlog of different PRs that I was assigned. I'll start reviewing this tomorrow. |
…isibility # Conflicts: # test/plot-schema.json
|
@alexshoe Hi Alex, I saw that my branch got out of date and conflicted with the current EDIT: After wiping node_modules and re-installing with
|
|
@my-tien could you please add some more information to the OP? I'm interested in seeing sections describing:
With that, I'll be able to better review the changes. |
…d instead of labels.
|
Hi @camdecoster, I also updated the OP and added a better description of the properties for the plot schema. Please let me know if anything is unclear or missing! |
camdecoster
left a comment
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.
Thanks for adding this feature! There are a few changes to make, but it seems to work well.
src/components/legend/defaults.js
Outdated
| !Array.isArray(trace.showlegend) | ||
| ? trace.showlegend || trace._dfltShowLegend | ||
| : trace.showlegend[index] == null | ||
| ? trace._dfltShowLegend | ||
| : trace.showlegend[index] |
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.
Could you please refactor this to avoid nested ternary expressions?
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.
Hope it's clearer now! Leaving the conversation unresolved for your judgement :)
camdecoster
left a comment
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.
Here are a few more suggestions.
| var showInLegend = trace.showlegend || trace._dfltShowLegend; | ||
| if (Array.isArray(trace.showlegend)) { | ||
| showInLegend = trace.showlegend[index] ?? trace._dfltShowLegend; | ||
| } | ||
| if(showInLegend) { | ||
| legendReallyHasATrace = true; | ||
| legendTraceCount++; | ||
| } |
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.
What about something like this? I'm not sure if you really need the nullish coalescing operator. If you do, then this will need to be adjusted.
| var showInLegend = trace.showlegend || trace._dfltShowLegend; | |
| if (Array.isArray(trace.showlegend)) { | |
| showInLegend = trace.showlegend[index] ?? trace._dfltShowLegend; | |
| } | |
| if(showInLegend) { | |
| legendReallyHasATrace = true; | |
| legendTraceCount++; | |
| } | |
| const showInLegend = Array.isArray(trace.showlegend) ? trace.showlegend[index] : trace.showlegend; | |
| if (showInLegend || trace._dfltShowLegend) { | |
| legendReallyHasATrace = true; | |
| legendTraceCount++; | |
| } |
| function isBoolean(value) { | ||
| return value === true || value === false; | ||
| } |
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.
Let's make this a one-liner:
| function isBoolean(value) { | |
| return value === true || value === false; | |
| } | |
| const isBoolean = value => value === true || value === false; |
Or
| function isBoolean(value) { | |
| return value === true || value === false; | |
| } | |
| const isBoolean = value => typeof value === "boolean"; |
| return value === true || value === false; | ||
| } | ||
|
|
||
| if (opts.arrayOk && isArrayOrTypedArray(v) && v.length > 0 && v.every(isBoolean)) { |
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 anticipate typed arrays being used here?
| if (opts.arrayOk && isArrayOrTypedArray(v) && v.length > 0 && v.every(isBoolean)) { | ||
| propOut.set(v); | ||
| } else if(isBoolean(v)) { | ||
| propOut.set(v); |
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.
Could you combine these two paths since they result in the same call?
| function isSubplotId(value) { | ||
| return typeof value === 'string' && regex.test(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.
| function isSubplotId(value) { | |
| return typeof value === 'string' && regex.test(value); | |
| } | |
| const isSubplotId = value => typeof value === 'string' && regex.test(value); |
| if (opts.arrayOk && isArrayOrTypedArray(v) && v.length > 0 && v.every(isSubplotId)) { | ||
| propOut.set(v); | ||
| return; | ||
| } else if(isSubplotId(v)) { | ||
| propOut.set(v); |
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.
Could you combine these two paths since they result in the same call?
This PR makes it possible to pass arrays to the pie properties 'legend' and 'showlegend' so that legend assignment and visibility can be configured per slice.
Changes:
Previously,
traces.pie.legendandtraces.pie.showlegendonly accepted one value that applied for all slices. With this PR, slice legend entries can be grouped and individual slices can be hidden in the legend:Result
Old behavior
No possibility to group slices semantically or to hide a slice. Also takes up more vertical space.

Testing
For testing, I added 3 mocks:
label0anddlabelare set instead oflabels.Note:
This feature is not expected to work when pie
valuesare not set.Disclaimer
I am required to add that…the software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.