A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » Charts and Charting
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Get mouse pointer position in chart coordinates



 
 
Thread Tools Display Modes
  #1  
Old March 13th, 2004, 11:30 PM
Jon Peltier
external usenet poster
 
Posts: n/a
Default Get mouse pointer position in chart coordinates

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
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
http://PeltierTech.com/Excel/Charts/
_______

Allen wrote:

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.


  #2  
Old March 18th, 2004, 02:24 PM
Jon Peltier
external usenet poster
 
Posts: n/a
Default Get mouse pointer position in chart coordinates

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
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
http://PeltierTech.com/Excel/Charts/
_______


LeninVMS wrote:

Allen -

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

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

1. Get x,y from Mousemove event
2. Get Chart's absolute coordinates using the following properties of
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 the
.visiblerange property

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

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

If (TopLeftCell.LeftChartobject.Left and
TopLeftCell.TopChartobject.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 your
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 the
top of the top left cell.

end if

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

i.e if chartobject.height=400, then your bottomright mouse position
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
----Lenin


---
Message posted from http://www.ExcelForum.com/


  #3  
Old March 19th, 2004, 02:43 PM
Jon Peltier
external usenet poster
 
Posts: n/a
Default Get mouse pointer position in chart coordinates

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
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
http://PeltierTech.com/Excel/Charts/
_______

Allen wrote:

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
in relation to your chart scale:

1. Get x,y from Mousemove event 2. Get Chart's absolute coordinates
using the following properties of 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 the
.visiblerange property




 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:40 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.