zoukankan      html  css  js  c++  java
  • 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);
            }

  • 相关阅读:
    [LeetCode]603. 连续空余座位(Mysql、自连接)
    [LeetCode]671. 二叉树中第二小的节点(递归)
    [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)
    [LeetCode]26. 删除排序数组中的重复项(数组,双指针)
    C# 把引用的dll嵌入到exe文件中
    iptables规则表
    [转载]EF Code First 学习笔记:约定配置
    使用itunes同步ios时丢失照片恢复
    USB硬件远程共享解决iphone已停用
    C# 非独占延时函数 非Sleep
  • 原文地址:https://www.cnblogs.com/w2011/p/14862112.html
Copyright © 2011-2022 走看看