Get mouse pointer position in chart coordinates

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

want to find the cursor position within a chart in chart coordinate system in my Chart_MouseMove procedure. In oder to do the coordinate system transformation, I need a way to get the ".Left Property" of the Chart, ChartArea, or Axes(xlCategory) object relative to the active chart's window. Any suggestions

Thanks.
 
Allen -

Why do you need the XY coordinates? Often there are easier ways to get
what you want.

To get some relevant dimensions in the chart, check out the .left and
..top properties of the ChartArea object, and the .insideleft and
..insidetop properties of the PlotArea object.

In fact, if you google the archives for my name and
"PlotArea.InsideLeft" you might find something useful
(http://www.google.com/advanced_group_search).

- Jon
 
Hi Jon

There were originally two reasons for getting the XY coordinates in MouseMove Procedure

1. To probing or digitizing the chart area: you can tell the value without an existing data point or doing any visual interpolation between tick marks on axe
2. To be able to select a set of data points by drawing a ploygon around them: you want to use only those selected data points for regression analysis late

Either of above actions requires the XY coordinates in a chart's own coordinates defined by MinimumScale and MaximumScale properties. Since the values of X and Y set by MouseMove are distance in points from current mouse pointer to the edges of the active window's client area, which are also affected by zoom factor and scrollbar actions of the window, I do need to know some kind of properties that can relate coordinate systems between active window and active chart or it's child items. The .Left of Chart, CharArea, PlotArea, and Axes are all measurements relative to a Chart object (PaperSize - Margin). They don't help

Correct me if I am wrong. Thanks again


----- Jon Peltier wrote: ----

Allen

Why do you need the XY coordinates? Often there are easier ways to get
what you want

To get some relevant dimensions in the chart, check out the .left and
..top properties of the ChartArea object, and the .insideleft and
..insidetop properties of the PlotArea object

In fact, if you google the archives for my name and
"PlotArea.InsideLeft" you might find something useful
(http://www.google.com/advanced_group_search)

- Jo
------
Jon Peltier, Microsoft Excel MV
Peltier Technical Service
http://PeltierTech.com/Excel/Charts
______

Allen wrote
 
Allen -

Mousemove event indeed gives the mouse position from the top-left o
the window's visible client area.

Here is a skeleton outline of how you can calculate the x,y position i
relation to your chart scale:

1. Get x,y from Mousemove event
2. Get Chart's absolute coordinates using the following properties o
the chartobject parent of your chart

With Me.parent (in class module)
or
With Activesheet.chartobjects(i)

.left
.top
.height
.width

3. Get the coordinates of the top-left visible cell using th
.visiblerange property

activewindow.visiblerange.cells(1,1).left
.top

4. Compare the left and top properties of your chartobject and the to
left cell to find how much of your chart is hidden.

If (TopLeftCell.Left<Chartobject.Left an
TopLeftCell.Top<Chartobject.Top) then

'No adjustment required based on chartposition here
x=x/activewindow.zoom*75
y=y/activewindow.zoom*75

'The 75 is an adjustment factor - Only at this zoom level will you
mouse pointer readings coincide with the chart measurements.

else

x=x/activewindow.zoom*75+adjustmentLeft
y=y/activewindow.zoom*75+adjustmentTop

'adjustments depend on how much of your chart is to the left or to th
top of the top left cell.

end if

5. Note that there is a slight problem you might encounter in th
adjustments. Excel loses 0.75 points for the chartobject.height fo
every 40 points of chartsize

i.e if chartobject.height=400, then your bottomright mouse positio
will only be 400-7.5=392.5 at a zoom of 75.

however, nothing is lost for chartobject.width.
Would be glad if someone can explain this.

Hope this helps.

Regards
----Leni
 
Lenin -

That's some good information. I hadn't known about the gradual loss in
chartobject.height, and I never worked in the zoom factor, since I
always work at 100%.
Would be glad if someone can explain this.

"Excel is like that sometimes."

- Jon
 
Lenin

My charct is located on a separate chart sheet, so it is a Chart object, not an embedded ChartObject object. The Chart itself does not have .left property, neither does its parent (Workbook). And the chart's window does not support properties similar to VisibleRange. That's where I got stuck. :-


----- LeninVMS > wrote: ----

Here is a skeleton outline of how you can calculate the x,y position i
relation to your chart scale

1. Get x,y from Mousemove even
2. Get Chart's absolute coordinates using the following properties o
the chartobject parent of your char

With Me.parent (in class module
o
With Activesheet.chartobjects(i

.lef
.to
.heigh
.widt

3. Get the coordinates of the top-left visible cell using th
.visiblerange propert
 
Allen -

Use the mousemove XY and compare to the plotarea coordinates. Here's a
little snippet for a chart sheet:

Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, _
ByVal X As Long, ByVal Y As Long)
Dim PlotArea_InsideLeft As Double
Dim PlotArea_InsideTop As Double
Dim PlotArea_InsideWidth As Double
Dim PlotArea_InsideHeight As Double
Dim AxisCategory_MinimumScale As Double
Dim AxisCategory_MaximumScale As Double
Dim AxisCategory_Reverse As Boolean
Dim AxisValue_MinimumScale As Double
Dim AxisValue_MaximumScale As Double
Dim AxisValue_Reverse As Boolean
Dim datatemp As Double
Dim Xcoordinate As Double
Dim Ycoordinate As Double
Dim X1 As Double
Dim Y1 As Double

X1 = X * 75 / ActiveWindow.Zoom
Y1 = Y * 75 / ActiveWindow.Zoom

PlotArea_InsideLeft = PlotArea.InsideLeft + ChartArea.Left
PlotArea_InsideTop = PlotArea.InsideTop + ChartArea.Top
PlotArea_InsideWidth = PlotArea.InsideWidth
PlotArea_InsideHeight = PlotArea.InsideHeight

With Axes(xlCategory)
AxisCategory_MinimumScale = .MinimumScale
AxisCategory_MaximumScale = .MaximumScale
AxisCategory_Reverse = .ReversePlotOrder
End With
With Axes(xlValue)
AxisValue_MinimumScale = .MinimumScale
AxisValue_MaximumScale = .MaximumScale
AxisValue_Reverse = .ReversePlotOrder
End With

datatemp = (X1 - PlotArea_InsideLeft) / PlotArea_InsideWidth * _
(AxisCategory_MaximumScale - AxisCategory_MinimumScale)
Xcoordinate = IIf(AxisCategory_Reverse, _
AxisCategory_MaximumScale - datatemp, _
datatemp + AxisCategory_MinimumScale)

datatemp = (Y1 - PlotArea_InsideTop) / PlotArea_InsideHeight * _
(AxisValue_MaximumScale - AxisValue_MinimumScale)
Ycoordinate = IIf(AxisValue_Reverse, _
datatemp + AxisValue_MinimumScale, _
AxisValue_MaximumScale - datatemp)

MsgBox "X = " & Xcoordinate & vbCrLf & "Y = " & Ycoordinate
End Sub

- Jon
 
Back
Top