none
Add Horizontal line to line chart

    Question

  • Is there a way to add a horizontal line to line chart. I would like to
    use the mousedown event to select the start and ending line.
    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class FRM_MovingAverage
        Dim CL As Decimal
        Dim Answer As Decimal
        Dim cc As Decimal ' Current
        Dim pp As Decimal ' Previous
        Dim ma As Decimal
        Dim amount As Decimal
        Dim count As Integer = 0
        Dim CurrentAnnotation As New CalloutAnnotation
        Dim my_Quote As Decimal
        Private Sub FRM_MovingAverage_Activated(sender As Object, e As EventArgs) Handles Me.Activated
            Try
                Chart2.Annotations.Add(CurrentAnnotation)
            Catch ex As Exception
    
            End Try
            If Flag_Chart = True Then Exit Sub
            TextBox1.Text = Form_title_First
            Call Find_Min_Max(0, 0)
            Flag_Chart = True
            Call chart_it()
        End Sub
        Private Sub Find_Min_Max(ByRef xMin As Decimal, ByRef xMax As Decimal)
            For i As Integer = 1 To count_array - 1
                If i = 1 Then xMin = CDec(my_array(i, 2))
                If i = 1 Then xMax = CDec(my_array(i, 2))
                amount = CDec(my_array(i, 2))
                count = count + 1
                If amount <> 0 Then
                    If amount > xMax Then xMax = amount
                    If amount < xMin Then xMin = amount
                End If
            Next
            Chart2.ChartAreas(0).AxisY.Minimum = xMin - ((xMax - xMin) * 0.15)
            Chart2.ChartAreas(0).AxisY.Maximum = xMax + ((xMax - xMin) * 0.1)
        End Sub
    
        Private Sub chart_it()
            For i As Integer = 1 To count
    
                Chart2.ChartAreas(0).AxisX.TitleFont = New Font("Times New Roman", 260, FontStyle.Bold)
    
                Me.Chart2.Series(0).BorderWidth = 4
                Me.Chart2.Series(1).BorderWidth = 4
                Me.Chart2.Series(2).BorderWidth = 4
                Me.Chart2.Series(0).Color = Color.Green
                Me.Chart2.Series(1).Color = Color.Red
                Me.Chart2.Series(2).Color = Color.Blue
                Chart2.Series(0).Points.AddXY(my_array(i, 3), my_array(i, 2))
                Chart2.Series(1).Points.AddY(my_array(i, 5))
                Chart2.Series(2).Points.AddY(my_array(i, 6))
            Next
    
    
        End Sub
    
        Private Sub Mouse_Down(ByVal x As String)
            'Call Mouse_Down(e.Button.ToString)
            Try
                Select Case UCase(x)
                    Case "MIDDLE"
    
                    Case "RIGHT"
                        With CurrentAnnotation
                            .Text = " "
                        End With
                    Case "LEFT"
                    Case "XBUTTON1"
                        Close()
                End Select
            Catch ex As Exception
    
            End Try
    
        End Sub
        Private Sub Key_Down(ByVal x As String)
            'Call Key_Down(e.KeyCode.ToString)
            Select Case UCase(x)
                Case "F1"
    
                Case "F2" ' Add new Record
    
                Case "F3" ' Chart Record
                    Close()
                Case "F4"  ' Delete Record
    
                Case "F6"  ' Update Record
    
                Case "F7"
                    Close()
                Case "F8"
                Case "MENU" ' Alt Key
            End Select
        End Sub
        Private Sub BTN_Quit2_Click(sender As Object, e As EventArgs) Handles BTN_Quit2.Click
            Close()
        End Sub
    
        Private Sub Chart2_KeyDown(sender As Object, e As KeyEventArgs) Handles Chart2.KeyDown
            Call Key_Down(e.KeyCode.ToString)
        End Sub
    
        Private Sub Chart2_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart2.MouseDown
            Call Mouse_Down(e.Button.ToString)
        End Sub
    
        Private Sub Chart2_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart2.MouseMove
            Dim my_index As Integer
            Dim result As HitTestResult = Chart2.HitTest(e.X, e.Y)
            If result.ChartElementType = ChartElementType.DataPoint Then
                With CurrentAnnotation
                    .CalloutStyle = CalloutStyle.Borderline
                    .ForeColor = Color.Maroon
                    .Font = New Font("Tahoma", 10)
                    .AnchorDataPoint = Chart2.Series(result.Series.Name).Points(result.PointIndex)
                    .X = Chart2.Series(result.Series.Name).Points(result.PointIndex).XValue
                     my_index = result.PointIndex + 1
                    my_Quote = CDec(my_array(my_index, 2))
                    .Text = "Current: " & my_Quote.ToString("c3") & vbCrLf & "Date: " & my_array(my_index, 3)
                End With
                Chart2.Invalidate()
    
            End If
        End Sub
    
        Private Sub FRM_MovingAverage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    


    Charlie Soroka

    Tuesday, April 18, 2017 4:52 PM

