Chart from multiple tables

不想你离开。 提交于 2019-12-25 01:37:28

问题


I have three tables named Deposit, Debit and Transfer, like

Deposit  { DepositID, DepostDate, Amount}
Debit    { DebitID, DebitDate, Amount}
Transfer { TransferID, TransferDate, Amount}

How can I show all three tables in one chart? I am even wondering if it is better to put those three tables into one table instead, like

Transaction {TransactionId, TransactionTypeId, TransactionDate, Amount} 

where TransactiontypeId could be 1 = for Deposit, 2 for Debit and 3 for Transfer and bind this transaction table to the chart. Let's say I have all those in one table instead and with table name Transactions then @mm8 helped me figure this out:

 var result = (from pr in db.Transactions 
                  join tr in db.TransactionType on pr.TrTypeId equals tr.TransactionTypeId
                  select new
                  {
                      TransactionDate = pr.TransactionDate,
                      TransactionType = tr.TrType,
                      Amount = pr.Amount

                  }).ToList();

chart1.DataSource = result
 .GroupBy(x => x.TransactionDate.Value.Year)
    .Select(g => new
    {
        Year = g.Key,
        TransactionType = g. //////
        Amount = g.Sum(y => y.Amount)
    })
    .ToArray();

Is is better to have a chart from one table or from multiple tables and how to do multiple.

I am aware that I have to create different series for every table like this:

var Depseries = chart1.Series.Add("Deposit");
Depseries.XValueMember = "Year";
Depseries.YValueMembers = "DepositAmount";
Depseries.Name = "Deposit";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.Series["Deposit"].IsValueShownAsLabel = true;
Depseries.CustomProperties = "LabelStyle=Left";

// Debit

var Debseries = chart1.Series.Add("Debit");
Debseries.XValueMember = "Year";
Debseries.YValueMembers = "DebitAmount";
Debseries.Name = "Debit";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.Series["Debit"].IsValueShownAsLabel = true;
Debseries.CustomProperties = "LabelStyle=Left";

// Transfer
var FDseries = chart1.Series.Add("Transfer");
FDseries.XValueMember = "Year";
FDseries.YValueMembers = "TransferAmount";
FDseries.Name = "Transfer";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.Series["Transfer"].IsValueShownAsLabel = true;
FDseries.CustomProperties = "LabelStyle=Left";  

回答1:


You could just select the data from each of the tables and then use the DataBind method to populate the series with data, e.g.:

var deposits = (from x in db.Deposits select new { x.DepositDate, x.Amount })
    .ToArray()
    .GroupBy(x => x.DepositDate.Year)
    .Select(g => new { Year = g.Key, Amount = g.Sum(y => y.Amount) })
    .ToArray();
var Depseries = chart1.Series.Add("Deposit");
Depseries.XValueMember = "Year";
Depseries.YValueMembers = "DepositAmount";
Depseries.Name = "Deposit";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.Series["Deposit"].IsValueShownAsLabel = true;
Depseries.CustomProperties = "LabelStyle=Left";
chart1.Series["Deposit"].Points.DataBind(deposits, "Year", "Amount", null);

var debits = (from x in db.Debits select new { x.DebitDate, x.Amount })
    .ToArray()
    .GroupBy(x => x.DebitDate.Year)
    .Select(g => new { Year = g.Key, Amount = g.Sum(y => y.Amount) })
    .ToArray();
var Debseries = chart1.Series.Add("Debit");
Debseries.XValueMember = "Year";
Debseries.YValueMembers = "DebitAmount";
Debseries.Name = "Debit";
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
chart1.Series["Debit"].IsValueShownAsLabel = true;
Debseries.CustomProperties = "LabelStyle=Left";
chart1.Series["Debit"].Points.DataBind(debits, "Year", "Amount", null);

...


来源:https://stackoverflow.com/questions/54670007/chart-from-multiple-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!