/// <summary> /// Creates the sample graph. /// </summary> private void CreateSampleGraph(IGraph graph) { INode n1, n2, n3, n4; layerIndices[n1 = graph.CreateNode()] = 0; layerIndices[n2 = graph.CreateNode()] = 1; layerIndices[n3 = graph.CreateNode()] = 2; layerIndices[n4 = graph.CreateNode()] = 2; graph.CreateEdge(n1, n2); graph.CreateEdge(n2, n3); graph.CreateEdge(n1, n4); // create an HierarchicLayout instance to provide an initial layout var hl = CreateLayout(); // use the GivenLayersLayerer to respect the above node to layer assignment hl.FromScratchLayerer = new GivenLayersLayerer(); // provide additional data to configure the layout // respect the above node to layer assignment var hlData = new HierarchicLayoutData { GivenLayersLayererIds = { Mapper = layerIndices } }; // run the layout graph.ApplyLayout(hl, hlData); // and update the layer visualization layerVisualCreator.UpdateLayers(graph, layerIndices); }
/// <summary> /// Does a tree layout of the graph. /// The layout and assistant attributes from the business data of the employees are used to /// guide the the layout. /// </summary> public void DoLayout() { IGraph tree = graphControl.Graph; var layoutData = CreateLayoutData(tree, null); tree.ApplyLayout(new BendDuplicatorStage(new TreeLayout()), layoutData); }
/// <summary> /// Initializes the graph instance setting default styles /// and creating a small sample graph. /// </summary> protected virtual void InitializeGraph() { IGraph graph = Graph; // load a sample graph new GraphMLIOHandler().Read(graph, "Resources/sample.graphml"); // set some defaults graph.NodeDefaults.Style = Enumerable.First(graph.Nodes).Style; graph.NodeDefaults.ShareStyleInstance = true; // we start with a simple run of OrganicLayout to get a good starting result // the algorithm is optimized to "unfold" graphs quicker than // interactive organic, so we use this result as a starting solution var initialLayout = new OrganicLayout { MinimumNodeDistance = 50 }; graph.ApplyLayout(initialLayout); // center the initial graph GraphControl.FitGraphBounds(); movedNodes = new List <INode>(); // we wrap the PositionHandler for nodes so that we always have the collection of nodes // that are currently being moved available in "movedNodes". // this way we do not need to know how the node is moved and do not have to guess // what elements are currently being moved based upon selection, etc. graph.GetDecorator().NodeDecorator.PositionHandlerDecorator.SetImplementationWrapper( (item, implementation) => new CollectingPositionHandlerWrapper(item, movedNodes, implementation)); // create a copy of the graph for the layout algorithm LayoutGraphAdapter adapter = new LayoutGraphAdapter(graphControl.Graph); copiedLayoutGraph = adapter.CreateCopiedLayoutGraph(); // create and start the layout algorithm layout = StartLayout(); WakeUp(); // register a listener so that structure updates are handled automatically graph.NodeCreated += delegate(object source, ItemEventArgs <INode> args) { if (layout != null) { var center = args.Item.Layout.GetCenter(); layout.SyncStructure(true); //we nail down all newly created nodes var copiedNode = copiedLayoutGraph.GetCopiedNode(args.Item); layout.SetCenter(copiedNode, center.X, center.Y); layout.SetInertia(copiedNode, 1); layout.SetStress(copiedNode, 0); layout.WakeUp(); } }; graph.NodeRemoved += OnStructureChanged; graph.EdgeCreated += OnStructureChanged; graph.EdgeRemoved += OnStructureChanged; }
private void RunMultipageLayout() { // parse the pageWidth and pageHeight parameters double pageWidth; double pageHeight; if (!Double.TryParse(pageWidthTextBox.Text, out pageWidth)) { pageWidth = 800; } if (!Double.TryParse(pageHeightTextBox.Text, out pageHeight)) { pageHeight = 800; } // get the core layout string coreLayoutKey = coreLayoutComboBox.SelectedItem as string; if (coreLayoutKey != null && coreLayouts.ContainsKey(coreLayoutKey)) { coreLayout = coreLayouts[coreLayoutKey]; } else { coreLayout = coreLayouts["Hierarchic"]; } // a data provider for the node, edge, and label IDs: // this data provider returns the node/edge/label instances themselves var multiPageLayoutData = new MultiPageLayoutData { NodeIds = { Delegate = node => node }, EdgeIds = { Delegate = edge => edge }, NodeLabelIds = { Delegate = label => label }, EdgeLabelIds = { Delegate = label => label }, AbortHandler = abortHandler = new AbortHandler() }; // apply the multi page layout // multiPageLayout contains a list with the single page graphs MultiPageLayout multiPageLayout = new MultiPageLayout(coreLayout) { MaximumPageSize = new YDimension(pageWidth, pageHeight), LayoutCallback = new DelegateLayoutCallback(result => BeginInvoke(new Action(() => { ApplyLayoutResult(result, pageWidth, pageHeight); abortHandler = null; ShowLoadingIndicator(false); // force to update the command state CommandManager.InvalidateRequerySuggested(); }))) }; // execute layout in thread to prevent ui blocking new Thread(() => { try { modelGraph.ApplyLayout(multiPageLayout, multiPageLayoutData); } catch (AlgorithmAbortedException) { // layout was aborted BeginInvoke(new Action(() => { // reset abortHandler and loading indicator in the view thread abortHandler = null; ShowLoadingIndicator(false); })); } }).Start(); }