【问题标题】:ASP.NET Chart Control and Databinding to multiple SeriesASP.NET 图表控件和数据绑定到多个系列
【发布时间】:2012-02-08 19:32:02
【问题描述】:

我正在使用 ASP.NET 图表控件,并且正在使用以下代码将数据绑定到图表控件。图表上有三个系列:Series1、Series2 和 Series3。我在下面的代码中遇到的问题是它似乎只绑定到第一个系列,因此当显示图表时,只有第一个系列 (Series1) 显示,其他两个不显示。如果我逐步完成他的代码,一切似乎都很好。

有人对此问题的解决方案有任何建议吗?

private void ChartData1()
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["someConnectionString"].ConnectionString;

            SqlCommand cmd = null;
            cmd = new SqlCommand("dbo.ChartData", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Codevalue", SqlDbType.VarChar, 12);
            cmd.Parameters.Add("@filter", SqlDbType.VarChar, 50);

            cmd.Parameters["@Codevalue"].Value = "JAM";
            cmd.Parameters["@filter"].Value = "three_letter_code";

            conn.Open();
            SqlDataReader chartReader = cmd.ExecuteReader();

            //Bind the data using the DataBindTable method
            this.Chart1.Series["Series1"].Points.DataBindXY(chartReader, "bucketdate", chartReader, "RecordCount");
            this.Chart1.Series["Series2"].Points.DataBindXY(chartReader, "bucketdate", chartReader, "AverageTurns");
            this.Chart1.Series["Series3"].Points.DataBindXY(chartReader, "bucketdate", chartReader, "MovingAverageTurns");

            chartReader.Close();
            conn.Close();
        }

    }

【问题讨论】:

    标签: c# asp.net charts


    【解决方案1】:

    阅读器只是向前的,你的代码需要遍历阅读器三遍。

    有许多选项,但如果您想使用 DataBindXY,则需要将读取器中的数据提取到一个集合中(例如使用 LINQ)并绑定到该集合。

    另一种方法是在每个系列上设置XValueMemberYValueMembers 属性,然后调用DataBind,然后通过阅读器在一次迭代中绑定所有三个系列。

    【讨论】:

    • 谢谢乔,我会试试的。此外,我确实让这个工作。我通过阅读器迭代了三遍。我当然需要重新调整它以使其更加优雅/高效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2011-08-11
    相关资源
    最近更新 更多