How to display data in a chart from database?

Viewed 187

enter image description here

enter image description here

I'm trying to display the amount of sold shoes in a chart.

I have four tables such as tblCategory, tblProduct, tblTransaction and tblTransactionItem.

I have three categories (MenShose, WomanShose, and KiddsShose as you see in the picture).

On the X-axis I want to display the category name and shose size like for example Wemanshose-35 there "WomenShe is categoryName and 35 is the size of the shose."

I tried this but it is giving me the wrong result.

public DisplaySales()
{
    InitializeComponent(); 
    chart1.Series.Clear();

    var series = chart1.Series.Add("Series1");
    series.XValueMember = "ProduktId";
    series.YValueMembers = "TolatlSold";
    series.Name = "Shose";
    chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
    chart1.Series["Shose"].IsValueShownAsLabel = true;
    series.CustomProperties = "LabelStyle=Left";

    ShowTodaysSoldProduct();


}

// And here is what I'm trying but it shows me the wrong result. Please Help!


private void ShowTodaysSoldProduct()
{
    using (Db db = new Db())
    {
        DateTime today = DateTime.Today;
        DateTime tomorrow = today.AddDays(1);
        var result = (from tr in db.TransactionItems
                      join t in db.Transactions on tr.TransactionId equals t.TransactionId
                      //join p in db.Products on tr.ProductId equals p.ProductId
                      join c in db.Categories on tr.CategoryId equals c.CategoryId
                      where t.TransactionDate >= today && t.TransactionDate < tomorrow
                      group tr by tr.categories.CategoryName  into g
                      select new ProductSaled
                      {
                          ProduktId =  g.Key, // Before g.Key I want to display CategoryName too 
                          TolatlSold = g.Count()

                      }).ToList();
        chart1.DataSource = result;

        chart1.DataBind();
        chart1.Show();
    }
}
0 Answers
Related