Answers

  • Thank you for your reply, I was able to get your solution to work. 

    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Tuesday, April 25, 2017 1:16 AM
  • Is there a way to add a horizontal line to line chart. I would like to
    use the mousedown event to select the start and ending line.

    There is some discussion of this here:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/59514030-45cc-426a-937e-e1dff4ede2e1

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Tuesday, April 18, 2017 9:47 PM
  • Hi sorokateam,

    According to your description,  I guess you want to achieve the following function.

    Please refer to the sample below

    Public Class Form4
    
        Private startPoint As New Point(50, 217)
        Private endPoint As New Point(50, 217)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
    
        Private Sub Form4_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
            Dim g As Graphics = e.Graphics
            Dim vertFont As New Font("Verdana", 10, FontStyle.Bold)
            Dim horzFont As New Font("Verdana", 10, FontStyle.Bold)
            Dim vertBrush As New SolidBrush(Color.Black)
            Dim horzBrush As New SolidBrush(Color.Blue)
            Dim blackPen As New Pen(Color.Black)
            Dim bluePen As New Pen(Color.Blue)
            ' Drawing a vertical and a horizontal line 
            g.DrawLine(blackPen, 50, 220, 50, 25)
            g.DrawLine(bluePen, 50, 220, 250, 220)
            ' x-axis drawing 
            g.DrawString("0", horzFont, horzBrush, 30, 220)
            g.DrawString("1", horzFont, horzBrush, 50, 220)
            g.DrawString("2", horzFont, horzBrush, 70, 220)
            g.DrawString("3", horzFont, horzBrush, 90, 220)
            g.DrawString("4", horzFont, horzBrush, 110, 220)
            g.DrawString("5", horzFont, horzBrush, 130, 220)
            g.DrawString("6", horzFont, horzBrush, 150, 220)
            g.DrawString("7", horzFont, horzBrush, 170, 220)
            g.DrawString("8", horzFont, horzBrush, 190, 220)
            g.DrawString("9", horzFont, horzBrush, 210, 220)
            g.DrawString("10", horzFont, horzBrush, 230, 220)
            'Draw vertical strings 
            Dim vertStrFormat As New StringFormat()
            vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical
            g.DrawString("-", horzFont, horzBrush, 50, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 70, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 90, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 110, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 130, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 150, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 170, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 190, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 210, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 230, 212, vertStrFormat)
            ' y-axis drawing 
            g.DrawString("100 -", vertFont, vertBrush, 20, 20)
            g.DrawString("90 -", vertFont, vertBrush, 25, 40)
            g.DrawString("80 -", vertFont, vertBrush, 25, 60)
            g.DrawString("70 -", vertFont, vertBrush, 25, 80)
            g.DrawString("60 -", vertFont, vertBrush, 25, 100)
            g.DrawString("50 -", vertFont, vertBrush, 25, 120)
            g.DrawString("40 -", vertFont, vertBrush, 25, 140)
            g.DrawString("30 -", vertFont, vertBrush, 25, 160)
            g.DrawString("20 -", vertFont, vertBrush, 25, 180)
            g.DrawString("10 -", vertFont, vertBrush, 25, 200)
            ' Dispose of objects 
            vertFont.Dispose()
            horzFont.Dispose()
            vertBrush.Dispose()
            horzBrush.Dispose()
            blackPen.Dispose()
            bluePen.Dispose()
    
        End Sub
    
        Private Sub Form4_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
            If e.Button = MouseButtons.Left Then
                'Create a Graphics object 
                Dim g1 As Graphics = Me.CreateGraphics()
                ' Create two pens 
                Dim linePen As New Pen(Color.Green, 1)
                Dim ellipsePen As New Pen(Color.Red, 1)
                startPoint = endPoint
                endPoint = New Point(e.X, e.Y)
                ' Draw the line from the current point 
                ' to the new point 
    
                g1.DrawLine(linePen, startPoint, endPoint)
                ' If rectangle check box is checked, 
                ' draw a rectangle to represent the point 
                If CheckBox1.Checked Then
                    g1.DrawRectangle(ellipsePen, e.X - 2, e.Y - 2, 4, 4)
                Else
                    ' Draw a circle to represent the point 
                    g1.DrawEllipse(ellipsePen, e.X - 2, e.Y - 2, 4, 4)
                End If
                'Dispose of objects 
                linePen.Dispose()
                ellipsePen.Dispose()
                g1.Dispose()
            End If
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            startPoint.X = 50
            startPoint.Y = 217
            endPoint.X = 50
            endPoint.Y = 217
            Me.Invalidate(Me.ClientRectangle)
    
        End Sub
    End Class

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Wednesday, April 19, 2017 7:00 AM
    Moderator
  • Thank you for your reply. What I would like to do is draw start and ending lines on a chart.  Please

    see my image attached.

    Charlie


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Wednesday, April 19, 2017 2:15 PM
  • Your post was great code for form. I am using a chart and I don't think the code would work.

    Since my original post was not complete code, my mistake, I am uploading a simple form with

    chart that also has data needed to chart.  I hope this will make my question more understandable.

    Charlie

    Public Class Form1
        Dim CL As Decimal
        Dim Answer As Decimal
        Dim ma As Decimal
        Dim ma10 As Decimal
        Dim ma30 As Decimal
        Dim my_change As Decimal
        Dim my_index As Integer
        Dim my_vol As Decimal
        Dim amt_max As Decimal
        Dim amt_min As Decimal
        Dim my_low As Decimal
        Dim my_high As Decimal
        Dim my_close As Decimal
        Dim c As Decimal ' Current
        Dim p As Decimal ' Previous
        Dim dp As Decimal ' Data Point
        ' Fields used   Date       High , Low , Open, Close 
        Dim k As New List(Of dbdata1)() From {
         New dbdata1("3/15/2017", 8.37F),
         New dbdata1("3/16/2017", 8.25F),
         New dbdata1("3/17/2017", 8.32F),
         New dbdata1("3/20/2017", 8.27F),
         New dbdata1("3/21/2017", 7.41F),
         New dbdata1("3/22/2017", 7.62F),
         New dbdata1("3/23/2017", 7.53F),
         New dbdata1("3/24/2017", 7.34F),
         New dbdata1("3/27/2017", 6.81F),
         New dbdata1("3/28/2017", 7.34F),
         New dbdata1("3/29/2017", 7.2F),
         New dbdata1("3/30/2017", 7.23F),
         New dbdata1("3/31/2017", 7.28F),
         New dbdata1("4/03/2017", 7.06F),
         New dbdata1("4/04/2017", 7.19F),
         New dbdata1("4/05/2017", 6.83F),
         New dbdata1("4/06/2017", 7.09F),
         New dbdata1("4/07/2017", 7.65F),
         New dbdata1("4/10/2017", 7.23F),
         New dbdata1("4/11/2017", 6.95F)
                                                  }
        
        Private Sub chart_it()
            With Chart_MA
                ma10 = 10
                ma30 = 30
                .Series("10 Day MA").BorderWidth = 4
                .Series("10 Day MA").Color = Color.Lime
                .Series("30 Day MA").BorderWidth = 4
                .Series("30 Day MA").Color = Color.Red
                Call Find_Min_Max(0, 0)
            End With
    
    
            For i As Integer = 1 To k.Count - 1
                ' adding date and high
                'TB_Series0.Text = "Series 1 (" & ma10 & " Day MA)"
                Chart_MA.Series(0).Points.AddXY(k(i).My_Date, k(i).My_Current)
    
                'TB_Series1.Text = "Series 2 (" & ma30 & " Day MA)"
                dp = EMA(k(i).My_Current, k(i - 1).My_Current, i, ma30, Answer)
                Chart_MA.Series(1).Points.AddXY(k(i).My_Date, Answer)
            Next
        End Sub
        Private Sub Find_Min_Max(ByRef xMin As Decimal, ByRef xMax As Decimal)
            Dim count As Integer = 0
    
            For i As Integer = 1 To k.Count - 1
                If i = 1 Then
    
                    amt_min = CDec(k(i).My_Current)
                    amt_max = CDec(k(i).My_Current)
                End If
    
                xMin = CDec(k(i).My_Current)
                xMax = CDec(k(i).My_Current)
                count = count + 1
                If xMin <> 0 Then
                    If xMax <> 0 Then
                        If xMin < amt_min Then amt_min = xMin
                        If xMax > amt_max Then amt_max = xMax
                    End If
                End If
            Next
            Chart_MA.ChartAreas(0).AxisY.Minimum = amt_min - (amt_min * 0.02)
            Chart_MA.ChartAreas(0).AxisY.Maximum = amt_max + (amt_max * 0.02)
        End Sub
        Private Function EMA(ByVal c As Decimal, ByVal p As Decimal, ByVal i As Decimal, ByVal pt As Integer, ByRef r As Decimal)
            ' Stock Value   Stock Value-1   Position   Moving Average Periods   Return Value
            CL = Math.Round(2 / (1 + pt), 4) ' Round to 4 decimal places
            r = (c * CL) + (p * (1 - CL)) ' back a day for Previous
            'r = (CL * (c - p) + p) ' back a day for Previous
    
            Return r
        End Function
    
        Private Sub BTN_Quit_Click(sender As Object, e As EventArgs) Handles BTN_Quit.Click
            Close()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            chart_it()
        End Sub
    End Class
    Public Class dbdata1
        Public My_Date As String
        Public My_Current As Single
    
        Public Sub New(d As String, c As Single)
            My_Date = d
            My_Current = c
    
        End Sub
    End Class
    


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Wednesday, April 19, 2017 6:56 PM
  • Your post was great code for form. I am using a chart and I don't think the code would work.

    Which of the suggestions in that discussion did you implement?  What problem did you run into with the solution you selected?

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Wednesday, April 19, 2017 8:43 PM
  • Your Solution writes to a form, My solution needs to wirte to a chart that is on top of
    a form is the best that I can explain. To write a date & value to a chart I would use the statement:
    Chart_MA.Series(0).Points.AddXY(k(i).My_Date, k(i).My_Current). If you think that your solution

    can be used to write to a chart, I will give it a try.

    Charlie

    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Wednesday, April 19, 2017 11:39 PM
  • Your Solution writes to a form, My solution needs to wirte to a chart that is on top of
    a form is the best that I can explain.

    I did not provide a solution.  I referred you to a discussion where a number of solutions were provided, all of which draw to the chart.  Which of those solutions did you try, and what problem did you have implementing it?

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 12:31 AM
  • soro,

    Do you want to use data points that are plotted in the chart as shown here?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6768ed48-027e-4162-8180-16721ec6559d/how-to-display-information-of-a-line-chart-by-doing-mouse-over?forum=vbgeneral

    Or do you want to use the mouse click coordinates from anywhere on the chart?

    Then what do you want to do exactly? Draw and annotate the line from two points? Show a picture of the result you want.

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 12:54 AM
  • Thank you for your reply, I would like to use the mouse click coordinates from anywhere on the chart
    to draw a line from right mouse click 1 to right mouse click 2.  Please see jpeg file attached
    


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 1:56 AM
  • Thank you for your reply, I would like to use the mouse click coordinates from anywhere on the chart
    to draw a line from right mouse click 1 to right mouse click 2.  Please see jpeg file attached


    Charlie Soroka

    Here is a start.


    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form6
    
        Private r As New Random(Now.Millisecond)
        Private MouseDownPt As Point
        Private MouseMovePt As Point
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Chart1.Legends.Clear()
            Chart1.Series.Clear()
    
            Chart1.ChartAreas(0).AxisX.Minimum = 0
            Chart1.ChartAreas(0).AxisX.Maximum = 20
            Chart1.ChartAreas(0).AxisY.Minimum = 0
            Chart1.ChartAreas(0).AxisY.Maximum = 200
            Chart1.ChartAreas(0).AxisY.Interval = 100
            Chart1.ChartAreas(0).AxisX.Interval = 5
            Chart1.Series.Add("Series1")
            Chart1.Series("Series1").ChartType = SeriesChartType.FastLine
            Chart1.Series("Series1").BorderWidth = 0
    
            'just to add some random data points
            Dim y As Integer
            For i As Integer = 1 To 20
                y += r.Next(1, 20)
                Chart1.Series("Series1").Points.AddXY(i, y)
            Next
        End Sub
    
        Private Sub Chart1_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart1.MouseDown
            MouseDownPt = GetChartPointFromMousePoint(e.Location)
            MouseMovePt = MouseDownPt
        End Sub
    
        Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseMove
            If e.Button = MouseButtons.Left Then
                MouseMovePt = GetChartPointFromMousePoint(e.Location)
                Chart1.Invalidate()
            End If
        End Sub
    
        Private Function GetChartPointFromMousePoint(thisPt As Point) As Point
            GetChartPointFromMousePoint = Nothing
    
            Dim ht As DataVisualization.Charting.HitTestResult = Chart1.HitTest(thisPt.X, thisPt.Y)
            If Not ht.ChartElementType = ChartElementType.Nothing Then
                Dim xx As Double = ht.ChartArea.AxisX.PixelPositionToValue(thisPt.X)
                Dim yy As Double = ht.ChartArea.AxisY.PixelPositionToValue(thisPt.Y)
                If xx >= Chart1.ChartAreas(0).AxisX.Minimum And xx <= Chart1.ChartAreas(0).AxisX.Maximum And
                    yy >= Chart1.ChartAreas(0).AxisY.Minimum And yy <= Chart1.ChartAreas(0).AxisY.Maximum Then
                    GetChartPointFromMousePoint = New Point(xx, yy)
                End If
            End If
        End Function
    
        Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    
            Dim x1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, MouseDownPt.X))
            Dim y1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, MouseDownPt.Y))
    
            'convert to model
            Dim point1 As PointF = New Point(x1, y1)
            point1 = e.ChartGraphics.GetAbsolutePoint(point1)
    
            x1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, MouseMovePt.X))
            y1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, MouseMovePt.Y))
            Dim point2 As PointF = New Point(x1, y1)
            point2 = e.ChartGraphics.GetAbsolutePoint(point2)
    
            e.ChartGraphics.Graphics.DrawLine(Pens.Red, point1, point2)
    
        End Sub
    End Class
    
    


    • Edited by tommytwotrain Thursday, April 20, 2017 3:39 PM optimized
    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 1:23 PM
  • Thank you for your reply
    I have added your code to test application & added a third series for horizontal line
    as a new series3 for your code.  I lost 10 Day MA & 30 Day MA Series. Drawing
    Horizontal Line Works Great. What am I doing wrong on the merge. I Uploaded new code.
    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
        Dim CL As Decimal
        Dim Answer As Decimal
        Dim ma As Decimal
        Dim ma10 As Decimal
        Dim ma30 As Decimal
        Dim my_change As Decimal
        Dim my_index As Integer
        Dim my_vol As Decimal
        Dim amt_max As Decimal
        Dim amt_min As Decimal
        Dim my_low As Decimal
        Dim my_high As Decimal
        Dim my_close As Decimal
        Dim c As Decimal ' Current
        Dim p As Decimal ' Previous
        Dim dp As Decimal ' Data Point
        '========================Start New Code=====================================
        'show mouse pointer position in chart coordinates
        Private r As New Random(Now.Millisecond)
        Private MouseDownPt As Point
        Private MouseMovePt As Point
        Private MouseDownStage As Integer
    
        '========================End of New Code=====================================
    
        ' Fields used   Date       High , Low , Open, Close 
        Dim k As New List(Of dbdata1)() From {
         New dbdata1("3/15/2017", 8.37F),
         New dbdata1("3/16/2017", 8.25F),
         New dbdata1("3/17/2017", 8.32F),
         New dbdata1("3/20/2017", 8.27F),
         New dbdata1("3/21/2017", 7.41F),
         New dbdata1("3/22/2017", 7.62F),
         New dbdata1("3/23/2017", 7.53F),
         New dbdata1("3/24/2017", 7.34F),
         New dbdata1("3/27/2017", 6.81F),
         New dbdata1("3/28/2017", 7.34F),
         New dbdata1("3/29/2017", 7.2F),
         New dbdata1("3/30/2017", 7.23F),
         New dbdata1("3/31/2017", 7.28F),
         New dbdata1("4/03/2017", 7.06F),
         New dbdata1("4/04/2017", 7.19F),
         New dbdata1("4/05/2017", 6.83F),
         New dbdata1("4/06/2017", 7.09F),
         New dbdata1("4/07/2017", 7.65F),
         New dbdata1("4/10/2017", 7.23F),
         New dbdata1("4/11/2017", 6.95F)
                                                  }
    
        Private Sub chart_it()
            With Chart_MA
                ma10 = 10
                ma30 = 30
                .Series("10 Day MA").BorderWidth = 4
                .Series("10 Day MA").Color = Color.Lime
                .Series("30 Day MA").BorderWidth = 4
                .Series("30 Day MA").Color = Color.Red
                Call Find_Min_Max(0, 0)
            End With
    
    
            For i As Integer = 1 To k.Count - 1
                ' adding date and high
                'TB_Series0.Text = "Series 1 (" & ma10 & " Day MA)"
                Chart_MA.Series(0).Points.AddXY(k(i).My_Date, k(i).My_Current)
    
                'TB_Series1.Text = "Series 2 (" & ma30 & " Day MA)"
                dp = EMA(k(i).My_Current, k(i - 1).My_Current, i, ma30, Answer)
                Chart_MA.Series(1).Points.AddXY(k(i).My_Date, Answer)
            Next
         
        
    
        End Sub
        Private Sub Find_Min_Max(ByRef xMin As Decimal, ByRef xMax As Decimal)
            Dim count As Integer = 0
    
            For i As Integer = 1 To k.Count - 1
                If i = 1 Then
    
                    amt_min = CDec(k(i).My_Current)
                    amt_max = CDec(k(i).My_Current)
                End If
    
                xMin = CDec(k(i).My_Current)
                xMax = CDec(k(i).My_Current)
                count = count + 1
                If xMin <> 0 Then
                    If xMax <> 0 Then
                        If xMin < amt_min Then amt_min = xMin
                        If xMax > amt_max Then amt_max = xMax
                    End If
                End If
            Next
            Chart_MA.ChartAreas(0).AxisY.Minimum = amt_min - (amt_min * 0.02)
            Chart_MA.ChartAreas(0).AxisY.Maximum = amt_max + (amt_max * 0.02)
        End Sub
        Private Function EMA(ByVal c As Decimal, ByVal p As Decimal, ByVal i As Decimal, ByVal pt As Integer, ByRef r As Decimal)
            ' Stock Value   Stock Value-1   Position   Moving Average Periods   Return Value
            CL = Math.Round(2 / (1 + pt), 4) ' Round to 4 decimal places
            r = (c * CL) + (p * (1 - CL)) ' back a day for Previous
            'r = (CL * (c - p) + p) ' back a day for Previous
    
            Return r
        End Function
    
        Private Sub BTN_Quit_Click(sender As Object, e As EventArgs) Handles BTN_Quit.Click
            Close()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            chart_it()
            Call Load_It() ' Addedd
        End Sub
        Private Sub Load_It()
            '========================Start New Code=====================================
            Chart_MA.Legends.Clear()
            Chart_MA.Series.Clear()
    
            Chart_MA.ChartAreas(0).AxisX.Minimum = 0
            Chart_MA.ChartAreas(0).AxisX.Maximum = 50
            Chart_MA.ChartAreas(0).AxisY.Minimum = 0
            Chart_MA.ChartAreas(0).AxisY.Maximum = 500
            Chart_MA.ChartAreas(0).AxisY.Interval = 100
            Chart_MA.ChartAreas(0).AxisX.Interval = 10
            Chart_MA.Series.Add("Series3")
            Chart_MA.Series("Series3").ChartType = SeriesChartType.FastLine
            Chart_MA.Series("Series3").BorderWidth = 0
    
            'just to add some random data points
            'Dim y As Integer
            'For i As Integer = 1 To 50
            '    y += r.Next(1, 20)
            '    Chart_MA.Series("Series3").Points.AddXY(i, y)
            'Next
        End Sub
    
        Private Sub Chart_MA_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart_MA.MouseDown
            MouseDownPt = GetChartPointFromMousePoint(e.Location)
            MouseDownStage = 1
            Chart_MA.Invalidate()
        End Sub
    
        Private Sub Chart_MA_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart_MA.MouseMove
            If e.Button = MouseButtons.Left And MouseDownStage = 1 Then
                MouseMovePt = GetChartPointFromMousePoint(e.Location)
                If MouseMovePt.X > 0 Then Chart_MA.Invalidate()
            End If
        End Sub
    
        Private Sub Chart_MA_MouseUp(sender As Object, e As MouseEventArgs) Handles Chart_MA.MouseUp
            MouseDownStage = 0
        End Sub
        Private Function GetChartPointFromMousePoint(thisPt As Point) As Point
            GetChartPointFromMousePoint = Nothing
    
            Dim ht As DataVisualization.Charting.HitTestResult = Chart_MA.HitTest(thisPt.X, thisPt.Y)
            If Not ht.ChartElementType = ChartElementType.Nothing Then
                Dim xx As Double = ht.ChartArea.AxisX.PixelPositionToValue(thisPt.X)
                Dim yy As Double = ht.ChartArea.AxisY.PixelPositionToValue(thisPt.Y)
                If xx >= Chart_MA.ChartAreas(0).AxisX.Minimum And xx <= Chart_MA.ChartAreas(0).AxisX.Maximum And
                    yy >= Chart_MA.ChartAreas(0).AxisY.Minimum And yy <= Chart_MA.ChartAreas(0).AxisY.Maximum Then
                    GetChartPointFromMousePoint = New Point(xx, yy)
                End If
            End If
        End Function
    
        Private Sub Chart_MA_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart_MA.PostPaint
            Dim x1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.X, MouseDownPt.X))
            Dim y1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.Y, MouseDownPt.Y))
    
            'convert to model
            Dim point1 As PointF = New Point(x1, y1)
            point1 = e.ChartGraphics.GetAbsolutePoint(point1)
    
            x1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.X, MouseMovePt.X))
            y1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.Y, MouseMovePt.Y))
            Dim point2 As PointF = New Point(x1, y1)
            point2 = e.ChartGraphics.GetAbsolutePoint(point2)
    
            e.ChartGraphics.Graphics.DrawLine(Pens.Black, point1, point2)
            '========================End of New Code=====================================
        End Sub
    End Class
    Public Class dbdata1
        Public My_Date As String
        Public My_Current As Single
    
        Public Sub New(d As String, c As Single)
            My_Date = d
            My_Current = c
        End Sub
    End Class
    
    #Region "Comments"
    
    '  Chart_MA.ChartAreas(0).AxisY.StripLines.Add(New StripLine)
    '        Chart_MA.ChartAreas(0).BackColor = Color.FromArgb(10, 252, 180, 65)
    '        Chart_MA.ChartAreas(0).AxisY.StripLines(0).StripWidth = 40
    '        Chart_MA.ChartAreas(0).AxisY.StripLines(0).Interval = 1000
    '        Chart_MA.ChartAreas(0).AxisY.StripLines(0).IntervalOffset = 20
    
    '        Chart_MA.ChartAreas(0).CursorY.Position = 3
    '        Chart_MA.ChartAreas(0).CursorY.LineWidth = 3
    '        Chart_MA.ChartAreas(0).CursorY.LineColor = Color.Black
    
    
    #End Region
    


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 6:13 PM
  • soro,

    It appears you are setting the axis with your max min functions so you don't need the code from my example that sets it:

        Chart_MA.ChartAreas(0).AxisX.Minimum = 0
            Chart_MA.ChartAreas(0).AxisX.Maximum = 50
            Chart_MA.ChartAreas(0).AxisY.Minimum = 0
            Chart_MA.ChartAreas(0).AxisY.Maximum = 500
            Chart_MA.ChartAreas(0).AxisY.Interval = 100
            Chart_MA.ChartAreas(0).AxisX.Interval = 10



    PS Note the update I made to the mouse event code in my example that simplifies and sets the mousemovept in mousedown so you don't get a ghost point at zero when mouse down.
    • Edited by tommytwotrain Thursday, April 20, 2017 8:17 PM
    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 7:52 PM
  • I commented the code for min and max but 10 Day MA & 30 Day MA Series still don't display.

    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 8:28 PM
  • I commented the code for min and max but 10 Day MA & 30 Day MA Series still don't display.

    Charlie Soroka

    Your example requires the data right? So I cant run it and I am not sure what it does.

    However, the code to do this line has nothing to do with the rest of the chart drawing code.

    So you should be able to insert just the mouse and paint event codes without changing the rest of your chart code.

    Try it again I guess. Do not global search and replace etc. You only add the event code with the correct names of your chart. Watch the series names and use of series(0) and series(1).

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 9:06 PM
  • My application does not require data, data is in array: Dim k As New List(Of dbdata1)() From {
         New dbdata1("3/15/2017", 8.37F),
         New dbdata1("3/16/2017", 8.25F),
         New dbdata1("3/17/2017", 8.32F),
         New dbdata1("3/20/2017", 8.27F),
         New dbdata1("3/21/2017", 7.41F),
         New dbdata1("3/22/2017", 7.62F),
         New dbdata1("3/23/2017", 7.53F),
         New dbdata1("3/24/2017", 7.34F),
         New dbdata1("3/27/2017", 6.81F),
         New dbdata1("3/28/2017", 7.34F),
         New dbdata1("3/29/2017", 7.2F),
         New dbdata1("3/30/2017", 7.23F),
         New dbdata1("3/31/2017", 7.28F),
         New dbdata1("4/03/2017", 7.06F),
         New dbdata1("4/04/2017", 7.19F),
         New dbdata1("4/05/2017", 6.83F),
         New dbdata1("4/06/2017", 7.09F),
         New dbdata1("4/07/2017", 7.65F),
         New dbdata1("4/10/2017", 7.23F),
         New dbdata1("4/11/2017", 6.95F)
                                                  }

    Charlie


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 10:11 PM
  • soro,

    in form load you call it this:

          Chart_MA.Series.Add("Series3")


    but in chart_it you call it this:

           .Series("10 Day MA").BorderWidth = 4

    You should use Option Strict On to have Visual Studio point out more simple errors although this is not one.

    PS In chart it you have

          Chart_MA.Series(1).Points.AddXY(k(i).My_Date, Answer)

    but you have only defined one series in form load. So it should be  Chart_MA.Series(0). or you need to define the second series.

    It appears you removed the other series you say are not plotting and then forgot?

    I cant get your example to run because there are too many problems like this and I don't know exactly what you are doing so all I can do is rewrite it which I did already for my example.

    As I say, all you need to do is insert the mouse event code from my example and change the names. You don't need to modify the chart code you have.


    • Edited by tommytwotrain Friday, April 21, 2017 12:08 PM
    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Friday, April 21, 2017 11:38 AM
  • Thank you for your reply.  The statement Chart_MA_Series.ADD("Series3") was for your code.  There are three series the "10 Day MA" & "30 Day MA" that was added in Chart properties for my code, sorry about that. The 3rd series was "Series3" for your code. You can turn your code on or off by the Load statement "Load_it()", my chart code is turned on or off by "Chart_it"

    I commented your code "Load_it" and chart code runs normal. I am sorry I did not explain the 2 series that was not added in code. I hope I have explained it correctly.

    Charlie


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Friday, April 21, 2017 1:56 PM
  • Thank you for your reply.  The statement Chart_MA_Series.ADD("Series3") was for your code.  There are three series the "10 Day MA" & "30 Day MA" that was added in Chart properties for my code, sorry about that. The 3rd series was "Series3" for your code. You can turn your code on or off by the Load statement "Load_it()", my chart code is turned on or off by "Chart_it"

    I commented your code "Load_it" and chart code runs normal. I am sorry I did not explain the 2 series that was not added in code. I hope I have explained it correctly.

    Charlie


    Charlie Soroka

    Just insert the code I gave you from the mouse events into your chart code and change the chart names and series names in the mouse event code you add to whatever you are using now. You don't need to modify any of your existing code.

    That's all I am going to do for you. My example runs. Yours does not.

    I showed you how to fish ...

    :)

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Friday, April 21, 2017 2:18 PM

All replies

  • Is there a way to add a horizontal line to line chart. I would like to
    use the mousedown event to select the start and ending line.

    There is some discussion of this here:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/59514030-45cc-426a-937e-e1dff4ede2e1

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Tuesday, April 18, 2017 9:47 PM
  • Hi sorokateam,

    According to your description,  I guess you want to achieve the following function.

    Please refer to the sample below

    Public Class Form4
    
        Private startPoint As New Point(50, 217)
        Private endPoint As New Point(50, 217)
    
        Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
    
        Private Sub Form4_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
            Dim g As Graphics = e.Graphics
            Dim vertFont As New Font("Verdana", 10, FontStyle.Bold)
            Dim horzFont As New Font("Verdana", 10, FontStyle.Bold)
            Dim vertBrush As New SolidBrush(Color.Black)
            Dim horzBrush As New SolidBrush(Color.Blue)
            Dim blackPen As New Pen(Color.Black)
            Dim bluePen As New Pen(Color.Blue)
            ' Drawing a vertical and a horizontal line 
            g.DrawLine(blackPen, 50, 220, 50, 25)
            g.DrawLine(bluePen, 50, 220, 250, 220)
            ' x-axis drawing 
            g.DrawString("0", horzFont, horzBrush, 30, 220)
            g.DrawString("1", horzFont, horzBrush, 50, 220)
            g.DrawString("2", horzFont, horzBrush, 70, 220)
            g.DrawString("3", horzFont, horzBrush, 90, 220)
            g.DrawString("4", horzFont, horzBrush, 110, 220)
            g.DrawString("5", horzFont, horzBrush, 130, 220)
            g.DrawString("6", horzFont, horzBrush, 150, 220)
            g.DrawString("7", horzFont, horzBrush, 170, 220)
            g.DrawString("8", horzFont, horzBrush, 190, 220)
            g.DrawString("9", horzFont, horzBrush, 210, 220)
            g.DrawString("10", horzFont, horzBrush, 230, 220)
            'Draw vertical strings 
            Dim vertStrFormat As New StringFormat()
            vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical
            g.DrawString("-", horzFont, horzBrush, 50, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 70, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 90, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 110, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 130, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 150, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 170, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 190, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 210, 212, vertStrFormat)
            g.DrawString("-", horzFont, horzBrush, 230, 212, vertStrFormat)
            ' y-axis drawing 
            g.DrawString("100 -", vertFont, vertBrush, 20, 20)
            g.DrawString("90 -", vertFont, vertBrush, 25, 40)
            g.DrawString("80 -", vertFont, vertBrush, 25, 60)
            g.DrawString("70 -", vertFont, vertBrush, 25, 80)
            g.DrawString("60 -", vertFont, vertBrush, 25, 100)
            g.DrawString("50 -", vertFont, vertBrush, 25, 120)
            g.DrawString("40 -", vertFont, vertBrush, 25, 140)
            g.DrawString("30 -", vertFont, vertBrush, 25, 160)
            g.DrawString("20 -", vertFont, vertBrush, 25, 180)
            g.DrawString("10 -", vertFont, vertBrush, 25, 200)
            ' Dispose of objects 
            vertFont.Dispose()
            horzFont.Dispose()
            vertBrush.Dispose()
            horzBrush.Dispose()
            blackPen.Dispose()
            bluePen.Dispose()
    
        End Sub
    
        Private Sub Form4_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
            If e.Button = MouseButtons.Left Then
                'Create a Graphics object 
                Dim g1 As Graphics = Me.CreateGraphics()
                ' Create two pens 
                Dim linePen As New Pen(Color.Green, 1)
                Dim ellipsePen As New Pen(Color.Red, 1)
                startPoint = endPoint
                endPoint = New Point(e.X, e.Y)
                ' Draw the line from the current point 
                ' to the new point 
    
                g1.DrawLine(linePen, startPoint, endPoint)
                ' If rectangle check box is checked, 
                ' draw a rectangle to represent the point 
                If CheckBox1.Checked Then
                    g1.DrawRectangle(ellipsePen, e.X - 2, e.Y - 2, 4, 4)
                Else
                    ' Draw a circle to represent the point 
                    g1.DrawEllipse(ellipsePen, e.X - 2, e.Y - 2, 4, 4)
                End If
                'Dispose of objects 
                linePen.Dispose()
                ellipsePen.Dispose()
                g1.Dispose()
            End If
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            startPoint.X = 50
            startPoint.Y = 217
            endPoint.X = 50
            endPoint.Y = 217
            Me.Invalidate(Me.ClientRectangle)
    
        End Sub
    End Class

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Wednesday, April 19, 2017 7:00 AM
    Moderator
  • Thank you for your reply. What I would like to do is draw start and ending lines on a chart.  Please

    see my image attached.

    Charlie


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Wednesday, April 19, 2017 2:15 PM
  • Your post was great code for form. I am using a chart and I don't think the code would work.

    Since my original post was not complete code, my mistake, I am uploading a simple form with

    chart that also has data needed to chart.  I hope this will make my question more understandable.

    Charlie

    Public Class Form1
        Dim CL As Decimal
        Dim Answer As Decimal
        Dim ma As Decimal
        Dim ma10 As Decimal
        Dim ma30 As Decimal
        Dim my_change As Decimal
        Dim my_index As Integer
        Dim my_vol As Decimal
        Dim amt_max As Decimal
        Dim amt_min As Decimal
        Dim my_low As Decimal
        Dim my_high As Decimal
        Dim my_close As Decimal
        Dim c As Decimal ' Current
        Dim p As Decimal ' Previous
        Dim dp As Decimal ' Data Point
        ' Fields used   Date       High , Low , Open, Close 
        Dim k As New List(Of dbdata1)() From {
         New dbdata1("3/15/2017", 8.37F),
         New dbdata1("3/16/2017", 8.25F),
         New dbdata1("3/17/2017", 8.32F),
         New dbdata1("3/20/2017", 8.27F),
         New dbdata1("3/21/2017", 7.41F),
         New dbdata1("3/22/2017", 7.62F),
         New dbdata1("3/23/2017", 7.53F),
         New dbdata1("3/24/2017", 7.34F),
         New dbdata1("3/27/2017", 6.81F),
         New dbdata1("3/28/2017", 7.34F),
         New dbdata1("3/29/2017", 7.2F),
         New dbdata1("3/30/2017", 7.23F),
         New dbdata1("3/31/2017", 7.28F),
         New dbdata1("4/03/2017", 7.06F),
         New dbdata1("4/04/2017", 7.19F),
         New dbdata1("4/05/2017", 6.83F),
         New dbdata1("4/06/2017", 7.09F),
         New dbdata1("4/07/2017", 7.65F),
         New dbdata1("4/10/2017", 7.23F),
         New dbdata1("4/11/2017", 6.95F)
                                                  }
        
        Private Sub chart_it()
            With Chart_MA
                ma10 = 10
                ma30 = 30
                .Series("10 Day MA").BorderWidth = 4
                .Series("10 Day MA").Color = Color.Lime
                .Series("30 Day MA").BorderWidth = 4
                .Series("30 Day MA").Color = Color.Red
                Call Find_Min_Max(0, 0)
            End With
    
    
            For i As Integer = 1 To k.Count - 1
                ' adding date and high
                'TB_Series0.Text = "Series 1 (" & ma10 & " Day MA)"
                Chart_MA.Series(0).Points.AddXY(k(i).My_Date, k(i).My_Current)
    
                'TB_Series1.Text = "Series 2 (" & ma30 & " Day MA)"
                dp = EMA(k(i).My_Current, k(i - 1).My_Current, i, ma30, Answer)
                Chart_MA.Series(1).Points.AddXY(k(i).My_Date, Answer)
            Next
        End Sub
        Private Sub Find_Min_Max(ByRef xMin As Decimal, ByRef xMax As Decimal)
            Dim count As Integer = 0
    
            For i As Integer = 1 To k.Count - 1
                If i = 1 Then
    
                    amt_min = CDec(k(i).My_Current)
                    amt_max = CDec(k(i).My_Current)
                End If
    
                xMin = CDec(k(i).My_Current)
                xMax = CDec(k(i).My_Current)
                count = count + 1
                If xMin <> 0 Then
                    If xMax <> 0 Then
                        If xMin < amt_min Then amt_min = xMin
                        If xMax > amt_max Then amt_max = xMax
                    End If
                End If
            Next
            Chart_MA.ChartAreas(0).AxisY.Minimum = amt_min - (amt_min * 0.02)
            Chart_MA.ChartAreas(0).AxisY.Maximum = amt_max + (amt_max * 0.02)
        End Sub
        Private Function EMA(ByVal c As Decimal, ByVal p As Decimal, ByVal i As Decimal, ByVal pt As Integer, ByRef r As Decimal)
            ' Stock Value   Stock Value-1   Position   Moving Average Periods   Return Value
            CL = Math.Round(2 / (1 + pt), 4) ' Round to 4 decimal places
            r = (c * CL) + (p * (1 - CL)) ' back a day for Previous
            'r = (CL * (c - p) + p) ' back a day for Previous
    
            Return r
        End Function
    
        Private Sub BTN_Quit_Click(sender As Object, e As EventArgs) Handles BTN_Quit.Click
            Close()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            chart_it()
        End Sub
    End Class
    Public Class dbdata1
        Public My_Date As String
        Public My_Current As Single
    
        Public Sub New(d As String, c As Single)
            My_Date = d
            My_Current = c
    
        End Sub
    End Class
    


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Wednesday, April 19, 2017 6:56 PM
  • Your post was great code for form. I am using a chart and I don't think the code would work.

    Which of the suggestions in that discussion did you implement?  What problem did you run into with the solution you selected?

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Wednesday, April 19, 2017 8:43 PM
  • Your Solution writes to a form, My solution needs to wirte to a chart that is on top of
    a form is the best that I can explain. To write a date & value to a chart I would use the statement:
    Chart_MA.Series(0).Points.AddXY(k(i).My_Date, k(i).My_Current). If you think that your solution

    can be used to write to a chart, I will give it a try.

    Charlie

    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Wednesday, April 19, 2017 11:39 PM
  • Your Solution writes to a form, My solution needs to wirte to a chart that is on top of
    a form is the best that I can explain.

    I did not provide a solution.  I referred you to a discussion where a number of solutions were provided, all of which draw to the chart.  Which of those solutions did you try, and what problem did you have implementing it?

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 12:31 AM
  • soro,

    Do you want to use data points that are plotted in the chart as shown here?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/6768ed48-027e-4162-8180-16721ec6559d/how-to-display-information-of-a-line-chart-by-doing-mouse-over?forum=vbgeneral

    Or do you want to use the mouse click coordinates from anywhere on the chart?

    Then what do you want to do exactly? Draw and annotate the line from two points? Show a picture of the result you want.

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 12:54 AM
  • Thank you for your reply, I would like to use the mouse click coordinates from anywhere on the chart
    to draw a line from right mouse click 1 to right mouse click 2.  Please see jpeg file attached
    


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 1:56 AM
  • Thank you for your reply, I would like to use the mouse click coordinates from anywhere on the chart
    to draw a line from right mouse click 1 to right mouse click 2.  Please see jpeg file attached


    Charlie Soroka

    Here is a start.


    Imports System.Windows.Forms.DataVisualization.Charting
    Public Class Form6
    
        Private r As New Random(Now.Millisecond)
        Private MouseDownPt As Point
        Private MouseMovePt As Point
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Chart1.Legends.Clear()
            Chart1.Series.Clear()
    
            Chart1.ChartAreas(0).AxisX.Minimum = 0
            Chart1.ChartAreas(0).AxisX.Maximum = 20
            Chart1.ChartAreas(0).AxisY.Minimum = 0
            Chart1.ChartAreas(0).AxisY.Maximum = 200
            Chart1.ChartAreas(0).AxisY.Interval = 100
            Chart1.ChartAreas(0).AxisX.Interval = 5
            Chart1.Series.Add("Series1")
            Chart1.Series("Series1").ChartType = SeriesChartType.FastLine
            Chart1.Series("Series1").BorderWidth = 0
    
            'just to add some random data points
            Dim y As Integer
            For i As Integer = 1 To 20
                y += r.Next(1, 20)
                Chart1.Series("Series1").Points.AddXY(i, y)
            Next
        End Sub
    
        Private Sub Chart1_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart1.MouseDown
            MouseDownPt = GetChartPointFromMousePoint(e.Location)
            MouseMovePt = MouseDownPt
        End Sub
    
        Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseMove
            If e.Button = MouseButtons.Left Then
                MouseMovePt = GetChartPointFromMousePoint(e.Location)
                Chart1.Invalidate()
            End If
        End Sub
    
        Private Function GetChartPointFromMousePoint(thisPt As Point) As Point
            GetChartPointFromMousePoint = Nothing
    
            Dim ht As DataVisualization.Charting.HitTestResult = Chart1.HitTest(thisPt.X, thisPt.Y)
            If Not ht.ChartElementType = ChartElementType.Nothing Then
                Dim xx As Double = ht.ChartArea.AxisX.PixelPositionToValue(thisPt.X)
                Dim yy As Double = ht.ChartArea.AxisY.PixelPositionToValue(thisPt.Y)
                If xx >= Chart1.ChartAreas(0).AxisX.Minimum And xx <= Chart1.ChartAreas(0).AxisX.Maximum And
                    yy >= Chart1.ChartAreas(0).AxisY.Minimum And yy <= Chart1.ChartAreas(0).AxisY.Maximum Then
                    GetChartPointFromMousePoint = New Point(xx, yy)
                End If
            End If
        End Function
    
        Private Sub Chart1_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart1.PostPaint
    
            Dim x1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, MouseDownPt.X))
            Dim y1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, MouseDownPt.Y))
    
            'convert to model
            Dim point1 As PointF = New Point(x1, y1)
            point1 = e.ChartGraphics.GetAbsolutePoint(point1)
    
            x1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.X, MouseMovePt.X))
            y1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart1.ChartAreas(0).Name, AxisName.Y, MouseMovePt.Y))
            Dim point2 As PointF = New Point(x1, y1)
            point2 = e.ChartGraphics.GetAbsolutePoint(point2)
    
            e.ChartGraphics.Graphics.DrawLine(Pens.Red, point1, point2)
    
        End Sub
    End Class
    
    


    • Edited by tommytwotrain Thursday, April 20, 2017 3:39 PM optimized
    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 1:23 PM
  • Thank you for your reply
    I have added your code to test application & added a third series for horizontal line
    as a new series3 for your code.  I lost 10 Day MA & 30 Day MA Series. Drawing
    Horizontal Line Works Great. What am I doing wrong on the merge. I Uploaded new code.
    Imports System.Windows.Forms.DataVisualization.Charting
    
    Public Class Form1
        Dim CL As Decimal
        Dim Answer As Decimal
        Dim ma As Decimal
        Dim ma10 As Decimal
        Dim ma30 As Decimal
        Dim my_change As Decimal
        Dim my_index As Integer
        Dim my_vol As Decimal
        Dim amt_max As Decimal
        Dim amt_min As Decimal
        Dim my_low As Decimal
        Dim my_high As Decimal
        Dim my_close As Decimal
        Dim c As Decimal ' Current
        Dim p As Decimal ' Previous
        Dim dp As Decimal ' Data Point
        '========================Start New Code=====================================
        'show mouse pointer position in chart coordinates
        Private r As New Random(Now.Millisecond)
        Private MouseDownPt As Point
        Private MouseMovePt As Point
        Private MouseDownStage As Integer
    
        '========================End of New Code=====================================
    
        ' Fields used   Date       High , Low , Open, Close 
        Dim k As New List(Of dbdata1)() From {
         New dbdata1("3/15/2017", 8.37F),
         New dbdata1("3/16/2017", 8.25F),
         New dbdata1("3/17/2017", 8.32F),
         New dbdata1("3/20/2017", 8.27F),
         New dbdata1("3/21/2017", 7.41F),
         New dbdata1("3/22/2017", 7.62F),
         New dbdata1("3/23/2017", 7.53F),
         New dbdata1("3/24/2017", 7.34F),
         New dbdata1("3/27/2017", 6.81F),
         New dbdata1("3/28/2017", 7.34F),
         New dbdata1("3/29/2017", 7.2F),
         New dbdata1("3/30/2017", 7.23F),
         New dbdata1("3/31/2017", 7.28F),
         New dbdata1("4/03/2017", 7.06F),
         New dbdata1("4/04/2017", 7.19F),
         New dbdata1("4/05/2017", 6.83F),
         New dbdata1("4/06/2017", 7.09F),
         New dbdata1("4/07/2017", 7.65F),
         New dbdata1("4/10/2017", 7.23F),
         New dbdata1("4/11/2017", 6.95F)
                                                  }
    
        Private Sub chart_it()
            With Chart_MA
                ma10 = 10
                ma30 = 30
                .Series("10 Day MA").BorderWidth = 4
                .Series("10 Day MA").Color = Color.Lime
                .Series("30 Day MA").BorderWidth = 4
                .Series("30 Day MA").Color = Color.Red
                Call Find_Min_Max(0, 0)
            End With
    
    
            For i As Integer = 1 To k.Count - 1
                ' adding date and high
                'TB_Series0.Text = "Series 1 (" & ma10 & " Day MA)"
                Chart_MA.Series(0).Points.AddXY(k(i).My_Date, k(i).My_Current)
    
                'TB_Series1.Text = "Series 2 (" & ma30 & " Day MA)"
                dp = EMA(k(i).My_Current, k(i - 1).My_Current, i, ma30, Answer)
                Chart_MA.Series(1).Points.AddXY(k(i).My_Date, Answer)
            Next
         
        
    
        End Sub
        Private Sub Find_Min_Max(ByRef xMin As Decimal, ByRef xMax As Decimal)
            Dim count As Integer = 0
    
            For i As Integer = 1 To k.Count - 1
                If i = 1 Then
    
                    amt_min = CDec(k(i).My_Current)
                    amt_max = CDec(k(i).My_Current)
                End If
    
                xMin = CDec(k(i).My_Current)
                xMax = CDec(k(i).My_Current)
                count = count + 1
                If xMin <> 0 Then
                    If xMax <> 0 Then
                        If xMin < amt_min Then amt_min = xMin
                        If xMax > amt_max Then amt_max = xMax
                    End If
                End If
            Next
            Chart_MA.ChartAreas(0).AxisY.Minimum = amt_min - (amt_min * 0.02)
            Chart_MA.ChartAreas(0).AxisY.Maximum = amt_max + (amt_max * 0.02)
        End Sub
        Private Function EMA(ByVal c As Decimal, ByVal p As Decimal, ByVal i As Decimal, ByVal pt As Integer, ByRef r As Decimal)
            ' Stock Value   Stock Value-1   Position   Moving Average Periods   Return Value
            CL = Math.Round(2 / (1 + pt), 4) ' Round to 4 decimal places
            r = (c * CL) + (p * (1 - CL)) ' back a day for Previous
            'r = (CL * (c - p) + p) ' back a day for Previous
    
            Return r
        End Function
    
        Private Sub BTN_Quit_Click(sender As Object, e As EventArgs) Handles BTN_Quit.Click
            Close()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            chart_it()
            Call Load_It() ' Addedd
        End Sub
        Private Sub Load_It()
            '========================Start New Code=====================================
            Chart_MA.Legends.Clear()
            Chart_MA.Series.Clear()
    
            Chart_MA.ChartAreas(0).AxisX.Minimum = 0
            Chart_MA.ChartAreas(0).AxisX.Maximum = 50
            Chart_MA.ChartAreas(0).AxisY.Minimum = 0
            Chart_MA.ChartAreas(0).AxisY.Maximum = 500
            Chart_MA.ChartAreas(0).AxisY.Interval = 100
            Chart_MA.ChartAreas(0).AxisX.Interval = 10
            Chart_MA.Series.Add("Series3")
            Chart_MA.Series("Series3").ChartType = SeriesChartType.FastLine
            Chart_MA.Series("Series3").BorderWidth = 0
    
            'just to add some random data points
            'Dim y As Integer
            'For i As Integer = 1 To 50
            '    y += r.Next(1, 20)
            '    Chart_MA.Series("Series3").Points.AddXY(i, y)
            'Next
        End Sub
    
        Private Sub Chart_MA_MouseDown(sender As Object, e As MouseEventArgs) Handles Chart_MA.MouseDown
            MouseDownPt = GetChartPointFromMousePoint(e.Location)
            MouseDownStage = 1
            Chart_MA.Invalidate()
        End Sub
    
        Private Sub Chart_MA_MouseMove(sender As Object, e As MouseEventArgs) Handles Chart_MA.MouseMove
            If e.Button = MouseButtons.Left And MouseDownStage = 1 Then
                MouseMovePt = GetChartPointFromMousePoint(e.Location)
                If MouseMovePt.X > 0 Then Chart_MA.Invalidate()
            End If
        End Sub
    
        Private Sub Chart_MA_MouseUp(sender As Object, e As MouseEventArgs) Handles Chart_MA.MouseUp
            MouseDownStage = 0
        End Sub
        Private Function GetChartPointFromMousePoint(thisPt As Point) As Point
            GetChartPointFromMousePoint = Nothing
    
            Dim ht As DataVisualization.Charting.HitTestResult = Chart_MA.HitTest(thisPt.X, thisPt.Y)
            If Not ht.ChartElementType = ChartElementType.Nothing Then
                Dim xx As Double = ht.ChartArea.AxisX.PixelPositionToValue(thisPt.X)
                Dim yy As Double = ht.ChartArea.AxisY.PixelPositionToValue(thisPt.Y)
                If xx >= Chart_MA.ChartAreas(0).AxisX.Minimum And xx <= Chart_MA.ChartAreas(0).AxisX.Maximum And
                    yy >= Chart_MA.ChartAreas(0).AxisY.Minimum And yy <= Chart_MA.ChartAreas(0).AxisY.Maximum Then
                    GetChartPointFromMousePoint = New Point(xx, yy)
                End If
            End If
        End Function
    
        Private Sub Chart_MA_PostPaint(sender As Object, e As ChartPaintEventArgs) Handles Chart_MA.PostPaint
            Dim x1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.X, MouseDownPt.X))
            Dim y1 As Single = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.Y, MouseDownPt.Y))
    
            'convert to model
            Dim point1 As PointF = New Point(x1, y1)
            point1 = e.ChartGraphics.GetAbsolutePoint(point1)
    
            x1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.X, MouseMovePt.X))
            y1 = CSng(e.ChartGraphics.GetPositionFromAxis(Chart_MA.ChartAreas(0).Name, AxisName.Y, MouseMovePt.Y))
            Dim point2 As PointF = New Point(x1, y1)
            point2 = e.ChartGraphics.GetAbsolutePoint(point2)
    
            e.ChartGraphics.Graphics.DrawLine(Pens.Black, point1, point2)
            '========================End of New Code=====================================
        End Sub
    End Class
    Public Class dbdata1
        Public My_Date As String
        Public My_Current As Single
    
        Public Sub New(d As String, c As Single)
            My_Date = d
            My_Current = c
        End Sub
    End Class
    
    #Region "Comments"
    
    '  Chart_MA.ChartAreas(0).AxisY.StripLines.Add(New StripLine)
    '        Chart_MA.ChartAreas(0).BackColor = Color.FromArgb(10, 252, 180, 65)
    '        Chart_MA.ChartAreas(0).AxisY.StripLines(0).StripWidth = 40
    '        Chart_MA.ChartAreas(0).AxisY.StripLines(0).Interval = 1000
    '        Chart_MA.ChartAreas(0).AxisY.StripLines(0).IntervalOffset = 20
    
    '        Chart_MA.ChartAreas(0).CursorY.Position = 3
    '        Chart_MA.ChartAreas(0).CursorY.LineWidth = 3
    '        Chart_MA.ChartAreas(0).CursorY.LineColor = Color.Black
    
    
    #End Region
    


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 6:13 PM
  • soro,

    It appears you are setting the axis with your max min functions so you don't need the code from my example that sets it:

        Chart_MA.ChartAreas(0).AxisX.Minimum = 0
            Chart_MA.ChartAreas(0).AxisX.Maximum = 50
            Chart_MA.ChartAreas(0).AxisY.Minimum = 0
            Chart_MA.ChartAreas(0).AxisY.Maximum = 500
            Chart_MA.ChartAreas(0).AxisY.Interval = 100
            Chart_MA.ChartAreas(0).AxisX.Interval = 10



    PS Note the update I made to the mouse event code in my example that simplifies and sets the mousemovept in mousedown so you don't get a ghost point at zero when mouse down.
    • Edited by tommytwotrain Thursday, April 20, 2017 8:17 PM
    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 7:52 PM
  • I commented the code for min and max but 10 Day MA & 30 Day MA Series still don't display.

    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 8:28 PM
  • I commented the code for min and max but 10 Day MA & 30 Day MA Series still don't display.

    Charlie Soroka

    Your example requires the data right? So I cant run it and I am not sure what it does.

    However, the code to do this line has nothing to do with the rest of the chart drawing code.

    So you should be able to insert just the mouse and paint event codes without changing the rest of your chart code.

    Try it again I guess. Do not global search and replace etc. You only add the event code with the correct names of your chart. Watch the series names and use of series(0) and series(1).

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 9:06 PM
  • My application does not require data, data is in array: Dim k As New List(Of dbdata1)() From {
         New dbdata1("3/15/2017", 8.37F),
         New dbdata1("3/16/2017", 8.25F),
         New dbdata1("3/17/2017", 8.32F),
         New dbdata1("3/20/2017", 8.27F),
         New dbdata1("3/21/2017", 7.41F),
         New dbdata1("3/22/2017", 7.62F),
         New dbdata1("3/23/2017", 7.53F),
         New dbdata1("3/24/2017", 7.34F),
         New dbdata1("3/27/2017", 6.81F),
         New dbdata1("3/28/2017", 7.34F),
         New dbdata1("3/29/2017", 7.2F),
         New dbdata1("3/30/2017", 7.23F),
         New dbdata1("3/31/2017", 7.28F),
         New dbdata1("4/03/2017", 7.06F),
         New dbdata1("4/04/2017", 7.19F),
         New dbdata1("4/05/2017", 6.83F),
         New dbdata1("4/06/2017", 7.09F),
         New dbdata1("4/07/2017", 7.65F),
         New dbdata1("4/10/2017", 7.23F),
         New dbdata1("4/11/2017", 6.95F)
                                                  }

    Charlie


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Thursday, April 20, 2017 10:11 PM
  • soro,

    in form load you call it this:

          Chart_MA.Series.Add("Series3")


    but in chart_it you call it this:

           .Series("10 Day MA").BorderWidth = 4

    You should use Option Strict On to have Visual Studio point out more simple errors although this is not one.

    PS In chart it you have

          Chart_MA.Series(1).Points.AddXY(k(i).My_Date, Answer)

    but you have only defined one series in form load. So it should be  Chart_MA.Series(0). or you need to define the second series.

    It appears you removed the other series you say are not plotting and then forgot?

    I cant get your example to run because there are too many problems like this and I don't know exactly what you are doing so all I can do is rewrite it which I did already for my example.

    As I say, all you need to do is insert the mouse event code from my example and change the names. You don't need to modify the chart code you have.


    • Edited by tommytwotrain Friday, April 21, 2017 12:08 PM
    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Friday, April 21, 2017 11:38 AM
  • Thank you for your reply.  The statement Chart_MA_Series.ADD("Series3") was for your code.  There are three series the "10 Day MA" & "30 Day MA" that was added in Chart properties for my code, sorry about that. The 3rd series was "Series3" for your code. You can turn your code on or off by the Load statement "Load_it()", my chart code is turned on or off by "Chart_it"

    I commented your code "Load_it" and chart code runs normal. I am sorry I did not explain the 2 series that was not added in code. I hope I have explained it correctly.

    Charlie


    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Friday, April 21, 2017 1:56 PM
  • Thank you for your reply.  The statement Chart_MA_Series.ADD("Series3") was for your code.  There are three series the "10 Day MA" & "30 Day MA" that was added in Chart properties for my code, sorry about that. The 3rd series was "Series3" for your code. You can turn your code on or off by the Load statement "Load_it()", my chart code is turned on or off by "Chart_it"

    I commented your code "Load_it" and chart code runs normal. I am sorry I did not explain the 2 series that was not added in code. I hope I have explained it correctly.

    Charlie


    Charlie Soroka

    Just insert the code I gave you from the mouse events into your chart code and change the chart names and series names in the mouse event code you add to whatever you are using now. You don't need to modify any of your existing code.

    That's all I am going to do for you. My example runs. Yours does not.

    I showed you how to fish ...

    :)

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:17 AM
    Friday, April 21, 2017 2:18 PM
  • Thank you for your reply, I was able to get your solution to work. 

    Charlie Soroka

    • Marked as answer by sorokateam Tuesday, April 25, 2017 1:16 AM
    Tuesday, April 25, 2017 1:16 AM
  • Thank you for your reply, I was able to get your solution to work. 

    Charlie Soroka

    Oh that's good. :)

    Thanks for letting us know. I was wondering but the forum has been down...

    Tuesday, April 25, 2017 12:22 PM