来自多个表的图表 [英] Chart from multiple tables
问题描述
我有三个表,分别称为存款,借方和转账,像
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}
其中TransactiontypeId可以是1 =用于存款,2用于借记,3用于转移和绑定此交易表到图表。
比方说,我将所有表都放在一个表中,并以表名命名,然后@ mm8的交易帮助我弄清了这一点:
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";
推荐答案
您只需从每个表中选择数据然后使用 DataBind
方法为数据填充序列,例如:
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);
...
这篇关于来自多个表的图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!