@@ -35,6 +35,24 @@ exports.attributes = {
3535 'with `x` [1, 3] and one trace with `x` [2, 4].'
3636 ] . join ( ' ' )
3737 } ,
38+ namepattern : {
39+ valType : 'string' ,
40+ dflt : '%g (%t)' ,
41+ description : [
42+ 'Pattern by which grouped traces are named. Available special escape' ,
43+ 'sequences are `%g`, which inserts the group name, and `%t`, which' ,
44+ 'inserts the trace name. If grouping GDP data by country, for example' ,
45+ 'The default "%g (%t)" would return "Monaco (GDP per capita)".'
46+ ] . join ( ' ' )
47+ } ,
48+ groupnames : {
49+ valType : 'any' ,
50+ description : [
51+ 'An array of trace names based on group name. Each entry must be an' ,
52+ 'object `{name: "group", value: "trace name"}` which is then applied' ,
53+ 'to the particular group, overriding the name derived from `namepattern`.'
54+ ] . join ( ' ' )
55+ } ,
3856 styles : {
3957 _isLinkedToArray : 'style' ,
4058 target : {
@@ -83,6 +101,8 @@ exports.supplyDefaults = function(transformIn) {
83101 if ( ! enabled ) return transformOut ;
84102
85103 coerce ( 'groups' ) ;
104+ coerce ( 'groupnames' ) ;
105+ coerce ( 'namepattern' ) ;
86106
87107 var styleIn = transformIn . styles ;
88108 var styleOut = transformOut . styles = [ ] ;
@@ -130,9 +150,15 @@ exports.transform = function(data, state) {
130150 return newData ;
131151} ;
132152
153+ function computeName ( pattern , traceName , groupName ) {
154+ return pattern . replace ( / % g / g, groupName )
155+ . replace ( / % t / g, traceName ) ;
156+ }
157+
133158
134159function transformOne ( trace , state ) {
135160 var i , j , k , attr , srcArray , groupName , newTrace , transforms , arrayLookup ;
161+ var groupNameObj ;
136162
137163 var opts = state . transform ;
138164 var groups = trace . transforms [ state . transformIndex ] . groups ;
@@ -153,6 +179,10 @@ function transformOne(trace, state) {
153179 styleLookup [ styles [ i ] . target ] = styles [ i ] . value ;
154180 }
155181
182+ if ( opts . groupnames ) {
183+ groupNameObj = Lib . keyedContainer ( opts , 'groupnames' ) ;
184+ }
185+
156186 // An index to map group name --> expanded trace index
157187 var indexLookup = { } ;
158188
@@ -162,7 +192,18 @@ function transformOne(trace, state) {
162192
163193 // Start with a deep extend that just copies array references.
164194 newTrace = newData [ i ] = Lib . extendDeepNoArrays ( { } , trace ) ;
165- newTrace . name = groupName ;
195+ newTrace . _group = groupName ;
196+
197+ var suppliedName = null ;
198+ if ( groupNameObj ) {
199+ suppliedName = groupNameObj . get ( groupName ) ;
200+ }
201+
202+ if ( suppliedName ) {
203+ newTrace . name = suppliedName ;
204+ } else {
205+ newTrace . name = computeName ( opts . namepattern , trace . name , groupName ) ;
206+ }
166207
167208 // In order for groups to apply correctly to other transform data (e.g.
168209 // a filter transform), we have to break the connection and clone the
0 commit comments