I have a question realting to binding data from a database in MSChart. I have a database that contains "Value", "Time", "ID" and "UniqueID". I want to bind the X axis to "Value" and the Y axis to "Time". I use the "ID" to identify all the data that needs to be bound to the chart control. So I query the database for "ID" 1 and retrieve say the first 10 results and bind it to the chart control. The data is bound to Series "Series1". Now I add a new Series "Series2" and I Query the database to "ID" 2 and bind the data to the chart. For somereason when I bind the second query the chart only displays the second set of data.
Could you please direct me to what I am doing wrong.
public void connect()
{
SqlConnection cn;
SqlCommand myCommand;
cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
myCommand = new SqlCommand();
myCommand.Connection = cn;
myCommand.CommandText = "SELECT TOP 3 [Value],[Time] FROM [dbo].[Table] WHERE [ID] = 1";
cn.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
Chart1.Series["Series1"].XValueMember = "Value";
Chart1.Series["Series1"].YValueMembers = "Time";
Chart1.DataSource = myReader;
Chart1.DataBind();
cn.Close();
Chart1.Series["Series1"].Color = System.Drawing.Color.FromArgb(0, 51, 25);
}
public void connect2()
{
SqlConnection cn;
SqlCommand myCommand;
cn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
myCommand = new SqlCommand();
myCommand.Connection = cn;
myCommand.CommandText = "SELECT TOP 3 [Value],[Time] FROM [dbo].[Table] WHERE [ID] = 2";
cn.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
Chart1.Series["Series1"].XValueMember = "Value";
Chart1.Series["Series1"].YValueMembers = "Time";
Chart1.DataSource = myReader;
Chart1.DataBind();
cn.Close();
Chart1.Series["Series1"].Color = System.Drawing.Color.FromArgb(0, 51, 25);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Chart1_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
connect();
connect2();
}
You are replacing the same series Series1
with new data. What you should be doing instead is adding a new series to the chart using chart1.series.add
The link below should get you started:
http://msdn.microsoft.com/en-us/library/vstudio/dd456769%28v=vs.100%29.aspx
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.