-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Clickable Legend Titles #7698
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
Open
alexshoe
wants to merge
20
commits into
plotly:master
Choose a base branch
from
alexshoe:clickable-legend-titles
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+542
−35
Open
Clickable Legend Titles #7698
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
88a6c0d
Fix bug where `groupclick`: `toggleitem` doesn't work when there are …
alexshoe b2ef711
Add `titleclick` and `titledoubleclick` attributes
alexshoe cdb570a
Add handleTitleClick function for toggling visibility via legend title
alexshoe 089fa73
Add legend title click toggle setup and event handling
alexshoe 207910e
Add mock chart for legend title click feature
alexshoe 2f934ad
Only enable legend title click by default when there are multiple leg…
alexshoe cd92fea
Add jasmine tests for legend title click
alexshoe 53073d7
Update schema
alexshoe 7c2e878
Merge remote-tracking branch 'origin/master' into clickable-legend-ti…
alexshoe 459e229
Add baseline image
alexshoe 6bec6d1
Convert var to const where applicable
alexshoe fdf1d65
Move `getId()` to `helpers.js`
alexshoe 0cde808
Refactor handleClick() signature to be consistent with handleTitleClick
alexshoe 3a1336f
Add docstrings for `handleClick` and `handleTitleClick`
alexshoe 42c40f6
Replace null value with early continue and skip non-displayed traces
alexshoe 2b5d2af
Rename `handleClick` to `handleItemClick`
alexshoe 6cbdb49
Test legend title click attributes with non-default values
alexshoe 6f6fd91
Move titleToggle positioning into computeLegendDimensions
alexshoe e3a068e
Fix group title click resolving to wrong legend by adding missing leg…
alexshoe 28990fb
Update schema
alexshoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,14 +6,21 @@ var pushUnique = Lib.pushUnique; | |
|
|
||
| var SHOWISOLATETIP = true; | ||
|
|
||
| module.exports = function handleClick(g, gd, numClicks) { | ||
| exports.handleClick = function handleClick(g, gd, numClicks) { | ||
alexshoe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var fullLayout = gd._fullLayout; | ||
|
|
||
| if(gd._dragged || gd._editing) return; | ||
|
|
||
| var itemClick = fullLayout.legend.itemclick; | ||
| var itemDoubleClick = fullLayout.legend.itemdoubleclick; | ||
| var groupClick = fullLayout.legend.groupclick; | ||
|
|
||
| var legendItem = g.data()[0][0]; | ||
| if(legendItem.groupTitle && legendItem.noClick) return; | ||
|
|
||
| var legendId = legendItem.trace.legend || 'legend'; | ||
| var legendObj = fullLayout[legendId]; | ||
|
|
||
| var itemClick = legendObj.itemclick; | ||
| var itemDoubleClick = legendObj.itemdoubleclick; | ||
| var groupClick = legendObj.groupclick; | ||
|
|
||
| if(numClicks === 1 && itemClick === 'toggle' && itemDoubleClick === 'toggleothers' && | ||
| SHOWISOLATETIP && gd.data && gd._context.showTips | ||
|
|
@@ -35,9 +42,6 @@ module.exports = function handleClick(g, gd, numClicks) { | |
| fullLayout.hiddenlabels.slice() : | ||
| []; | ||
|
|
||
| var legendItem = g.data()[0][0]; | ||
| if(legendItem.groupTitle && legendItem.noClick) return; | ||
|
|
||
| var fullData = gd._fullData; | ||
| var shapesWithLegend = (fullLayout.shapes || []).filter(function(d) { return d.showlegend; }); | ||
| var allLegendItems = fullData.concat(shapesWithLegend); | ||
|
|
@@ -269,3 +273,64 @@ module.exports = function handleClick(g, gd, numClicks) { | |
| } | ||
| } | ||
| }; | ||
|
|
||
| exports.handleTitleClick = function handleTitleClick(gd, legendObj, mode) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also similar comment here to above -- check whether you can share some of this logic with the
alexshoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var fullLayout = gd._fullLayout; | ||
| var fullData = gd._fullData; | ||
| var legendId = legendObj._id || 'legend'; | ||
| var shapesWithLegend = (fullLayout.shapes || []).filter(function(d) { return d.showlegend; }); | ||
| var allLegendItems = fullData.concat(shapesWithLegend); | ||
|
|
||
| function isInLegend(item) { | ||
| return (item.legend || 'legend') === legendId; | ||
| } | ||
|
|
||
| var toggleThisLegend; | ||
| var toggleOtherLegends; | ||
|
|
||
| if(mode === 'toggle') { | ||
alexshoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // If any item is visible in this legend, hide all. If all are hidden, show all | ||
| var anyVisibleHere = allLegendItems.some(function(item) { | ||
| return isInLegend(item) && item.visible === true; | ||
| }); | ||
|
|
||
| toggleThisLegend = !anyVisibleHere; | ||
| toggleOtherLegends = null; | ||
alexshoe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| // isolate this legend or set all legends to visible | ||
| var anyVisibleElsewhere = allLegendItems.some(function(item) { | ||
| return !isInLegend(item) && item.visible === true && item.showlegend !== false; | ||
| }); | ||
|
|
||
| toggleThisLegend = true; | ||
| toggleOtherLegends = !anyVisibleElsewhere; | ||
| } | ||
|
|
||
| var dataUpdate = { visible: [] }; | ||
| var dataIndices = []; | ||
| var updatedShapes = (fullLayout.shapes || []).map(function(d) { return d._input; }); | ||
| var shapesUpdated = false; | ||
|
|
||
| for(var i = 0; i < allLegendItems.length; i++) { | ||
| var item = allLegendItems[i]; | ||
| var shouldShow = isInLegend(item) ? toggleThisLegend : toggleOtherLegends; | ||
| var newVis = shouldShow ? true : 'legendonly'; | ||
|
|
||
| // Only update if the item is visible and the visibility is different from the new visibility | ||
| if ((item.visible !== false) && (shouldShow !== null) && (item.visible !== newVis)) { | ||
| if(item._isShape) { | ||
| updatedShapes[item._index].visible = newVis; | ||
| shapesUpdated = true; | ||
| } else { | ||
| dataIndices.push(item.index); | ||
| dataUpdate.visible.push(newVis); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if(shapesUpdated) { | ||
| Registry.call('_guiUpdate', gd, dataUpdate, {shapes: updatedShapes}, dataIndices); | ||
| } else if(dataIndices.length) { | ||
| Registry.call('_guiRestyle', gd, dataUpdate, dataIndices); | ||
| } | ||
| }; | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.