@@ -3,7 +3,26 @@ import {maybeTransposeData} from './index';
33
44const SRC_ATTR_PATTERN = / s r c $ / ;
55
6- export default function dereference ( container , dataSources , config = { deleteKeys : false } ) {
6+ export function getColumnNames ( srcArray , dataSourceOptions ) {
7+ return srcArray
8+ . map ( src => {
9+ const columns = dataSourceOptions . filter ( dso => dso . value === src ) ;
10+ if ( columns . length === 1 ) {
11+ return columns [ 0 ] . columnName || columns [ 0 ] . label ;
12+ }
13+ return '' ;
14+ } )
15+ . join ( ' - ' ) ;
16+ }
17+
18+ export default function dereference (
19+ container ,
20+ dataSources ,
21+ config = { deleteKeys : false } ,
22+ dataSourceOptions = null
23+ ) {
24+ const containerIsData = Array . isArray ( container ) ;
25+
726 const replacer = ( key , parent , srcPath ) => {
827 if ( ! SRC_ATTR_PATTERN . test ( key ) ) {
928 return ;
@@ -34,22 +53,31 @@ export default function dereference(container, dataSources, config = {deleteKeys
3453 return ;
3554 }
3655
37- if ( Array . isArray ( container ) ) {
38- // Case where we were originally given data to dereference
39- const traceType = parent . type ;
40- parent [ dataKey ] = maybeTransposeData ( dereferencedData , srcPath , traceType ) ;
56+ if ( containerIsData ) {
57+ if ( parent . type !== null ) {
58+ // we're at the top level of the trace
59+ if ( dataSourceOptions !== null ) {
60+ parent . meta = parent . meta || { } ;
61+ parent . meta . columnNames = parent . meta . columnNames || { } ;
62+ parent . meta . columnNames [ dataKey ] = getColumnNames ( srcRef , dataSourceOptions ) ;
63+ }
64+ parent [ dataKey ] = maybeTransposeData ( dereferencedData , srcPath , parent . type ) ;
65+ } else {
66+ parent [ dataKey ] = dereferencedData ;
67+ }
4168 } else {
42- // This means we're dereferencing layout
69+ // container is layout
4370 parent [ dataKey ] = dereferencedData ;
4471 }
4572 } ;
4673
47- if ( Array . isArray ( container ) ) {
74+ if ( containerIsData ) {
4875 walkObject ( container , replacer , {
4976 walkArraysMatchingKeys : [ 'data' , 'transforms' ] ,
5077 pathType : 'nestedProperty' ,
5178 } ) ;
5279 } else {
80+ // container is layout
5381 walkObject ( container , replacer , { pathType : 'nestedProperty' } ) ;
5482 }
5583}
0 commit comments