// Creates a collection of randomly colored nodes. private ObservableCollection <SimpleData> GenerateNodes() { var nodeSource = new ObservableCollection <SimpleData>(); int minNodes = 20; int maxNodes = 50; int numberOfNodes = rand.Next(minNodes, maxNodes + 1); for (int i = 0; i < numberOfNodes; i++) { nodeSource.Add(new SimpleData() { Key = "Node" + i.ToString(), Color = String.Format("#{0:X}{1:X}{2:X}", 120 + rand.Next(100), 120 + rand.Next(100), 120 + rand.Next(100)) }); } // Randomize the nodes a little: for (int i = 0; i < nodeSource.Count; i++) { int swap = rand.Next(0, nodeSource.Count); SimpleData temp = nodeSource[swap]; nodeSource[swap] = nodeSource[i]; nodeSource[i] = temp; } return(nodeSource); }
// Takes the random collection of nodes and creates a random tree with them. private ObservableCollection <LinkData> GenerateLinks(ObservableCollection <SimpleData> nodes) { var linkSource = new ObservableCollection <LinkData>(); if (nodes.Count == 0) { return(linkSource); } int minLinks = 2; int maxLinks = 4; List <SimpleData> available = nodes.ToList <SimpleData>(); foreach (SimpleData next in nodes) { available.Remove(next); int children = rand.Next(minLinks, maxLinks + 1); for (int i = 1; i <= children; i++) { if (available.Count == 0) { break; } SimpleData to = available[0]; available.Remove(to); linkSource.Add(new LinkData() { From = next.Key, To = to.Key }); } } return(linkSource); }