2015-12-11 79 views
1

我有一个图表,其中月份标记在X轴上,y值是整数。vb.net按X轴排序图表日期

Private MonthProfit As SortedDictionary(Of String, Integer) = New SortedDictionary(Of String, Integer) 

Private Sub frmChart_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim Query As String = "SELECT Profit, SaleDate FROM tblpayment" 

     Using Conn As New MySqlConnection(MySQL.ConnectionDetails) 
      Using Comm As New MySqlCommand() 
       With Comm 
        .Connection = Conn 
        .CommandText = Query 
        .CommandType = CommandType.Text 
       End With 
       Try 
        Conn.Open() 
        Dim Reader As MySqlDataReader = Comm.ExecuteReader 
        While Reader.Read OrElse (Reader.NextResult And Reader.Read) 
         If MonthProfit.ContainsKey(MonthName(Reader.GetDateTime(1).Month)) Then 
          MonthProfit(MonthName(Reader.GetDateTime(1).Month)) += Reader.GetInt32(0) 
         Else 
          MonthProfit.Add(MonthName(Reader.GetDateTime(1).Month), 0) 
          MonthProfit(MonthName(Reader.GetDateTime(1).Month)) += Reader.GetInt32(0) 
         End If 
        End While 
       Catch ex As MySqlException 

       End Try 
      End Using 
     End Using 

     For Each index In MonthProfit.ToArray 
      'chMain.Series.Add(index.Key) 
      chMain.Series("Profit").Points.AddXY(index.Key, index.Value) 
     Next 
     chMain.Series("Profit").Sort(pointSortOrder:=DataVisualization.Charting.PointSortOrder.Ascending, sortBy:="Axislabel") 

    End Sub 

我似乎无法弄清楚如何做到这一点,所以我希望得到一些帮助,指出我朝着正确的方向发展。


固定解:

Private Sub frmChart_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim Query As String = "SELECT MONTHNAME(SaleDate) AS MonthName, Profit FROM tblpayment ORDER BY SaleDate ASC" 

    Using Conn As New MySqlConnection(MySQL.ConnectionDetails) 
     Using Comm As New MySqlCommand() 
      With Comm 
       .Connection = Conn 
       .CommandText = Query 
       .CommandType = CommandType.Text 
      End With 
      Try 
       Conn.Open() 
       Dim Reader As MySqlDataReader = Comm.ExecuteReader 
       While Reader.Read OrElse (Reader.NextResult And Reader.Read) 
        If MonthProfit.ContainsKey(Reader.GetString(0)) Then 
         MonthProfit(Reader.GetString(0)) += Reader.GetInt32(1) 
        Else 
         MonthProfit.Add(Reader.GetString(0), 0) 
         MonthProfit(Reader.GetString(0)) += Reader.GetInt32(1) 
        End If 
       End While 
      Catch ex As Exception 
       MsgBox(ex.GetBaseException.ToString) 
      End Try 
     End Using 
    End Using 

    For Each index In MonthProfit.ToArray 
     chMain.Series(0).Points.AddXY(index.Key, index.Value) 
    Next 

    chMain.Series(0).XValueMember = "Month Name" 
    chMain.Series(0).YValueMembers = "Profit" 
    chMain.ChartAreas(0).AxisX.Interval = 1 

End Sub 
+0

您可以简单地将排序添加到您的查询。 –

+0

@RezaAghaei你能帮我多一点吗? – Semonoir

回答

0

您可以简单地添加排序的查询,也可以使用DataTableTableAdapter和使用这种方式的数据绑定到图表:

Dim ConnectionString As String = "your connection string" 
Dim CommandText As String = "SELECT Profit, MONTHNAME(SaleDate) AS MonthName " & _ 
          "FROM tblpayment ORDER BY SaleDate ASC" 
Dim Adapter As New MySqlDataAdapter(CommandText, ConnectionString) 
Dim Table As New DataTable() 
Adapter.Fill(Table) 

Me.Chart1.Series(0).XValueMember = "MonthName" 
Me.Chart1.Series(0).YValueMembers = "Profit" 
Me.Chart1.ChartAreas(0).AxisX.Interval = 1 
Me.Chart1.DataSource = Table 
Me.Chart1.DataBind() 
+0

好的谢谢你的帮助。但是,它并不按我的意愿工作。如果该月份已经以x值存在,我希望它将利润中的值添加到该月份。那就是我在代码中发现的问题。然而,我采取了你的查询,并在我这样的代码尝试它,似乎没有任何区别:http://pastebin.com/hu8894vi对不起,我不得不做一个pastebin链接,因为你不能在评论中添加代码。 – Semonoir

+0

@Semonoir **如果月份已经以x值的形式存在,我希望它在当月增加利润值。**这不是你的代码执行情况,也不是你在主要问题中说的:)但是你可以使用对此进行分组和总结。 –

+0

我的代码确实说:MonthProfit(MonthName(Reader.GetDateTime(1).Month))+ = Reader.GetInt32(0)。我用+ =来表示它,但我应该更清楚。无论如何感谢您的帮助,我会尽力继续。 – Semonoir