public NodeGraph GenerateDesignGraph(int count) { if (DataSource.Count < count) { ResetDataSource(count); } //if (DataSource.Count < count) var result = new NodeGraph(); Random rand = new Random(); try { //Create and add vertices using some DataSource for ID's foreach (var item in DataSource.Take(count)) { result.AddVertex(new DataVertex() { ID = item.ID, Text = item.Text }); } var vlist = result.Vertices.ToList(); //Generate random edges for the vertices int c = 0; foreach (var vertex1 in vlist) { //if (rand.Next(0, count) > count/10) continue; var vertex2 = vlist[rand.Next(0, result.VertexCount - 1)]; result.AddEdge(new DataEdge(vertex1, vertex2, 1) { Text = string.Format("{0} -> {1}", vertex1, vertex2) }); if (c % 5 == 0) { var vertex3 = vlist[rand.Next(0, result.VertexCount - 1)]; result.AddEdge(new DataEdge(vertex1, vertex3, 0.5) { Text = string.Format("{0} -> {1}", vertex1, vertex2) }); } c++; } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }
private void initGraphButton_Click(object sender, EventArgs e) { graph = new NodeGraph(); try { adjmatrix = Helper.MatrixFromString(matrixBox.Text); } catch (FormatException ex) { MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int nodeCount = adjmatrix.GetLength(0); vertices = new Vertex[nodeCount]; for (int k = 0; k < nodeCount; k++) { vertices[k] = new Vertex(k.ToString(), 1.0, 0); graph.AddVertex(vertices[k]); } //dreadnaut für orbits starten string dreadnautcmd = "n=" + nodeCount + " g "; for (int irow = 0; irow < nodeCount; irow++) { for (int icol = 0; icol < nodeCount; icol++) { if (adjmatrix[irow, icol] != 0) { graph.AddEdge(new Edge<Vertex>(vertices[irow], vertices[icol])); dreadnautcmd += icol + " "; } } dreadnautcmd += ";"; } dreadnautcmd += "x o q"; string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "dreadnaut.exe"); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.FileName = path; Process process = new Process(); process.StartInfo = startInfo; try { System.IO.File.WriteAllBytes(path, ClusterNum.Properties.Resources.dreadnaut); process.Start(); } catch (Exception ex) { MessageBox.Show("dreadnaut konnte nicht gestartet werden", "dreadnaut-Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } process.StandardInput.WriteLine(dreadnautcmd); process.WaitForExit(); System.IO.File.Delete(path); string ergebnis = process.StandardOutput.ReadToEnd(); //cluster auslesen cluster = Helper.dreadnaut2cluster(ergebnis); rmsChart.Series.Clear(); gseries = new Series[nodeCount]; clusterBox.Text = ""; for (int nodenum = 0; nodenum < nodeCount; nodenum++) { gseries[nodenum] = new Series(); gseries[nodenum].IsVisibleInLegend = false; gseries[nodenum].ChartArea = "ChartArea1"; rmsChart.Series.Add(gseries[nodenum]); } for (int i = 0; i < cluster.Length; i++) { clusterBox.Text += "Cluster " + i + " mit Knoten: "; foreach (int k in cluster[i]) { clusterBox.Text += +k + " "; vertices[k].Cluster = i; } clusterBox.Text += "\r\n"; System.Windows.Media.Color coltmp = Vertex.cluster_colors[i % Vertex.cluster_colors.Length]; foreach (int nodenum in cluster[i]) { if (nodenum == cluster[i][0]) { gseries[nodenum].LegendText = "Cluster " + i.ToString(); gseries[nodenum].IsVisibleInLegend = true; } gseries[nodenum].BorderWidth = 2; gseries[nodenum].ChartType = SeriesChartType.FastLine; gseries[nodenum].Color = Color.FromArgb(255, coltmp.R, coltmp.G, coltmp.B); } } //Tmat erstellen if (networkDropdown.SelectedIndex == 3) { //nutze relativkoordinaten wenn custom ausgewählt TMat = Helper.TMat(cluster); } else {//nutze eingespeicherte TMat = Helper.TMat(networkDropdown.SelectedIndex); } //Parameter für die anordnung. einfach irgendwelche genommen. nochmal drüber nachdenken/nachlesen GraphControl = new GraphSharpControl(); GraphControl.layout.LayoutMode = LayoutMode.Simple; GraphControl.layout.LayoutAlgorithmType = "CompoundFDP"; GraphSharp.Algorithms.Layout.Compound.FDP.CompoundFDPLayoutParameters layoutParam = new GraphSharp.Algorithms.Layout.Compound.FDP.CompoundFDPLayoutParameters(); layoutParam.ElasticConstant *= 1.5; GraphControl.layout.LayoutParameters = layoutParam; GraphControl.layout.OverlapRemovalConstraint = AlgorithmConstraints.Must; GraphControl.layout.OverlapRemovalAlgorithmType = "FSA"; GraphSharp.Algorithms.OverlapRemoval.OverlapRemovalParameters overlapParam = new GraphSharp.Algorithms.OverlapRemoval.OverlapRemovalParameters(); overlapParam.HorizontalGap = 25; overlapParam.VerticalGap = 25; GraphControl.layout.OverlapRemovalParameters = overlapParam; GraphControl.layout.HighlightAlgorithmType = "Simple"; GraphControl.layout.Graph = graph; elementHost1.Child = GraphControl; graph_loaded = true; iterator_init(); runButton.Enabled = true; iterateButton.Enabled = true; layoutButton.Enabled = true; betaRunButton.Enabled = true; }