Results 1 to 11 of 11

Thread: [RESOLVED] MSChart Zoom with mouse roller?

  1. #1

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Resolved [RESOLVED] MSChart Zoom with mouse roller?

    I have the standard zoom function with the mouse down, click and drag to zoom in that you can enable in the main chart settings. I was wondering, is there an easy way of adding in a function to where the chart zooms in by a certain percentage where the mouse is when the roller is activated?

    I researched it a while back, and couldn't get it to work so I abandoned it. Thought I'd bring the question here to see what you all thought

    Thanks!

    Frab
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: MSChart Zoom with mouse roller?

    If you already have the zoom working then what you'll need to do is generate the MouseWheel event for your control and then check if the Delta property greater than or less than 0:
    Code:
    Private Sub Chart1_MouseWheel(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseWheel
        If e.Delta > 0 Then
            'Zoom up
        Else
            'Zoom down
        End If
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: MSChart Zoom with mouse roller?

    Quote Originally Posted by dday9 View Post
    If you already have the zoom working then what you'll need to do is generate the MouseWheel event for your control and then check if the Delta property greater than or less than 0:
    Code:
    Private Sub Chart1_MouseWheel(ByVal sender As Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseWheel
        If e.Delta > 0 Then
            'Zoom up
        Else
            'Zoom down
        End If
    End Sub

    Thank you for the reply

    I have already gotten that far, however my zoom function I didnt code. It was the default zoom that mscharts automatically comes with.
    I have been trying to break it down and see (by trial and error) if I can get it working. So far the only thing that I have came up with is the following code

    vb.net Code:
    1. Private Sub Chart1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Chart1.MouseWheel
    2.  
    3.             zoomamount = TextBox28.Text
    4.  
    5.  
    6.  
    7.         Dim x As Int32 = e.Location.X
    8.         Dim y As Int32 = e.Location.Y
    9.  
    10.         Dim chartStarty As Int32 = Chart1.Location.Y
    11.         Dim chartstartx As Int32 = Chart1.Location.X
    12.  
    13.         Dim chartsizey As Int32 = Chart1.Size.Height
    14.         Dim chartsizex As Int32 = Chart1.Size.Width
    15.  
    16.         Dim currentSIZEx As Int32 = Chart1.ChartAreas(0).AxisX.Maximum
    17.         Dim currentSIZEy As Int32 = Chart1.ChartAreas(0).AxisY.Maximum
    18.  
    19.  
    20.  
    21.         If e.Delta > 0 Then
    22.            ' Chart1.ChartAreas(0).CursorY.SelectionStart = (y / (zoomamount))
    23.            ' Chart1.ChartAreas(0).CursorX.SelectionStart = (x / zoomamount))
    24.  
    25.            ' Chart1.ChartAreas(0).CursorY.SelectionEnd = ((chartsizey - y) / (zoomamount))
    26.           '  Chart1.ChartAreas(0).CursorX.SelectionEnd = ((chartsizex - x) / (zoomamount))
    27.  
    28.             Chart1.ChartAreas(0).AxisY.Maximum = (currentSIZEy - (25))
    29.             Chart1.ChartAreas(0).AxisX.Maximum = (currentSIZEx - (25))
    30.         Else
    31.             Chart1.ChartAreas(0).AxisY.Maximum = (currentSIZEy + (25))
    32.             Chart1.ChartAreas(0).AxisX.Maximum = (currentSIZEx + (25))
    33.  
    34.         End If
    35.     End Sub

    however this just changes the max and min of the chart. Not really a zoom, and with no scroll bars.
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: MSChart Zoom with mouse roller?

    I'm sorry, I must have misunderstood. I thought you had the zoom function already worked up. I'm unsure as to how to zoom in and out on a Chart because I've never used a Chart control before, so I'll have to bow out. Good luck though!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: MSChart Zoom with mouse roller?

    Quote Originally Posted by dday9 View Post
    I'll have to bow out. Good luck though!
    No sweat, and thanks
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  6. #6

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: MSChart Zoom with mouse roller?

    as an update, i have figured out part of the code.


    vb.net Code:
    1. If e.Delta < 0 Then
    2.             Chart1.ChartAreas(0).AxisX.ScaleView.ZoomReset()
    3.             Chart1.ChartAreas(0).AxisY.ScaleView.ZoomReset()
    4.             amountofrolls = 0
    5.         Else
    6.  
    7.             Dim xMin As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMinimum
    8.             Dim xMax As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMaximum
    9.             Dim yMin As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMinimum
    10.             Dim yMax As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMaximum
    11.             Dim amm As Double = 4
    12.  
    13.          
    14.  
    15.             Dim posXStart As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / amm
    16.  
    17.             Dim posXFinish As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / amm
    18.  
    19.             Dim posYStart As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / amm
    20.  
    21.             Dim posYFinish As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / amm
    22.  
    23.  
    24.  
    25.  
    26.             Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(posXStart, posXFinish)
    27.             Chart1.ChartAreas(0).AxisY.ScaleView.Zoom(posYStart, posYFinish)
    28.         End If

    the AxisX.ScaleView.Zoom(Double, Double) is the actual zoom function itself. Now I need to figure out the right math formula to get it to work properly. Any help on this would be greatly appreciated.

    Rigth now, it kinda zooms right, but not 100%. Its a bit hard for me to explain. It zooms in, but only if the mouse is not in the center of the chart.
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  7. #7

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: MSChart Zoom with mouse roller?

    bump
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  8. #8
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: MSChart Zoom with mouse roller?

    This is not quite 100%. Not sure why, but:
    Code:
    Dim scale As Double = 2   '   or what ever
    
    Dim xMin As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMinimum
    Dim xMax As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMaximum
    Dim yMin As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMinimum
    Dim yMax As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMaximum
    
    
    Dim xValue As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.Location.X)
    Dim yValue As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Location.Y)
    
    Dim posXStart As Double = xValue - (xValue - xMin) / scale
    Dim posXFinish As Double = posXStart + (xMax - xMin) / scale
    
    Dim posYStart As Double = yValue - (yValue - yMin) / scale
    Dim posYFinish As Double = posYStart + (yMax - yMin) / scale
    
    
    Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(posXStart, posXFinish)
    Chart1.ChartAreas(0).AxisY.ScaleView.Zoom(posYStart, posYFinish)

  9. #9

    Thread Starter
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Location
    USA
    Posts
    393

    Re: MSChart Zoom with mouse roller?

    Quote Originally Posted by Inferrd View Post
    This is not quite 100%. Not sure why
    Thank you so much!
    You have put me on the right track. I was able to modify your code to get a close to perfect zoom in function. What I changed was made an area that said if the mouse was within a percentage of the zoom area then it would not zoom in on it. That way, if you are close to the edge you will stay on the edge.

    vb.net Code:
    1. If e.Delta < 0 Then
    2.             Chart1.ChartAreas(0).AxisX.ScaleView.ZoomReset()
    3.             Chart1.ChartAreas(0).AxisY.ScaleView.ZoomReset()
    4.             amountofrolls = 0
    5.         Else
    6.             Dim scale As Double = 1.25   '   or what ever
    7.  
    8.             Dim xMin As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMinimum
    9.             Dim xMax As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMaximum ' + Chart1.ChartAreas(0).AxisX.ScaleView.Position
    10.             Dim yMin As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMinimum
    11.             Dim yMax As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMaximum ' + Chart1.ChartAreas(0).AxisX.ScaleView.Position
    12.  
    13.  
    14.             Dim xValue As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.Location.X)
    15.             Dim yValue As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Location.Y)
    16.  
    17.  
    18.             Dim posXFinish As Double
    19.             Dim posXStart As Double
    20.  
    21.  
    22.  
    23.             'this code checks to see if you are within a certain percentage of the starting point
    24.             ' and if you are it will not zoom in
    25.             '
    26.             'check zoom  for x Start
    27.             If (xValue - (xValue - xMin) / scale) >= xValue Then
    28.                 posXStart = xValue - (xValue - xMin)
    29.             Else
    30.                 posXStart = xValue - (xValue - xMin) / scale
    31.             End If
    32.  
    33.             '
    34.             'check zoom  for x finish
    35.             If xValue >= (posXStart + (xMax - xMin) / scale) Then
    36.                 posXFinish = posXStart + (xMax - xMin)
    37.             Else
    38.                 posXFinish = posXStart + (xMax - xMin) / scale
    39.             End If
    40.  
    41.  
    42.  
    43.             Dim posYFinish As Double
    44.             Dim posYStart As Double
    45.  
    46.  
    47.             '
    48.             'check zoom for Y start
    49.             If (yValue - (yValue - yMin) / scale) >= yValue Then
    50.                 posYStart = yValue - (yValue - yMin)
    51.             Else
    52.                 posYStart = yValue - (yValue - yMin) / scale
    53.             End If
    54.  
    55.  
    56.             '
    57.             'check zoom for Y finish
    58.             If yValue >= (posYStart + (yMax - yMin) / scale) Then
    59.                 posYFinish = posYStart + (yMax - yMin)
    60.             Else
    61.                 posYFinish = posYStart + (yMax - yMin) / scale
    62.             End If
    63.  
    64.  
    65.  
    66.             Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(posXStart, posXFinish)
    67.             Chart1.ChartAreas(0).AxisY.ScaleView.Zoom(posYStart, posYFinish)
    68.  
    69.  
    70.  
    71.  
    72.  
    73.  
    74.         End If

    Now I'll get cracking on the zoom out
    Thanks!
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

    Useful Things:

    How to link your VB.Net application to Excel

  10. #10
    New Member
    Join Date
    Jun 2018
    Posts
    1

    Re: [RESOLVED] MSChart Zoom with mouse roller?

    Now this is Working very fine with me zooming in & out Thanks guys you inspired me...
    شكرا لكم رفاقي ,هذا الكود لكم ولمن يريد أن يستخدمه صدقة لوجه الله

    Code:
     Private Sub Chart1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Chart1.MouseWheel
            Try
                Dim ZoomFactor As Double = 0.25   '0 to 1 Represent 0% to 100% Every Wheel Tick.
    
                Dim Current_xMin As Integer = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMinimum
                Dim Current_xMax As Integer = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMaximum
                Dim Current_yMin As Integer = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMinimum
                Dim Current_yMax As Integer = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMaximum
    
                Dim PointerPosOnChart_xAxis As Integer = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.Location.X)
                Dim PointerPosOnChart_yAxis As Integer = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Location.Y)
    
                Dim New_xMin As Integer
                Dim New_xMax As Integer
                Dim New_yMin As Integer
                Dim New_yMax As Integer
    
                If Current_xMin <= PointerPosOnChart_xAxis And PointerPosOnChart_xAxis <= Current_xMax And
                    Current_yMin <= PointerPosOnChart_yAxis And PointerPosOnChart_yAxis <= Current_yMax Then
    
                    If e.Delta > 0 Then 'Zoom in While Moving The Mouse Wheel Forward (Up)
    
                        New_xMin = Current_xMin + ((PointerPosOnChart_xAxis - Current_xMin) * ZoomFactor)
                        New_xMax = Current_xMax - ((Current_xMax - PointerPosOnChart_xAxis) * ZoomFactor)
                        New_yMin = Current_yMin + ((PointerPosOnChart_yAxis - Current_yMin) * ZoomFactor)
                        New_yMax = Current_yMax - ((Current_yMax - PointerPosOnChart_yAxis) * ZoomFactor)
    
                    ElseIf e.Delta < 0 Then 'Zoom out While Moving The Mouse Wheel Backward (Down)
    
                        New_xMin = Current_xMin - ((PointerPosOnChart_xAxis - Current_xMin) * ZoomFactor)
                        New_xMax = Current_xMax + ((Current_xMax - PointerPosOnChart_xAxis) * ZoomFactor)
                        New_yMin = Current_yMin - ((PointerPosOnChart_yAxis - Current_yMin) * ZoomFactor)
                        New_yMax = Current_yMax + ((Current_yMax - PointerPosOnChart_yAxis) * ZoomFactor)
    
                    End If
    
                    Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(New_xMin, New_xMax)
                    Chart1.ChartAreas(0).AxisY.ScaleView.Zoom(New_yMin, New_yMax)
    
                End If
            Catch ex As System.Exception
                MsgBox(ex.Message)
            End Try
        End Sub

  11. #11
    New Member
    Join Date
    Jun 2015
    Posts
    7

    Re: [RESOLVED] MSChart Zoom with mouse roller?

    I have further improved this code by making zoom out work as well as zoom in.

    Code:
     Private Sub Chart1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Chart1.MouseWheel
        Try
    
            With Chart1
    
                Dim xMin As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMinimum
                Dim xMax As Double = Chart1.ChartAreas(0).AxisX.ScaleView.ViewMaximum ' + Chart1.ChartAreas(0).AxisX.ScaleView.Position
                Dim yMin As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMinimum
                Dim yMax As Double = Chart1.ChartAreas(0).AxisY.ScaleView.ViewMaximum ' + Chart1.ChartAreas(0).AxisX.ScaleView.Position
    
                Dim xValue As Double = Chart1.ChartAreas(0).AxisX.PixelPositionToValue(e.Location.X)
                Dim yValue As Double = Chart1.ChartAreas(0).AxisY.PixelPositionToValue(e.Location.Y)
    
                Dim posXFinish As Double
                Dim posXStart As Double
    
                Dim posYFinish As Double
                Dim posYStart As Double
    
    
    
                'this code checks to see if you are within a certain percentage of the starting point
                ' and if you are it will not zoom in
    
                If e.Delta > 0 Then
                    'check zoom  for x Start
                    If (xValue - (xValue - xMin) / scale) >= xValue Then
                        posXStart = xValue - (xValue - xMin)
                    Else
                        posXStart = xValue - (xValue - xMin) / scale
                    End If
    
                    'check zoom  for x finish
                    If xValue >= (posXStart + (xMax - xMin) / scale) Then
                        posXFinish = posXStart + (xMax - xMin)
                    Else
                        posXFinish = posXStart + (xMax - xMin) / scale
                    End If
    
                    'check zoom for Y start
                    If (yValue - (yValue - yMin) / scale) >= yValue Then
                        posYStart = yValue - (yValue - yMin)
                    Else
                        posYStart = yValue - (yValue - yMin) / scale
                    End If
    
                    'check zoom for Y finish
                    If yValue >= (posYStart + (yMax - yMin) / scale) Then
                        posYFinish = posYStart + (yMax - yMin)
                    Else
                        posYFinish = posYStart + (yMax - yMin) / scale
                    End If
                ElseIf e.Delta < 0 Then
                    'check zoom  for x Start
                    If (xValue - (xValue - xMin) * scale) <= xValue Then
                        posXStart = xValue - (xValue - xMin)
                    Else
                        posXStart = xValue - (xValue - xMin) * scale
                    End If
    
                    'check zoom  for x finish
                    If xValue <= (posXStart + (xMax - xMin) * scale) Then
                        posXFinish = posXStart + (xMax - xMin)
                    Else
                        posXFinish = posXStart + (xMax - xMin) * scale
                    End If
    
                    'check zoom for Y start
                    If (yValue - (yValue - yMin) * scale) >= yValue Then
                        posYStart = yValue - (yValue - yMin)
                    Else
                        posYStart = yValue - (yValue - yMin) * scale
                    End If
    
                    'check zoom for Y finish
                    If yValue >= (posYStart + (yMax - yMin) * scale) Then
                        posYFinish = posYStart + (yMax - yMin)
                    Else
                        posYFinish = posYStart + (yMax - yMin) * scale
                    End If
    
                End If
    
                Chart1.ChartAreas(0).AxisX.ScaleView.Zoom(posXStart, posXFinish)
                Chart1.ChartAreas(0).AxisY.ScaleView.Zoom(posYStart, posYFinish)
    
    
            End With
    
    
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width