-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add ability to rename grouped traces #1919
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
Conversation
On usage of the fancy container array thing. It's basically used like this: var carr = Lib.keyedContainer(fullInput, 'transforms[' + i + '].groupnames');
if(BLANK_STRING_REGEX.test(text)) {
carr.remove(legendItem.trace._group);
} else {
carr.set(legendItem.trace._group, [text]);
}
Plotly.restyle(gd, carr.constructUpdate(), [traceNumber]); When you add keys, it constructs the update. If you remove a key, it slides over all the other indices to keep the storage compressed (rather than a thousand undefined's because you kept modifying the plot). Really, the whole goal is just to keep things schema-conforming without arbitrary keys. That may or may not be the right approach here, but this library function at least facilitates the process. |
I've made my own requested changes to this, including internal cleanup and that it now shows the "groupName" if there's only one trace or "groupName (traceName)" if there's more than one trace. I had to pass |
src/components/legend/draw.js
Outdated
update[direction + '.name'] = text; | ||
} else if(fullInput.transforms) { | ||
for(i = fullInput.transforms.length - 1; i >= 0; i--) { | ||
if(fullInput.transforms[i].type === 'groupby') { |
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.
Might be nice to add a lib function similar to Registry.traceIs
for transforms e.g. Registry,hasTransform('groupby')
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.
Added Regsitry.hasTransform
and Registry.getTransformIndices
to accomplish this. It's less efficient than the above code, but so marginally that I think it's fine. ✅
src/components/legend/draw.js
Outdated
// string is retained. | ||
if(needsRedraw) { | ||
p = p.then(function() { | ||
return Plotly.redraw(gd); |
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.
Hmm. We shouldn't have to do this. We should fix _restyle
so that it leads to the correct update.
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.
Agreed. I'll try to make that happen.
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.
I'm having trouble reproducing that this was ever necessary. It seems to update just fine. Giving this the 🔪 and I think it's fine.
src/lib/keyed_container.js
Outdated
var VALUE = 2; | ||
var BOTH = 3; | ||
|
||
module.exports = function keyedContainer(baseObj, path, keyName, valueName) { |
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.
Interesting. I like it.
@rreusser can you of other situations where this could be useful?
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.
Yeah. It's also used in group styles, for one, though that case might be slightly more complicated. Also frames, though again, slightly more complicated. It's more a matter of realizing that unless we allow custom keys in the schema, abstracting this pattern seems like the only sane way forward.
src/plots/plots.js
Outdated
@@ -798,7 +798,7 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) { | |||
|
|||
for(i = 0; i < dataIn.length; i++) { | |||
trace = dataIn[i]; | |||
fullTrace = plots.supplyTraceDefaults(trace, cnt, fullLayout, i); | |||
fullTrace = plots.supplyTraceDefaults(trace, cnt, fullLayout, i, dataIn.length); |
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.
I would prefer stashing dataIn.length
on fullLayout
then passing it around like this.
Something like fullLayout._dataInLength
that could be set here side-by-side to fullLayout._dataLength
.
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.
Sounds good.
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.
Oh, actually layout._dataLength
is the length of the input data that I'm looking for. I think it's best not to make the count depend upon visibility (legend shouldn't fundamentally change when you toggle visibility, right?), so I think it's best to just use layout._dataLength
for this.
src/plots/plots.js
Outdated
|
||
// The group key gets cleared. If set, pass it forward | ||
if(expandedTrace._group) { | ||
fullExpandedTrace._group = expandedTrace._group; |
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.
Hmm. That's too bad. Do we really need this _group
, i.e. could just recompute it during Legend.draw
?
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.
Or maybe we could call relinkPrivateKeys
here, to make this solution generalizable?
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.
Yeah I didn't like it. I'll try to clean it up.
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.
Switched to relinkPrivateKeys(fullExpandedTrace, expandedTrace)
, which seems to me like probably the most general approach here as long as it's not too expensive.
@@ -35,6 +35,35 @@ exports.attributes = { | |||
'with `x` [1, 3] and one trace with `x` [2, 4].' | |||
].join(' ') | |||
}, | |||
nameformat: { |
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.
Bringing in @alexcjohnson @cldougl @chriddyp @cpsievert @monfera what do you think of this syntax?
A lot of existing attributes could benefit form special formatting character like this one. For example,
trace = {
y: [1, 2, 1]
marker: {
size: [20, 30, 10]
},
hovertext: '%marker.size'
}
would be a nice shortcut to show each point's marker.size
value on hover.
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.
Personally, I like %
. Though maybe I'd vote for enforcing it with {}
e.g. %{name}
similar to ES6 ${name}
.
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.
... and I would make user spell out %{name}
and %{group}
as opposed to e.g. %t
and %g
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.
I really like the %{group}
format.
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.
Yeah, this is cool! I hadn't thought about extending this to other keys but I can imagine that going off in all sorts of directions in the future (%{marker.size*5}
???).
%{...}
seems sufficiently useless on its own that we can do this without calling it a breaking change.
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.
Ah, I see. Like a general substitution framework? Yeah, the goal here was just a one-off custom substitution in which it only knows about a couple properties. But definitely possibilities. The only thing then would be choosing awkward names like %{group}
and %{name}
(instead of %{trace}
) so that it's backward compatibility in case we substitute trace properties instead of special/custom/ad hoc keys.
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.
@etpinard @alexcjohnson I've added Lib.templateString
that performs these substitutions. At the moment it just grabs object properties if it's simple or uses nestedProperty
if the key is more complicated than just a plain word. For this case the only data available is %{trace}
and %{group}
, but it at least opens the door to a unified interface for taking this further (or worst-case-scenario it's only a couple lines of code if it's not more generally useful). Feedback welcome.
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.
See:
Line 748 in 57dfe4a
lib.templateString = function(string, obj) { |
I've made the requested changes and have added a test for editing the legend that includes regular and grouped traces. I don't currently have anything further to add. |
@@ -277,4 +277,54 @@ describe('the register function', function() { | |||
|
|||
expect(Registry.transformsRegistry['mah-transform']).toBeDefined(); | |||
}); | |||
|
|||
describe('getTransformIndices', function() { |
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 the tests!
src/components/legend/draw.js
Outdated
} | ||
|
||
return p; | ||
return Plotly.restyle(gd, update, traceIndex); |
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.
fun 👍
src/transforms/groupby.js
Outdated
@@ -113,7 +113,7 @@ exports.supplyDefaults = function(transformIn, traceOut, layout, traceIn, inputT | |||
if(!enabled) return transformOut; | |||
|
|||
coerce('groups'); | |||
coerce('nameformat', inputTraceCount > 1 ? '%g (%t)' : '%g'); | |||
coerce('nameformat', layout._dataLength > 1 ? '%g (%t)' : '%g'); |
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.
This makes me very happy.
src/transforms/groupby.js
Outdated
'with trace name (`"%g (%t)"`). Available escape sequences are `%g`, which', | ||
'inserts the group name, and `%t`, which inserts the trace name. If grouping', | ||
'defaults to the group name (`"%{group}"`), otherwise defaults to the group name', | ||
'with trace name (`"%{group} (%{trace})"`). Available escape sequences are `%{group}`, which', |
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.
Shouldn't this be %{name}
instead of %{trace}
so that it corresponds to an attribute just like group
?
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.
... but wait, group
isn't a attribute either (groups
is though). So how should we do this?
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.
Yeah, you're right. There are two options here:
- Expose special sequences. That's what I've done here. So only
group
andtrace
are available. They're custom. - Make this a more general pattern of evaluating trace properties, in which case it would indeed be
%{name}
and… well, there's not really a property for the group unless we add it custom which is why I went with 1 here.
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.
Though to be honest, I'm starting to think we should intercept %{group}
and %{name}
for this special case but also allow more general evaluation.
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.
I'm starting to think we should intercept %{group} and %{name} for this special case
I'm starting to think the same. 👍 for the current implementation.
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.
One small change though, right? Perhaps templateString should accept multiple objects from which to pull values and it takes the first one with a value. Then group
and name
would be the first preference before grabbing trace
properties. Specifically:
Now:
Lib.templateString('%{group} (%{trace})', {group: ..., trace: ...})
Proposed:
Lib.templateString('%{group} (%{name})', {group: ..., name: ...}, traceObj);
So that technically %{marker.size}
would be equally valid.
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.
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.
Interesting. The only tricky part is that to be really precise, it would have to be %{transforms[2].group}
, which it then links up with the schema to realize the pluralized key is in the schema and is a data array, and can be indexed and expanded to %{transforms[2].groups[12]}
. The problem I see with that is that templateString
must then receive the schema and an item index and that the key must contain the transform number, both of which make it harder to implement and use. Correct me if I'm mistaken though.
src/plots/plots.js
Outdated
if(expandedTrace._group) { | ||
fullExpandedTrace._group = expandedTrace._group; | ||
} | ||
relinkPrivateKeys(fullExpandedTrace, expandedTrace); |
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.
Maybe add a comment here e.g.:
relink private (i.e. underscore) keys expanded trace to full expanded trace so that transform supply-default methods can set
_
keys for future use.
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.
✅
}).then(delay(20)); | ||
} | ||
|
||
it('sets and unsets trace group names', function(done) { |
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.
Lovely test.
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's testing a fair amount of feature actions which isn't the best, but it's not bad for end-to-end.
@rreusser any updates on this? My #1919 (comment) still stands as of today. We don't have to implement the proposed full attribute-value-to-text-replacement scheme in this PR. |
Sounds good @etpinard. Can you clarify that the comment you're referring to is that |
Exactly.
I guess |
Thanks for clarifying @etpinard. If that's the case, then I think this is complete. You can see here that |
Confirming 💃ability then, @etpinard / @alexcjohnson ? |
src/transforms/groupby.js
Outdated
'default "%{group} (%{trace})" would return "Monaco (GDP per capita)".' | ||
].join(' ') | ||
}, | ||
groupnames: { |
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.
Hmm. I missed this during my first review. I'm not a fan of groupnames
being an array container.
We could instead declare groupnames
as a data_array
and expecting its length to be equal to groups.length
, but that would lead to some (maybe a lot of) duplication.
Maybe we could groupname
attribute to the styles
container below instead which already expects a target group?
Actually, do we even need a groupnames
attribute? groups
items can be any strings, so setting groupnames[i].name
is equivalent to changing the corresponding groups
item.
I apologize for not catching this sooner.
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.
setting
groupnames[i].name
is equivalent to changing the correspondinggroups
item.
Not quite - the name will be set by nameformat
- ie a combination of the group value and the trace name by default - but when you want to override that (perhaps by typing something specific in the editable legend) you'll need this.
We could instead declare
groupnames
as adata_array
and expecting its length to be equal togroups.length
, but that would lead to some (maybe a lot of) duplication.
Or ambiguity, if the names were not perfectly duplicated. What would that mean?
Maybe we could
groupname
attribute to thestyles
container below instead which already expects a target group?
That sounds great! so each entry in styles
would have target
and optionally value
and/or name
? No need to have two separate containers both keyed on the group 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.
The main problem is that groups
have no correspondence with groupnames
, which I think rules out a plain array for the API. I think there's a good argument to be made though for merging styles
with groupnames
. Actually in hindsight, that does seem best to me. Not sure why I didn't realize that sooner…
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.
That sounds great! so each entry in styles would have target and optionally value and/or name?
Exactly. I'd vote for name
over value
, but I don't have a strong opinion here.
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.
value
is all the other styles... actually, would it work to just put name
inside value
, and not have to create any new API? We'd still of course have to point the editing routine at styles[i].value.name
but seems like that's all, right?
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.
I think that's the best option. 👍 Then it really is just a style property (which could be set anyway and in fact we'd be doing something surprising not to use what's already possible).
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.
FWIW, IMO, TBH, I'm even happier now that I took the time to implement keyedContainer…
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.
would it work to just put name inside value
good call 👍
9c4c4b3
to
be0e693
Compare
src/lib/keyed_container.js
Outdated
// If it's not an unset, force that bit to be unset. This is all related to the fact | ||
// that undefined and null are a bit specially implemented in nestedProperties. | ||
if(value !== null) { | ||
changeType = changeType & ~UNSET; |
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 does & ~4
do?
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.
I'm tracking what needs to be updated with a bit mask (which, before adding a bit to track whether it's unset in a way that nestedProperty
needs to handle specially, originally made 10% more sense since everything else was strictly an OR operation). & ~4 unsets precisely the 4 bit.
Well-earned 💃 @rreusser Before merging, would you mind shedding some light about those bitwise operations (in a PR comment or in the code directly, whatever you prefer). Also, could you update the PR header (which still mentions |
I'm having second thoughts about storing |
After a brief offline conversation with @alexcjohnson, I have to agree that the current API seems best and that it should be a concern of, for example, the UI to construct the proper update that doesn't unset the name accidentally. With that then, I'm content. Below is the last case that was rather tricky. If you remove styles items or slide them over to fill unused gaps in the container array, the marker size accidentally gets dropped. I fixed it by just leaving unused container in place rather than compressing the group styles. |
src/components/legend/draw.js
Outdated
|
||
astr = direction + '.name'; | ||
if(BLANK_STRING_REGEX.test(origText)) { |
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.
hmm, there is a use case for a blank name - maybe less so for groupby than for regular traces, but sometimes you want the title for one trace to stand in for a few traces below it. You could argue then that this should just be testing for an actual empty string ''
but a space ' '
should be allowed as the name.
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.
okay, I could just check === ''
instead.
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.
✅
💃 (again 😄) |
Okay, merging this at last. This API seems correct this way, though it makes the updates a bit tricky to compute. Improvements like compressing unused groups out of the picture would be a nice, backwards-compatible enhancement. |
See #1893
This PR adds the ability to rename grouped traces. The logic is as follows:
nameformat
as agroupby
setting. The default is%g (%t)
, which expands out into, for example, "groupName (traceName)". (This part is really easy to change. Feedback welcome.)styles
The result is that you can apply intelligent naming and also have the ability to override expanded trace names by modifying the legend.
To accomplish the
styles
part, I've implementedLib.keyedContainer
, which manages modifying and constructing updates for objects like[{name: ..., value: ...}]
, which are becoming numerous and kind of unwieldy to manage.As a result of the discussion below, the syntax for naming groups is now integrated with styles:
To do:
groupnames
astype: "any"
because I need to figure out how to make this a properly nested container array.groupName (traceName)
format were only applied when there's more than one trace, but, unless I'm mistaken, we'd need to expose a bit more global state to transform supplyDefaults, which needs just a bit of thought to make sure we really can't avoid it. 😬/cc @etpinard @alexcjohnson