C# 图表从数据表中添加多个系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13350036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Charts add multiple series from datatable
提问by 8bitcat
I retrieve several datatables from my DB, which vary in size. This one of 2 is just an example.
我从我的数据库中检索了几个大小不一的数据表。这两个中的一个只是一个例子。
See the structure here! 
I managed to create the 2 different series and have them show up on the legend.
看这里的结构!
我设法创建了 2 个不同的系列,并将它们显示在图例中。
My question is on how to bind that data to the respective series. The series name are created from column doman_namn and the amount of series are created from the "antal" column which holds the number of unique URLS.
我的问题是如何将该数据绑定到相应的系列。系列名称是从列 doman_namn 创建的,系列数量是从包含唯一 URL 数量的“antal”列创建的。
QUESTIONHOW TO BIND ADDY and ADDX to the chart it fails now.
问题如何将 ADDY 和 ADDX 绑定到它现在失败的图表。
This is my code so far...
到目前为止,这是我的代码...
Chart1.DataSource = dt;
int amountofrows = Convert.ToInt32(dt.Rows[0]["antal"].ToString());
for (int i = 0; i < amountofrows; i++)
{
string serieName = dt.Rows[i]["doman_namn"].ToString();
Chart1.Series.Add(serieName);
Chart1.Series[i].ChartType = SeriesChartType.Line;
foreach(DataRow dr in dt.Rows)
{
try
{
if (String.Equals(serieName,dr["doman_namn"].ToString(), StringComparison.Ordinal))
{
Chart1.Series[serieName].Points.AddY(Convert.ToDouble(dr["ranking_position"]));
Chart1.Series[serieName].Points.AddY(Convert.ToDouble(dr["ranking_date"]));
}
}
catch (Exception)
{
throw new InvalidOperationException("Failed when adding points");
}
}
}
Chart1.DataBind();
Chart1.Visible = true;
CODE AFTER HELP FROM GREGOR
来自 Gregor 的帮助后的代码
for (int i = 0; i < amountofrows; i++)
{
string serieName = dt.Rows[i]["doman_namn"].ToString();
Chart1.Series.Add(serieName);
Chart1.Series[i].ChartType = SeriesChartType.Line;
Chart1.Series[serieName].XValueMember = "ranking_date";
Chart1.Series[serieName].YValueMembers = "ranking_position";
}
Chart1.DataBind();
采纳答案by 8bitcat
I managed to do it myself, but you Gregor Primar pushed me in the right direction!
我自己设法做到了,但是您 Gregor Primar 将我推向了正确的方向!
What was important was that you set the valuetype for the X and Y-axis. As decimal type was not an option I used auto as type.
重要的是您设置了 X 轴和 Y 轴的值类型。由于十进制类型不是一个选项,我使用 auto 作为类型。
Chart1.DataSource = dt;
int amountofrows = Convert.ToInt32(dt.Rows[0]["antal"].ToString());
for (int i = 0; i < amountofrows; i++)
{
List<string> xvals = new List<string>();
List<decimal> yvals = new List<decimal>();
string serieName = dt.Rows[i]["doman_namn"].ToString();
Chart1.Series.Add(serieName);
Chart1.Series[i].ChartType = SeriesChartType.Line;
foreach(DataRow dr in dt.Rows)
{
try
{
if (String.Equals(serieName,dr["doman_namn"].ToString(), StringComparison.Ordinal))
{
xvals.Add(dr["ranking_date"].ToString());
yvals.Add(Convert.ToDecimal(dr["ranking_position"].ToString()));
}
}
catch (Exception)
{
throw new InvalidOperationException("Diagrammet kunde inte ritas upp");
}
}
try
{
Chart1.Series[serieName].XValueType = ChartValueType.String;
Chart1.Series[serieName].YValueType = ChartValueType.Auto;
Chart1.Series[serieName].Points.DataBindXY(xvals.ToArray(), yvals.ToArray());
}
catch (Exception)
{
throw new InvalidOperationException("Kunde inte bind punkterna till Diagrammet");
}
}
Chart1.DataBind();
Chart1.Visible = true;
回答by Gregor Primar
Take a look at one of my samples how to bind DataTable to MS Chart using code:
看看我的一个示例,如何使用代码将 DataTable 绑定到 MS Chart:
How to draw Chart based on DataTable from console application?
Hope you will find it usefull.
希望你会发现它很有用。
Here are the key points:
以下是关键点:
//setting the source from datatable....
chart.DataSource = dt;
//setting XValueMember for first serie (Name is column inside datasource)...
serie1.XValueMember = "ranking_position";
//setting YValueMembers...
serie1.YValueMembers = "ranking_date";
Here is another link for binding multiple series:
这是绑定多个系列的另一个链接:
http://dotnetslackers.com/articles/net/Binding-a-Microsoft-Chart-with-a-Dataset.aspx
http://dotnetslackers.com/articles/net/Binding-a-Microsoft-Chart-with-a-Dataset.aspx

![C# 从表列中读取二进制文件到 byte[] 数组中](/res/img/loading.gif)