来自多个表的图表

时间:2019-02-13 12:16:34

标签: c# winforms charts

enter image description here 我有三个表,分别是存款,借方和转账, 像

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

如何在一张图表中显示所有三个表?我什至想知道将这三个表放到一个表中是否更好,例如

Transaction {TransactionId, TransactionTypeId, TransactionDate, Amount} 

其中TransactiontypeId可以是1 =存款,2代表借方,3代表转账,并将该交易表绑定到图表。 假设我将所有这些都放在一个表中,并使用表名Transactions,那么@ mm8可以帮助我解决这个问题:

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

最好有一个表或多个表的图表,以及如何做多个表。

我知道我必须为每个表创建不同的序列,如下所示:

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 个答案:

答案 0 :(得分:1)

您可以只从每个表中选择数据,然后使用DataBind方法用数据填充该系列,例如:

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

...