@@ -34,6 +34,9 @@ public class ExportGraphViewCmdLet : PSCmdlet
3434 [ ValidateNotNullOrEmpty ]
3535 public string Path ;
3636
37+ [ Parameter ( Mandatory = false ) ]
38+ public SwitchParameter UseVirtualTreeRoot ;
39+
3740 private VegaExportTypes _vegaExportType = VegaExportTypes . JSON ;
3841
3942
@@ -114,24 +117,58 @@ protected override void ProcessRecord()
114117 private string ExportVegaTreeLayout ( VegaExportTypes exportType )
115118 {
116119 var modulePath = MyInvocation . MyCommand . Module ? . ModuleBase ;
117- var records = Graph . ConvertToParentChildList ( ) ;
118120 var vega = VegaHelper . GetVegaTemplateObjectFromModulePath ( modulePath , "vega.tree.layout.json" ) ;
119- vega . Data [ 0 ] . Values = records . ToList < object > ( ) ;
120121
121- switch ( exportType )
122+ if ( Graph . Roots ( ) . Count ( ) == 0 )
122123 {
123- case VegaExportTypes . HTML :
124- return VegaHelper . RenderHtml ( vega ) ;
125- default :
126- case VegaExportTypes . JSON :
127- return vega . ToJson ( ) ;
128- }
124+ var records = Graph . ConvertToParentChildList ( ) ;
125+ vega . Data [ 0 ] . Values = records . ToList < object > ( ) ;
129126
130- //string html = VegaHelper.RenderHtml(vega);
131-
132- //return html;
127+ switch ( exportType )
128+ {
129+ case VegaExportTypes . HTML :
130+ return VegaHelper . RenderHtml ( vega ) ;
131+ default :
132+ case VegaExportTypes . JSON :
133+ return vega . ToJson ( ) ;
134+ }
135+ }
136+ else
137+ {
138+ if ( ! UseVirtualTreeRoot . IsPresent )
139+ {
140+ WriteError ( new ErrorRecord (
141+ new InvalidOperationException ( "The graph cannot be exported as a tree. It has multiple roots." ) ,
142+ "PSGraph.ExportVegaTreeLayout.MultipleRoots" ,
143+ ErrorCategory . InvalidOperation ,
144+ Graph ) ) ;
145+ return string . Empty ;
146+ }
147+ else
148+ {
149+ var tmpGraph = Graph . Clone ( ) ;
150+ var roots = tmpGraph . Roots ( ) ;
151+ var virtualRoot = new PSVertex ( "VirtualRoot Node" ) ;
152+ tmpGraph . AddVertex ( virtualRoot ) ;
153+ foreach ( var root in roots )
154+ {
155+ tmpGraph . AddVerticesAndEdge ( new PSEdge ( virtualRoot , root , new PSEdgeTag ( string . Empty ) ) ) ;
156+ }
157+
158+ var records = tmpGraph . ConvertToParentChildList ( ) ;
159+ vega . Data [ 0 ] . Values = records . ToList < object > ( ) ;
160+
161+ switch ( exportType )
162+ {
163+ case VegaExportTypes . HTML :
164+ return VegaHelper . RenderHtml ( vega ) ;
165+ default :
166+ case VegaExportTypes . JSON :
167+ return vega . ToJson ( ) ;
168+ }
133169
134- //File.WriteAllText(Path, html);
170+ }
171+ }
135172 }
136173
137174 private string ExportVegaAdjacencyMatrix ( VegaExportTypes exportType )
0 commit comments