简体   繁体   English

在Mschart中绑定数据

[英]Binding data in mschart

I have a question realting to binding data from a database in MSChart. 我有一个问题要绑定到MSChart中的数据库数据。 I have a database that contains "Value", "Time", "ID" and "UniqueID". 我有一个包含“值”,“时间”,“ ID”和“ UniqueID”的数据库。 I want to bind the X axis to "Value" and the Y axis to "Time". 我想将X轴绑定到“值”,将Y轴绑定到“时间”。 I use the "ID" to identify all the data that needs to be bound to the chart control. 我使用“ ID”来标识需要绑定到图表控件的所有数据。 So I query the database for "ID" 1 and retrieve say the first 10 results and bind it to the chart control. 因此,我在数据库中查询“ ID” 1,并说出前10个结果并将其绑定到图表控件。 The data is bound to Series "Series1". 数据绑定到系列“ Series1”。 Now I add a new Series "Series2" and I Query the database to "ID" 2 and bind the data to the chart. 现在,我添加了一个新的Series“ Series2”,并将数据库查询为“ ID” 2,并将数据绑定到图表。 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. 您将用新数据替换同一系列Series1 What you should be doing instead is adding a new series to the chart using chart1.series.add 您应该做的是使用chart1.series.add将新系列添加到图表中

The link below should get you started: 以下链接将帮助您入门:

http://msdn.microsoft.com/en-us/library/vstudio/dd456769%28v=vs.100%29.aspx http://msdn.microsoft.com/zh-CN/library/vstudio/dd456769%28v=vs.100%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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