C#Charts从datatable添加多个系列

时间:2012-11-12 19:18:15

标签: c# charts

我从我的数据库中检索了几个数据表,这些数据表的大小各不相同。这只是一个例子。

在这里查看结构!
enter image description here
我设法创建了2个不同的系列,让它们出现在传奇故事中。

我的问题是如何将数据绑定到相应的系列。 系列名称是从列doman_namn创建的,系列的数量是从“antal”列创建的,该列保存唯一URL的数量。

问题 如何将ADDY和ADDX绑定到图表上,它现在失败了。

到目前为止,这是我的代码......

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;

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

3 个答案:

答案 0 :(得分:6)

看看我的一个示例如何使用代码将DataTable绑定到MS Chart:

How to draw Chart based on DataTable from console application?

希望你会发现它很有用。

以下是要点:

//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";

以下是绑定多个系列的另一个链接:

http://dotnetslackers.com/articles/net/Binding-a-Microsoft-Chart-with-a-Dataset.aspx

答案 1 :(得分:2)

我自己设法做到了,但你Gregor Primar把我推向了正确的方向!

重要的是您为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;

答案 2 :(得分:1)

这可能有效:

                if (dr.Rows.Count > 0)
                {
                    string[] seriesSessionArray = dr.AsEnumerable().Select(row => row.Field<string>("Username")).ToArray();
                    decimal[] pointsSessionArray = dr.AsEnumerable().Select(row => row.Field<decimal>("Percent")).ToArray();
                    chartGrowth.SuppressExceptions = true;
                    for (int i = 0; i < seriesSessionArray.Length; i++)
                    {
                        if (pointsSessionArray[i] == 0)
                        {
                            chartGrowth.ChartAreas[0].RecalculateAxesScale();
                        }
                        if (chartGrowth.Series.IndexOf(seriesSessionArray[i]) == -1)  //series does not exists
                        {
                            chartGrowth.Series.Add(seriesSessionArray[i]);
                        }
                        chartGrowth.Series[seriesSessionArray[i]].ChartType = SeriesChartType.SplineArea;
                        chartGrowth.Series[seriesSessionArray[i]].Points.AddY(pointsSessionArray[i]);
                        chartGrowth.Series[seriesSessionArray[i]].ToolTip = seriesSessionArray[i] + ":" + "#VAL{0.0}";
                        chartGrowth.Series[seriesSessionArray[i]].SetCustomProperty("LineTension", "0.45");
                    }

                }