/// <summary> /// Callback used by <see cref="GraphEditorInputMode"/> to actually create a node upon a click. /// </summary> /// <remarks> /// This method creates a dummy business object and associated it with a newly created node. /// </remarks> private INode CreateNode(IInputModeContext context, IGraph graph, PointD location, INode parent) { Customer c = new Customer("Sample Customer", "Your Computer", new Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds).Next(99999)); var simpleNode = new SimpleNode { Tag = c, Style = customerNodeStyle, Layout = new MutableRectangle(0, 0, 10, 10) }; var preferredSize = customerNodeStyle.GetPreferredSize(graphControl.CreateRenderContext(), simpleNode); return(graph.CreateNode(RectD.FromCenter(location, preferredSize), customerNodeStyle, c)); }
/// <summary> /// Initializes the graph instance setting default styles /// and creating a small sample graph. /// </summary> protected virtual async Task InitializeGraph() { IGraph graph = graphControl.Graph; // set the style as the default for all new nodes graph.NodeDefaults.Style = defaultStyle; // let the node decide how much space it needs and make sure it doesn't get any smaller. graph.NodeDefaults.Size = defaultStyle.GetPreferredSize(graphControl.CreateRenderContext(), new SimpleNode { Tag = new LayerConstraintsInfo() }); defaultStyle.MinimumSize = graph.NodeDefaults.Size; // create a simple label style DefaultLabelStyle labelStyle = new DefaultLabelStyle { Typeface = new Typeface("Arial"), BackgroundBrush = Brushes.White, AutoFlip = true }; // set the style as the default for all new node labels graph.NodeDefaults.Labels.Style = labelStyle; graph.EdgeDefaults.Labels.Style = labelStyle; graph.EdgeDefaults.Labels.LayoutParameter = new EdgeSegmentLabelModel().CreateDefaultParameter(); // create the graph and perform a layout operation CreateNewGraph(); await DoLayout(); }
/// <summary> /// Creates a simple model - this method is not used in this demo - instead the graph is loaded from /// the included GraphML file. /// </summary> private void CreateModel() { // remember the mapping for each object to the created node var objectMapping = new Dictionary <object, INode>(); // set the default node style.. Graph.NodeDefaults.Style = customerNodeStyle; // and create some nodes - and associate them with a tag. var customers = new[] { new Customer("Lucy Osbourne", "Arizona", 13413), new Customer("Stephanie Cornwell", "Oregon", 13414), new Customer("Mark Wright", "Michigan", 13415), new Customer("Ruby Turner", "South Carolina", 13416), new Customer("Norman Johnson", "Montana", 13417) }; foreach (var c in customers) { // calculate the preferred size before creating the node var simpleNode = new SimpleNode { Tag = c, Style = customerNodeStyle, Layout = new MutableRectangle(0, 0, 10, 10) }; var preferredSize = customerNodeStyle.GetPreferredSize(graphControl.CreateRenderContext(), simpleNode); // then create the node objectMapping[c] = Graph.CreateNode(new RectD(new PointD(), preferredSize), customerNodeStyle, c); } // create the products Graph.NodeDefaults.Style = productNodeStyle; // and the nodes.... var products = new[] { new Product("Donut Maker", 8971, true), new Product("Snow Boots", 8972, true), new Product("Cowboy Hat", 8973, false), }; foreach (var p in products) { // calculate the preferred size before creating the node var simpleNode = new SimpleNode { Tag = p, Style = productNodeStyle, Layout = new MutableRectangle(0, 0, 10, 10) }; var preferredSize = productNodeStyle.GetPreferredSize(graphControl.CreateRenderContext(), simpleNode); // then create the node objectMapping[p] = Graph.CreateNode(new RectD(new PointD(), preferredSize), productNodeStyle, p); } var relations = new[] { new Relation { Customer = customers[0], Product = products[0] }, new Relation { Customer = customers[0], Product = products[1] }, new Relation { Customer = customers[0], Product = products[2] }, new Relation { Customer = customers[1], Product = products[0] }, new Relation { Customer = customers[1], Product = products[2] }, new Relation { Customer = customers[2], Product = products[1] }, new Relation { Customer = customers[2], Product = products[2] }, new Relation { Customer = customers[3], Product = products[1] }, new Relation { Customer = customers[4], Product = products[2] }, }; // now add the edges using the stored mapping between products/customers and INodes foreach (var relation in relations) { var edge = Graph.CreateEdge(objectMapping[relation.Customer], objectMapping[relation.Product]); // and add a label var label = Graph.AddLabel(edge, relation.ToString()); label.Tag = relation; } }
/// <summary> /// Creates a GraphBuilder instance that's preconfigured with our demo's styles. /// </summary> internal static (GraphBuilder, NodesSource <INeo4jNode>, EdgesSource <IRelationship>) CreateGraphBuilder(GraphControl graphControl, IGraph graph, List <INeo4jNode> nodes, List <IRelationship> edges) { var builder = new GraphBuilder(graph); var nodesSource = builder.CreateNodesSource(nodes, n => n.Id); nodesSource.NodeCreator.TagProvider = n => n; var nodeStyle = new NodeControlNodeStyle("NodeStyle"); nodesSource.NodeCreator.Defaults.Style = nodeStyle; var edgesSource = builder.CreateEdgesSource(edges, e => e.StartNodeId, e => e.EndNodeId, e => e.Id); edgesSource.EdgeCreator.Defaults.Style = new BezierEdgeStyle { TargetArrow = Arrows.Default }; var labelBinding = edgesSource.EdgeCreator.CreateLabelBinding(item => item.Type); labelBinding.Defaults.LayoutParameter = new EdgeSegmentLabelModel().CreateParameterFromSource(0, 0, EdgeSides.AboveEdge); var context = graphControl.CreateRenderContext(); builder.NodeCreated += (sender, e) => { // Ensure that nodes have the correct size e.Graph.SetNodeLayout(e.Item, RectD.FromCenter(e.Item.Layout.GetCenter(), nodeStyle.GetPreferredSize(context, e.Item))); }; return(builder, nodesSource, edgesSource); }