我有一个图表,其中月份标记在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
您可以简单地将排序添加到您的查询。 –
@RezaAghaei你能帮我多一点吗? – Semonoir