Asp.net Chart control multiple series binding

user3657339 picture user3657339 · Jun 10, 2014 · Viewed 7.4k times · Source

I have got following dataset which is returned by 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 Charts - I need to display this CHART by MONTH (means Apr will be one series and MAY will be separate series) and Y-axis should be GROUPNAME (and show count for it).

Please help Regards

Answer

SoftSan picture SoftSan · Jun 11, 2014

use DataBindCrossTable

Sample class

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>

code behind (.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");

and here's the output enter image description here