0

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

user3657339
  • 607
  • 2
  • 9
  • 20

2 Answers2

2

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

SoftSan
  • 2,482
  • 3
  • 23
  • 54
  • Can you please assist with this: https://stackoverflow.com/questions/46204933/how-to-create-multiple-pie-chart-in-asp-net-using-c-sharp – Si8 Sep 13 '17 at 18:54
0

If you don't want to write a class for mapping, then you can directly convert a data table to Enumerable() as follow.

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

Something like this

  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");

and the result is enter image description here

stored procedure result was this

enter image description here

Nisar Ahmad
  • 113
  • 1
  • 1
  • 11