简体   繁体   中英

ASP.NET Chart Control and Databinding to multiple Series

I am working with the ASP.NET chart control and I am using the following code to databind data to the chart control. The chart has three series on it: Series1, Series2 and Series3. The problem that I am having with the code below is that it only seems to bind to the first series so that when the chart is displayed, only the first series (Series1) shows, the other two do not display. If I step through he code everything seems to be fine.

Does anyone have any suggestions as to a solution to this issue?

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

    }

A reader is forward only, your code as it stands would need to iterate through the reader three times.

There are a number of options, but if you want to use DataBindXY, you will need to extract the data from the reader into a collection (eg using LINQ) and bind to that collection.

An alternative would be to set the XValueMember and YValueMembers properties on each series, then call DataBind which would then bind all three series in one iteration through the reader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM