C#根据数据生成力引导图
2021/6/8 12:31:46
本文主要是介绍C#根据数据生成力引导图,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
效果:
public class Rootobject { public Node[] nodes { get; set; } public Link[] links { get; set; } public Category[] categories { get; set; } } /// <summary> /// 节点 /// </summary> public class Node { public string id { get; set; } public string name { get; set; } public float symbolSize { get; set; } public float value { get; set; } public int category { get; set; } } /// <summary> /// 关联 /// </summary> public class Link { public string source { get; set; } public string target { get; set; } } /// <summary> /// 分类 /// </summary> public class Category { public string name { get; set; } }
生成Json数据代码
List<Category> categories = new List<Category>(); List<Node> nodes = new List<Node>(); List<Link> links = new List<Link>(); Rootobject rootobject = new Rootobject(); private void toolStripButton1_Click(object sender, EventArgs e) { categories.Clear(); nodes.Clear(); links.Clear(); string[] lines = textBox1.Lines; #region 分类 for (int i = 0; i < lines.Length; i++) { string[] valueNum = lines[i].Split('#'); string cateGory = valueNum.LastOrDefault(); if (string.IsNullOrEmpty(cateGory)) { continue; } if (!categories.Any(o => o.name == cateGory)) { categories.Add(new Category() { name = cateGory }); } } #endregion #region 节点 for (int i = 0; i < lines.Length; i++) { string[] valueNum = lines[i].Split('#'); var newGroup = valueNum.Reverse().ToArray(); for (int x = 1; x < newGroup.Length; x++) { string cateGory = newGroup[0]; var categoryid = categories.FirstOrDefault(o => o.name == cateGory); int maxNode = 0; if (nodes.Any()) { maxNode=nodes.Max(p => Convert.ToInt32(p.id)); maxNode++; } Node node = nodes.FirstOrDefault(o => o.name == newGroup[x]); if (node == null) { node = new Node() { id = maxNode.ToString(), name = newGroup[x], category = categories.IndexOf(categoryid), symbolSize = 20, value = 2 }; nodes.Add(node); } if (x == 1) { continue; } else { var parentNode = nodes.FirstOrDefault(o => o.name == newGroup[x - 1]); links.Add(new Link() { source = node.id, target = parentNode.id }); } } } #endregion rootobject.categories = categories.ToArray(); rootobject.nodes = nodes.ToArray(); rootobject.links = links.ToArray(); string json = JsonConvert.SerializeObject(rootobject); textBox2.Text = ""; textBox2.AppendText(json); }
这篇关于C#根据数据生成力引导图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具