@@ -275,29 +275,25 @@ HTMLWidgets.widget({
275275 continue ;
276276 }
277277
278+ // set defaults for this keySet
279+ // note that we don't track the nested property (yet) since we always
280+ // emit the union -- http://cpsievert.github.io/talks/20161212b/#21
281+ keysBySet [ trace . set ] = keysBySet [ trace . set ] || {
282+ value : [ ] ,
283+ _isSimpleKey : trace . _isSimpleKey
284+ } ;
285+
278286 // selecting a point of a "simple" trace means: select the
279287 // entire key attached to this trace, which is useful for,
280288 // say clicking on a fitted line to select corresponding observations
281- if ( trace . _isSimpleKey ) {
282- keysBySet [ trace . set ] = { value : trace . key , _isSimpleKey : true } ;
283- // TODO: could this be made more efficient by looping at the trace level first?
284- continue ;
285- }
286-
287- // set defaults for this key set
288- keysBySet [ trace . set ] = keysBySet [ trace . set ] ||
289- { value : [ ] , _isSimpleKey : false } ;
290-
291- // the key for this point (could be "nested" -- i.e. a 2D array)
292- var key = trace . key [ points [ i ] . pointNumber ] ;
293- if ( trace . _isNestedKey ) {
294- // TODO: is this faster than pushing?
295- keysBySet [ trace . set ] . value = keysBySet [ trace . set ] . value . concat ( key ) ;
296- } else {
297- keysBySet [ trace . set ] . value . push ( key ) ;
298- }
289+ var key = trace . _isSimpleKey ? trace . key : trace . key [ points [ i ] . pointNumber ] ;
290+ // http://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript
291+ var keyFlat = trace . _isNestedKey ? [ ] . concat . apply ( [ ] , key ) : key ;
299292
293+ // TODO: better to only add new values?
294+ keysBySet [ trace . set ] . value = keysBySet [ trace . set ] . value . concat ( keyFlat ) ;
300295 }
296+
301297 return keysBySet ;
302298 }
303299
0 commit comments