Results 1 to 7 of 7

Thread: Create x,y points from mouse clicks

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,970
    Excel's probably not the ideal tool for this but it's possible.
    As you asked, this will only suggest a way of getting started - and don't take it as authoritative.

    In the attached I've set up an x,y scatter chart on its own chart sheet (this responds to events by default, an embedded chart in a stasndard worksheet doesn't but can be made to).
    There's an event handler for mouse down (left-click) Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long) which carries x and y values which are coordinates. Exactly what those coordinates refer to I'm not 100% sure, however the origin seems to be top left of the chart, so not the traditional DesCartes (cartesian) coordinate system. The ranges of returned values for x and y change according to the size of the chart on the screen, so it's a bit of a minefield.
    I set up fixed chart x and y axis limits (0 to 100 for both), then in the event handler did a bit of arithmetic on the returned x and y values to try to bring them more into line with traditional x,y coordinatres (origin at bottom left) and to try to bring them within 0 to 100. The code adds a new series as a single point (it could have been a single point within an existing series). This works when the chart occupies about a third of my screen on the left.
    If you click in the vicinity of the top left of the chart you're most likely to see the data point being plotted on the chart.

    If you do an internet search for translating the x and y values returned in the event handler to useable chart coordinate I'm sure you'll find something useful, which will handle varying window sizes/screen resolutions.

    If you have the attached open in a smallish window it might get somewhere close to behaving as you want.

    The code in the attached is rudimentary:
    Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
    'Debug.Print x, y
    With Me.SeriesCollection.NewSeries
      .XValues = x / 6.2
      .Values = (y * -1 + 400) / 3.6
    End With
    End Sub
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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