Hi elvisidrizi1,
Check this example. Now please take its reference and correct your code.
Namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Windows.Forms.DataVisualization.Charting
Code
Public Class Form1
Dim constr As String = "Data Source=####/SQLEXPRESS;Initial Catalog=Expenses;Trusted_Connection=yes;"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DataCharts()
End Sub
Private Sub DataCharts()
Dim dt As DataTable = GetData("Chart_ShareExpenses")
Dim Username As List(Of String) = (From p In dt.AsEnumerable()
Select p.Field(Of String)("Username")).Distinct().ToList()
Dim years As List(Of Int32) = (From p In dt.AsEnumerable()
Order By p.Field(Of Int32)("Day")
Select p.Field(Of Int32)("Day")).Distinct().ToList()
If Chart1.Series.Count() = 1 Then
Chart1.Series.Remove(Chart1.Series(0))
End If
Chart1.Series.Clear()
For Each Usernames As String In Username
Dim x As List(Of String) = New List(Of String)()
Dim y As List(Of Decimal) = New List(Of Decimal)()
For Each year As String In years
x.Add(year)
y.Add((From p In dt.AsEnumerable()
Where p.Field(Of String)("Username") = Usernames And p.Field(Of Int32)("Day") = year
Order By p.Field(Of Int32)("Day")
Select p.Field(Of Decimal)("Total")).FirstOrDefault())
Next
Chart1.Series.Add(New Series(Usernames))
Chart1.Series(Usernames).IsValueShownAsLabel = True
Chart1.Series(Usernames).BorderWidth = 2
Chart1.Series(Usernames).ChartType = SeriesChartType.Spline
Chart1.Series(Usernames).Points.DataBindXY(x, y)
Chart1.Series(Usernames).LabelForeColor = Color.MediumSeaGreen
Chart1.Series(Usernames).EmptyPointStyle.Color = Color.Red
Chart1.Series(Usernames).EmptyPointStyle.AxisLabel = "Empty"
Chart1.Series(Usernames).EmptyPointStyle.IsValueShownAsLabel = False
Next
Chart1.Legends(0).Enabled = True
Dim MyData As Single() = New Single(Chart1.Series.Count - 1) {}
For i = 0 To Chart1.Series.Count - 1
MyData(i) = Convert.ToSingle(5 * Math.Sin(i / CDbl(2)))
Dim withBlock = Chart1.Series(i)
withBlock.BorderWidth = 2
withBlock.MarkerStyle = MarkerStyle.Circle
withBlock.MarkerSize = 8
Chart1.Annotations.Add(New CalloutAnnotation())
Next
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Total", GetType(Decimal))
dt.Columns.Add("year", GetType(String))
dt.Columns.Add("Username", GetType(String))
dt.Columns.Add("Day", GetType(Integer))
dt.Rows.Add("34.35", "2020-07", "Alisa", 1)
dt.Rows.Add("5.25 ", "2020-07", "Alisa", 2)
dt.Rows.Add("52.55", "2020-07", "Alisa", 4)
dt.Rows.Add("10.81", "2020-07", "Alisa", 6)
dt.Rows.Add("32.84", "2020-07", "Alisa", 8)
dt.Rows.Add("25.20", "2020-07", "Alisa", 13)
dt.Rows.Add("49.74", "2020-07", "Alisa", 14)
dt.Rows.Add("60.36", "2020-07", "Alisa", 18)
dt.Rows.Add("34.00", "2020-07", "Alisa", 22)
dt.Rows.Add("5.00 ", "2020-07", "Alisa", 23)
dt.Rows.Add("4.90 ", "2020-07", "Elvis", 2)
dt.Rows.Add("9.89 ", "2020-07", "Elvis", 3)
dt.Rows.Add("13.13", "2020-07", "Elvis", 5)
dt.Rows.Add("27.25", "2020-07", "Elvis", 8)
dt.Rows.Add("46.37", "2020-07", "Elvis", 10)
dt.Rows.Add("32.36", "2020-07", "Elvis", 15)
dt.Rows.Add("3.90 ", "2020-07", "Elvis", 17)
dt.Rows.Add("22.30", "2020-07", "Elvis", 20)
dt.Rows.Add("53.80", "2020-07", "Elvis", 25)
Return dt
End Function
Private prevPosition As Point? = Nothing
Private tooltip As ToolTip = New ToolTip()
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Chart1.MouseMove
Dim pos = e.Location
If prevPosition.HasValue AndAlso pos = prevPosition.Value Then
tooltip.RemoveAll()
prevPosition = pos
Return
End If
Dim results = Chart1.HitTest(pos.X, pos.Y, False, ChartElementType.DataPoint)
For Each result In results
If result.ChartElementType = ChartElementType.DataPoint Then
Dim department As String = result.Series.Name
Dim year As String = result.Series.Points(result.PointIndex).AxisLabel
Dim total As Double = result.Series.Points(result.PointIndex).YValues(0)
Dim yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y)
tooltip.BackColor = Color.LightGray
tooltip.ToolTipTitle = "Data Information"
tooltip.Show(GetDetails(year, department), Chart1, pos.X, pos.Y - 15)
End If
Next
End Sub
Private Function GetDetails(ByVal day As String, ByVal userName As String) As String
Dim data As String = ""
Dim query As String = "SELECT Date,Description,Price,Quantity FROM [dbo].[ShareCost] WHERE DATEPART(DAY,Date) = @Day AND LTRIM(RTRIM(Username)) = @UserName"
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection(constr)
Using command As New SqlCommand(query, con)
command.CommandType = CommandType.Text
command.Parameters.AddWithValue("@Day", day.Trim())
command.Parameters.AddWithValue("@UserName", userName.Trim())
Using sda As SqlDataAdapter = New SqlDataAdapter(command)
sda.Fill(dt)
End Using
End Using
End Using
For Each row As DataRow In dt.Rows
Dim [date] As String = Convert.ToDateTime(row("Date")).ToString("dd/MM/yyyy")
Dim description As String = row("Description")
Dim price As String = If(row("Price").ToString = "", 0, row("Price").ToString)
Dim quantity As String = If(row("Quantity").ToString = "", 0, row("Quantity").ToString)
data = data & [date] & " - " & description & " - " & price & " - " & quantity & Environment.NewLine
Next
Return data
End Function
End Class
Screenshot
Values in MouseMove event