分离图表系列

时间:2014-03-25 15:10:03

标签: vb.net visual-studio-2010 charts mschart

我目前有一个包含4个系列的图表元素。我的问题是,当他们单独工作时,如果我设置超过1个系列,它会将所有这些数据设置为最后一个DataSet(因此系列1,2和3具有与4相同的位置)。< / p>

有人可以看看我破损的代码并让我知道哪里出了问题?也许有一些关于整理它的指示......我以前从未使用过任何形式的图表。

Using con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Response.mdb;")


    Dim MyQuery As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Happy'));"
    Using cmd = New OleDbCommand(MyQuery, con)
        Dim MyData As New OleDbDataAdapter(MyQuery, con)
        Dim MyDataSet As New DataSet
        con.Open()
        MyData.Fill(MyDataSet, "Table")
        ChrtMoodChanges.DataSource = MyDataSet.Tables("Table")
        Dim Series1 As Series = ChrtMoodChanges.Series("Series1")
        Series1.Name = "Happy"
        ChrtMoodChanges.Series(Series1.Name).XValueMember = "Actual_Date"
        ChrtMoodChanges.Series(Series1.Name).YValueMembers = "CountOfMood"
    End Using


    Dim MyQuery2 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Neutral'));"
    Using cmd = New OleDbCommand(MyQuery2, con)
        Dim MyData2 As New OleDbDataAdapter(MyQuery2, con)
        Dim MyDataSet2 As New DataSet
        MyData2.Fill(MyDataSet2, "Table")
        ChrtMoodChanges.DataSource = MyDataSet2.Tables("Table")
        Dim Series2 As Series = ChrtMoodChanges.Series("Series2")
        Series2.Name = "Neutral"
        ChrtMoodChanges.Series(Series2.Name).XValueMember = "Actual_Date"
        ChrtMoodChanges.Series(Series2.Name).YValueMembers = "CountOfMood"
    End Using


    Dim MyQuery3 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Sad'));"
    Using cmd = New OleDbCommand(MyQuery3, con)
        Dim MyData3 As New OleDbDataAdapter(MyQuery3, con)
        Dim MyDataSet3 As New DataSet
        MyData3.Fill(MyDataSet3, "Table")
        ChrtMoodChanges.DataSource = MyDataSet3.Tables("Table")
        Dim Series3 As Series = ChrtMoodChanges.Series("Series3")
        Series3.Name = "Sad"
        ChrtMoodChanges.Series(Series3.Name).XValueMember = "Actual_Date"
        ChrtMoodChanges.Series(Series3.Name).YValueMembers = "CountOfMood"
    End Using


    Dim MyQuery4 As String = "SELECT qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood, Count(qry_Response_By_Date_1.Mood) AS CountOfMood FROM qry_Response_By_Date_1 GROUP BY qry_Response_By_Date_1.Actual_Date, qry_Response_By_Date_1.Mood HAVING (((qry_Response_By_Date_1.Mood)='Angry'));"
    Using cmd = New OleDbCommand(MyQuery4, con)
        Dim MyData4 As New OleDbDataAdapter(MyQuery4, con)
        Dim MyDataSet4 As New DataSet
        MyData4.Fill(MyDataSet4, "Table")
        ChrtMoodChanges.DataSource = MyDataSet4.Tables("Table")
        Dim Series4 As Series = ChrtMoodChanges.Series("Series4")
        Series4.Name = "Angry"
        ChrtMoodChanges.Series(Series4.Name).XValueMember = "Actual_Date"
        ChrtMoodChanges.Series(Series4.Name).YValueMembers = "CountOfMood"
    End Using
End Using

P.S。 SQL为每个人返回正确的数据...它只是让它在图表中存储正确的数据是我的问题。

1 个答案:

答案 0 :(得分:2)

每次设置ChrtMoodChanges.DataSource时,您都会覆盖图表的数据源,这就是为什么最终只得到最后一个数据集的原因。

我建议您在开始将数据表绑定到图表之前合并数据表,然后将每个系列引用到数据源/数据表中的列

ChrtMoodChanges.Series(Series1.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series1.Name).YValueMember = "CountOfMood_Col1"
....
....
ChrtMoodChanges.Series(Series4.Name).XValueMember = "Actual_Date"
ChrtMoodChanges.Series(Series4.Name).YValueMember = "CountOfMood_Col4"

因此,建议清理代码,我会执行以下操作

  1. 获取4个数据表
  2. 将数据表与“Actual_Date”合并为主键
  3. 将每列添加到图表中作为新系列
  4. 合并示例: Merge columns of different types from datatables into one larger datatable

    希望这有帮助