C#根据数据生成力引导图

2021-06-09 02:01

阅读:657

标签:public   pen   loading   first   rem   span   val   nbsp   关联   

效果:技术图片

    public class Rootobject
    {
        public Node[] nodes { get; set; }
        public Link[] links { get; set; }
        public Category[] categories { get; set; }
    }

    /// 
    /// 节点
    /// 
    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; }
    }

    /// 
    /// 关联
    /// 
    public class Link
    {
        public string source { get; set; }
        public string target { get; set; }
    }

    /// 
    /// 分类
    /// 
    public class Category
    {
        public string name { get; set; }
    }

生成Json数据代码

        List categories = new List();
        List nodes = new List();
        List links = new List
();
        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 )
            {
                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 )
            {
                string[] valueNum = lines[i].Split(#);
                var newGroup = valueNum.Reverse().ToArray();
                for (int x = 1; 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#根据数据生成力引导图

标签:public   pen   loading   first   rem   span   val   nbsp   关联   

原文地址:https://www.cnblogs.com/w2011/p/14862112.html


评论


亲,登录后才可以留言!