Asp.net Chart控制多个系列绑定

时间:2014-06-10 11:48:11

标签: asp.net microsoft-chart-controls

我有以下SQL Server返回的数据集:

Group Name    Month       Total ORDER
Group-India    Apr          80
Group-US       Apr          70
Group-Europe   Apr          60
Group-India    May          82
Group-US       May          85
Group-Europe   May          89

ASP.Net图表 - 我需要通过MONTH显示此CHART(表示Apr将是一个系列,MAY将是单独的系列),Y轴应该是GROUPNAME(并显示它的计数)。

请帮忙 此致

2 个答案:

答案 0 :(得分:1)

使用DataBindCrossTable

Sample上课

public class Sample
    {
        public string groupName { get; set; }
        public string month { get; set; }
        public int totalOrder { get; set; }
    }

的.aspx

<asp:Chart ID="Chart1" runat="server" Width="600px">
                        <Legends>
                            <asp:Legend TitleFont="Microsoft Sans Serif, 8pt, style=Bold" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold" IsTextAutoFit="False" Enabled="True" Name="Default"></asp:Legend>
                        </Legends>
                        <Series>
                        </Series>
                        <ChartAreas>
                            <asp:ChartArea Name="ChartArea1">
                            </asp:ChartArea>
                        </ChartAreas>
                    </asp:Chart>

代码(.aspx.cs)

var sampleData = 
     new List<Sample> {
     new Sample { month = "apr", groupName = "Group-India",  totalOrder = 50 }, 
     new Sample { month = "apr", groupName = "Group-US",  totalOrder = 500 }, 
     new Sample { month = "apr", groupName = "Group-Europe",  totalOrder = 151 },
     new Sample {  month = "May", groupName = "Group-India", totalOrder = 15 },
     new Sample { month = "May", groupName = "Group-US", totalOrder = 150 },
     new Sample { month = "May", groupName = "Group-Europe",  totalOrder = 1500}
     };

// data bind
Chart1.DataBindCrossTable(sampleData, "groupName", "month", "totalOrder", "Label=totalOrder");

这是输出 enter image description here

答案 1 :(得分:0)

如果你不想写一个类进行映射,那么你可以直接将数据表转换为 Enumerable() 如下。

List<DataRow> list = dt.AsEnumerable().ToList();

像这样

  using (con = new SqlConnection(cs))
                {
                  
                    com = new SqlCommand("ERPM_Learnar_Info_By_Gender", con);
                    com.CommandType = CommandType.StoredProcedure;
                
                    adapt = new SqlDataAdapter(com);
                    dt = new DataTable();
                    adapt.Fill(dt);
                    List<DataRow> list = dt.AsEnumerable().ToList();
                    Chart1.DataBindCrossTable(list, "Gender_Name", "District_Name", "Total", "Label=Total");

结果是 enter image description here

存储过程结果是这样

enter image description here