.net mvc+echart 实现Sankey图

3个Datatable数据格式(datatable 2和3的数据其实都是从datatable1中分离出来的,我是直接从数据库把数据准备好,你也可以在后端转换,或者在前端处理datatable1,这样就不用三个datatable了)和效果图,图中序号跟数据列对应:
在这里插入图片描述
Datatable 2
在这里插入图片描述
Datatable 3
在这里插入图片描述

在这里插入图片描述
后端代码:

public string GetEchart(string StartYear, string EndYear)
        {

            Dictionary<string, object> dic = _service.GetUserInfoDic();
            //  dic.Add("StartYear", StartYear);
            // dic.Add("EndYear", EndYear);
            dic.Add("Action", "View");
            DataSet ds = _service.Query("PWD_IECapacityPlanningSankeychart", XmlParams.CreateSqlXml(dic));
            DataTable dt = ds.Tables[2];
            DataTable dt1 = ds.Tables[1];
            List<string> NameList = new List<string>();
            List<string> SourceList = new List<string>();
            List<string> TargetList = new List<string>();
            List<string> ValueList = new List<string>();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dataRow = dt.Rows[i];
                    NameList.Add(dataRow["name"].ToString());
                   

                }
                for(int j = 0; j < dt1.Rows.Count; j++)
                {
                    DataRow dataRow = dt1.Rows[j];
                    SourceList.Add(dataRow["source"].ToString());
                    TargetList.Add(dataRow["target"].ToString());
                    ValueList.Add(dataRow["value"].ToString());
                }
                var obj = new
                {
                    Name = NameList,
                    Source = SourceList,
                    Target = TargetList,
                    Value = ValueList,



                };
                

            string jsonstr = JsonConvert.SerializeObject(obj);
                return jsonstr;
            }
            else
            {
                string jsonstr = "";
                return jsonstr;
            }
        }

前端JS:

function GetEchart1()
   {
       var StartYear = $("#StartYear").val();
       var EndYear = $("#EndYear").val();
       var linksData = [];
       var mydata = [];
       
         $.ajax({
            url:'@Url.Action("GetEchart")',
            type: "post",
            dataType: "json",
            async: true,
             data: { StartYear: StartYear, EndYear: EndYear},

            success: function (data) {
                 debugger;
                //console.log(data);
                for (var i = 0; i < data.Name.length; i++) {
                   // names.push(data.Name[i]);
                    mydata[i] = { name: data.Name[i] };
                    
                }
                for (var j = 0; j < data.Source.length; j++) {
                    
                    linksData[j] = { source: data.Source[j], target: data.Target[j], value: data.Value[j] };

                }
                // const arr = ['B11 Total', 'DLP'];
                const arr = data.Name;
                /*
                const obj = {
                    Data: arr.map(name => ({ name }))
                };
                 */

                var chartDom = document.getElementById('chart');

                var myChart = echarts.init(chartDom);

                var option;
                option = {
                    title: {
                        text: 'B11 PWD Plan HC',
                        top:'5',
                    },
                    color: ['#38a97e', '#94b305', '#d87438', '#977c78', '#c4ccd3'],
                    tooltip: {
                        trigger: 'item',
                        triggerOn: 'mousemove'
                    },
                    
                    series: {
                        type: 'sankey',
                        layout: 'none',
                        emphasis: {
                            focus: 'adjacency'
                        },
                        focusNodeAdjacency: true,
                        itemStyle: {
                            borderWidth: 1,
                           
                            borderColor: '#fff'
                        },
                        lineStyle: {
                            curveness: 0.5,
                            
                        },
                      
                    
                        data: mydata,
                        links: linksData  ,
                    }
                };

        option && myChart.setOption(option);
            }
        });
    };

页面DIV 用于显示sankey图

 <div id="chart" style="width:96%;margin-left:2%;margin-right:2%;height:95%;margin-top:1%;">

        </div>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值