在mschart中绑定数据

时间:2014-12-09 03:58:30

标签: c# asp.net sql-server mschart

我有一个问题,试图从MSChart中的数据库绑定数据。我有一个数据库,其中包含"价值","时间"," ID"和" UniqueID"。我想将X轴绑定到"值"和Y轴到"时间"。我使用" ID"识别需要绑定到图表控件的所有数据。所以我在数据库中查询" ID" 1并检索说出前10个结果并将其绑定到图表控件。数据绑定到Series" Series1"。现在我添加一个新系列" Series2"我查询数据库到" ID" 2并将数据绑定到图表。对于某些原因,当我绑定第二个查询时,图表只显示第二组数据。

请你指点我做错了什么。

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

1 个答案:

答案 0 :(得分:0)

您正在用新数据替换相同的系列Series1。 您应该做的是使用chart1.series.add

向图表添加新系列

以下链接可以帮助您入门:

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