Skip to content

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

Merged
merged 19 commits into from
Aug 15, 2017
Merged

Add ability to rename grouped traces #1919

merged 19 commits into from
Aug 15, 2017

Conversation

rreusser
Copy link
Contributor

@rreusser rreusser commented Jul 27, 2017

See #1893

This PR adds the ability to rename grouped traces. The logic is as follows:

  • provides nameformat as a groupby setting. The default is %g (%t), which expands out into, for example, "groupName (traceName)". (This part is really easy to change. Feedback welcome.)
  • You can provide overrides in 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 implemented Lib.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:

{
  type: 'groupby',
  styles: [{
    target: 'a',
    value: {name: 'foo'}
  }]
}

To do:

  • I've left groupnames as type: "any" because I need to figure out how to make this a properly nested container array.
  • I'd love it if the 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. 😬
  • Tests. (Holding off until confirmed that this is a good approach).

ezgif-2-6729e8cf33

/cc @etpinard @alexcjohnson

@rreusser
Copy link
Contributor Author

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.

@rreusser
Copy link
Contributor Author

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 totalTraceCount all the way through to transform supplyDefaults to accomplish this, which is ugly, though it does work. Very open to feedback on any of this PR (including just scrapping/rethinking it).

update[direction + '.name'] = text;
} else if(fullInput.transforms) {
for(i = fullInput.transforms.length - 1; i >= 0; i--) {
if(fullInput.transforms[i].type === 'groupby') {
Copy link
Contributor

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')

Copy link
Contributor Author

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

// string is retained.
if(needsRedraw) {
p = p.then(function() {
return Plotly.redraw(gd);
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

var VALUE = 2;
var BOTH = 3;

module.exports = function keyedContainer(baseObj, path, keyName, valueName) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@@ -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);
Copy link
Contributor

@etpinard etpinard Jul 28, 2017

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Contributor Author

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.


// The group key gets cleared. If set, pass it forward
if(expandedTrace._group) {
fullExpandedTrace._group = expandedTrace._group;
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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: {
Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@rreusser rreusser Aug 1, 2017

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See:

lib.templateString = function(string, obj) {

@etpinard etpinard added the feature something new label Jul 31, 2017
@rreusser
Copy link
Contributor Author

rreusser commented Aug 1, 2017

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() {
Copy link
Contributor

@etpinard etpinard Aug 2, 2017

Choose a reason for hiding this comment

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

thanks for the tests!

}

return p;
return Plotly.restyle(gd, update, traceIndex);
Copy link
Contributor

Choose a reason for hiding this comment

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

fun 👍

@@ -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');
Copy link
Contributor

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.

'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',
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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:

  1. Expose special sequences. That's what I've done here. So only group and trace are available. They're custom.
  2. 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.

Copy link
Contributor Author

@rreusser rreusser Aug 2, 2017

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.

Copy link
Contributor

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.

Copy link
Contributor Author

@rreusser rreusser Aug 2, 2017

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

This plural vs singular problem came up too in #1770 ➡️ here - where values of plural attributes are listed using a singular (grammatically) key.

Maybe we should start declaring corresponding singular key in the plot schema and have Lib.templateString and appendArrayPointValue use it.

Copy link
Contributor Author

@rreusser rreusser Aug 2, 2017

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.

if(expandedTrace._group) {
fullExpandedTrace._group = expandedTrace._group;
}
relinkPrivateKeys(fullExpandedTrace, expandedTrace);
Copy link
Contributor

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Lovely test.

Copy link
Contributor Author

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.

@etpinard etpinard added this to the v1.30.0 milestone Aug 4, 2017
@etpinard
Copy link
Contributor

etpinard commented Aug 7, 2017

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

@rreusser
Copy link
Contributor Author

rreusser commented Aug 7, 2017

Sounds good @etpinard. Can you clarify that the comment you're referring to is that %{group} and %{trace} seem reasonable even though they're not attributes? Does that conflict with future-proofing this in case we do make them attributes? In other words, I'm just worried that if we make them special cases now, we'll never be able to remove them.

@etpinard
Copy link
Contributor

etpinard commented Aug 7, 2017

that %{group} and %{trace} seem reasonable even though they're not attributes?

Exactly.

Does that conflict with future-proofing this in case we do make them attributes?

%{trace} should never conflict (having attributes named trace in trace object would be very 😕 )

I guess %{group} could conflict with e.g. %{transforms[i].groups[j] (not sure what the indices mean here), but to me, it's definitively a special that we should expose.

@rreusser
Copy link
Contributor Author

rreusser commented Aug 7, 2017

Thanks for clarifying @etpinard. If that's the case, then I think this is complete. You can see here that group and trace are currently the only attributes exposed, though templateString is in principle more powerful and its use could be expanded (though if not, it's only a couple lines of code anyway so not a big deal).

@rreusser
Copy link
Contributor Author

rreusser commented Aug 7, 2017

Confirming 💃ability then, @etpinard / @alexcjohnson ?

'default "%{group} (%{trace})" would return "Monaco (GDP per capita)".'
].join(' ')
},
groupnames: {
Copy link
Contributor

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.

Copy link
Collaborator

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 corresponding groups 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 a data_array and expecting its length to be equal to groups.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 the styles 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.

Copy link
Contributor Author

@rreusser rreusser Aug 7, 2017

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…

Copy link
Contributor

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

@rreusser rreusser Aug 7, 2017

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

Copy link
Contributor Author

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…

Copy link
Contributor

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 👍

@rreusser rreusser force-pushed the rename-grouped-traces branch from 9c4c4b3 to be0e693 Compare August 15, 2017 00:47
// 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;
Copy link
Contributor

@etpinard etpinard Aug 15, 2017

Choose a reason for hiding this comment

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

What does & ~4 do?

Copy link
Contributor Author

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.

@etpinard
Copy link
Contributor

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 groupnames) for us potentially looking over this PR again in the future?

@rreusser
Copy link
Contributor Author

rreusser commented Aug 15, 2017

I'm having second thoughts about storing name inside styles because removing styles is how the UI reverts a group's styles to the defaults. That means reverting to default styles will unset the name, which suggests the nesting is a bit incorrect. I think it might be better to store the name next to the styles, which would then allow them to operate separately. Long story short, managing these container array objects in any general sense is rather more convoluted than it first appears. 😕 /cc @alexcjohnson @etpinard

@rreusser
Copy link
Contributor Author

rreusser commented Aug 15, 2017

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.

ezgif-4-24052e288f


astr = direction + '.name';
if(BLANK_STRING_REGEX.test(origText)) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexcjohnson
Copy link
Collaborator

💃 (again 😄)

@rreusser
Copy link
Contributor Author

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.

@rreusser rreusser merged commit 4ae95dd into master Aug 15, 2017
@rreusser rreusser deleted the rename-grouped-traces branch August 16, 2017 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants