DPlot Forum Index DPlot
http://www.dplot.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Getting X Positions through the MouseMove Event

 
Post new topic   Reply to topic    DPlot Forum Index -> Basic
View previous topic :: View next topic  
Author Message
xander_P



Joined: 11 Sep 2006
Posts: 10

PostPosted: Tue Sep 12, 2006 12:34 pm    Post subject: Getting X Positions through the MouseMove Event Reply with quote

I am currently using VB6 to create a program to plot data using the DPlotLib. I am attempting to "zoom in" on a graph when the user clicks on a certain section of the graph, and therefore need to know the X value clicked on. I copied the code used in the BTEST2.vbp project, and I am getting interesting and unexpected results.

The problem appears to be related to the range of points I choose to display. Unlike BTEST2.vbp's X range of 1 to 4, my plots are from 350 to 900, with Y values as large as 26000. I am also using a DataFormat = DATA_XYXY, instead of BTEST2's DATA_XYYY. I am still using linear scaling.

When I am on the tick mark for 450, the mousemove displays 477, 500=>522, and by the time it gets to 750=>752 it is almost correct. I experimented and increased my range to 350-1050, and it results in 450=>485 ... 850=>853, even worse. With a range of 350-360 everything was fine.

So it appears the offset I am getting is some function of the data range I am displaying. The magic guts of the MouseMove event appears to be the following code:
Code:
worldx = DPM.xlo + (X - DPM.hll) * (DPM.xhi - DPM.xlo) / (DPM.hur - DPM.hll)
worldy = DPM.ylo + (Y - DPM.vll) * (DPM.yhi - DPM.ylo) / (DPM.vur - DPM.vll)
           

The vll/hll/vur/hur values are in pixels. Is there anything special about those pixel values in relation to the picturebox I should account for? Am I missing something obvious?

In the meantime I am going to derive the equation for the offset I am seeing for the data ranges I plan on using, maybe that will shed some light on the situation.

thanks,
xander
Back to top
View user's profile Send private message
DPlotAdmin
Site Admin


Joined: 24 Jun 2003
Posts: 2310
Location: Vicksburg, Mississippi

PostPosted: Tue Sep 12, 2006 10:11 pm    Post subject: Reply with quote

I tried duplicating this just now by modifying the data produced by the btest2 example, but it all works out fine for me. With the cursor at 450, the display is 450.355. At 500 I see 499.882. I thought maybe there might be a problem with using (or not) ManualScale, but I get the same results either way.

The vll/hll/vur/hur pixel values are at the corners of the box surrounding the plot. So, for example, if your plot extents are (350,0) to (900,26000), vll, hll corresponds to (350,0).

One thing that does occur to me that might explain the problem: In the btest2 demo the requested bitmap is the size of the picturebox:

Code:

    ret = GetClientRect(Picture1.hwnd, rcPic)
    .
    .
    hBitmap = DPlot_GetBitmapEx(DocNum, rcPic.Right - rcPic.Left, rcPic.Bottom - rcPic.Top, DPM)


and the Picture1_Paint routine draws the bitmap with the upper left corner of the bitmap in the upper left corner of the picturebox. So mouse coordinates correspond to bitmap pixels; e.g. mouse position 0,0 is the upper left corner of the bitmap. If you're doing anything differently, that's fine as long as you take into account any offset between the bitmap and the picturebox. Let me know if you have any questions.
_________________
Visualize Your Data
support@dplot.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
xander_P



Joined: 11 Sep 2006
Posts: 10

PostPosted: Wed Sep 13, 2006 10:24 am    Post subject: Reply with quote

I've added a picture to show a sample graph from my program. It appears that the DPM object wasn't returning the pixel locations I was expecting. I've tagged the picture below to further explain my problem.

I was expecting the (hll,vll) point to be the at the crosshairs of the xy axis, but it shows up off the plot for my graph. When I modify the code to use (hll/vll) = (107,445) and (hur/vur) = (785,72) instead of the values being returned from the DPM object, everything works.

I am kind of at a loss as to why the DPM object is returning the points it is, other than it kinda looks like for my graph it is taking in account the axis labels. I am placing the graph starting at (0,0) in my picturebox, so I don't know where else that offset could orginate.

Anyway the problem for me is bypassed now, as I don't plan on having any change in axis labels, ranges, or picturebox sizes. So I can use my hardcoded values, and worst case can redetermine the values if something changes. If you think it might be a bug, and you would like to know more about the commands I sent to create the plot I can provide them to you, just let me know.

[/img]
Back to top
View user's profile Send private message
DPlotAdmin
Site Admin


Joined: 24 Jun 2003
Posts: 2310
Location: Vicksburg, Mississippi

PostPosted: Wed Sep 13, 2006 10:51 am    Post subject: Reply with quote

I don't have VB6 handy at the moment, but one other thing you might check: what is the "ScaleMode" setting for the picturebox? It should be "pixels".

I'll take another look at this late tonight and see what else I can come up with. You're welcome to send me your source files if you like.
_________________
Visualize Your Data
support@dplot.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
xander_P



Joined: 11 Sep 2006
Posts: 10

PostPosted: Wed Sep 13, 2006 11:34 am    Post subject: Reply with quote

My scaleMode is "3 - Pixel", I messed around with some of the other options for scaleMode, and none of them changed anything for the better. Instead of sending the source code, which is rather large, I will just copy the section you would need to reproduce the plot.

And like I said, I've gotten it to work through my "hack", so there is no need to waste too much time, when it more likely is just something I am doing wrong. But I appreciate the efforts you have taken already, and any further as well.

Code:

d.version = DPLOT_DDE_VERSION
    d.hwnd = vForm.hwnd
    d.DataFormat = DATA_XYXY
    d.MaxCurves = 1         ' Must be >= number of curves we plot
    d.MaxPoints = NP        ' Anything >= NP will do
    d.NumCurves = 1
    d.ScaleCode = SCALE_LINEARX_LINEARY
    d.NP(0) = NP
    d.LineType(0) = LINESTYLE_SOLID
    d.Title1 = "test"
    d.title2 = cstr(now)
    d.title3 = ""
    d.XAxis = "wavelength (nm)" & Chr$(0)
    d.YAxis = "Intensity" & Chr$(0)
       
    cmds = "[ManualScale(350,0,1050,21219)]"
    cmds = cmds & "[TickInterval(0,0,0)]"
    cmds = cmds & "[Color(-1,255,0,0)]"
    cmds = cmds & "[LineWidth(-2,20)]"  'curve width
    cmds = cmds & "[LineWidth(-1,20)]"  'coord axes width
    cmds = cmds & "[LineWidth(0,10)]"   'grid line width
   '[TextFont(Index,Points,Weight,Italic,R,G,B,"Face")]
    cmds = cmds & "[TextFont(1, 8,  0,0,0,0,0,""Arial"")]"  'numbers along the axes
    cmds = cmds & "[TextFont(2,8,700,0,0,0,0,""Arial"")]"  'first title line
    cmds = cmds & "[TextFont(3,8,700,0,0,0,0,""Arial"")]"  '2nd and 3rd title line
    cmds = cmds & "[TextFont(4,10,  0,0,0,0,0,""Arial"")]"  'X Axis Label
    cmds = cmds & "[TextFont(5,10,  0,0,0,0,0,""Arial"")]"  'Y Axis Label
    cmds = cmds & "[TextFont(6, 8,  0,0,0,0,0,""Arial"")]"  'Legend
    cmds = cmds & "[TextFont(7, 8,  0,0,0,0,0,""Arial"")]"  'Notes
    cmds = cmds & "[TextFont(8, 8,  0,0,0,0,0,""Arial"")]"  'Z Axis Label
    cmds = cmds & "[GridLines(2)]"
    cmds = cmds & "[ClearEditFlag()]"

    If (hemf <> 0) Then
        ret = DeleteEnhMetaFile(hemf)
        hemf = 0
    End If
    If (hBitmap <> 0) Then
        ret = DeleteObject(hBitmap)
        hBitmap = 0
    End If
   
    DocNum = DPlot_Plot(d, X(0), Y(0), cmds)

    If (DocNum > 0) Then
        hemf = DPlot_GetEnhMetaFile(DocNum, SizeX, SizeY)
        DPM.SIZE = Len(DPM)
        hBitmap = DPlot_GetBitmapEx(DocNum, rcPic.Right - rcPic.Left, rcPic.Bottom - rcPic.Top, DPM)
        ret = DPlot_Command(DocNum, "[FileClose()]")
    End If
    Call Graph_Paint(vPictureBox)
Back to top
View user's profile Send private message
DPlotAdmin
Site Admin


Joined: 24 Jun 2003
Posts: 2310
Location: Vicksburg, Mississippi

PostPosted: Wed Sep 13, 2006 1:07 pm    Post subject: Reply with quote

There's nothing there that would cause a problem. I don't see that you're getting/setting rcPic in there, but even if that rectangle is uninitialized (so that DPlot returns a bitmap the same size as the default document window), the scaling would still be correct, though the bitmap may be too large to fit the picturebox. That's assuming you're using BitBlt in your Graph_Paint routine (as is done in the btest2 demo) to draw the bitmap rather than StretchBlt or some built-in VB gizmo. If you are using StretchBlt... well, stop it Smile

The only other thing I can think of is to check your ScaleLeft and ScaleTop values for the picturebox. Those should both be 0.
_________________
Visualize Your Data
support@dplot.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
xander_P



Joined: 11 Sep 2006
Posts: 10

PostPosted: Wed Sep 13, 2006 1:56 pm    Post subject: Reply with quote

Well that was indirectly it. My code contains a mixture of BTEST2 and BTEST4. I was using the BTEST4 graph_paint routine to display the plot, when I should have been using the BTEST2 graph_paint routine. Its interesting that the 2 routines display the plot differently.

So I changed my paint routine to BTEST2 and the DPM is correct. Thats what I get for trying to piece-mail my code instead of fully understanding what is going on. Thanks for your time.

BTEST4
Code:
Private Sub Graph_Paint(ByRef vPictBox As PictureBox)
    Dim hdc As Long
    Dim ret As Long
    Dim rcPic As RECT
   
    If hemf <> 0 Then
        vPictBox.Cls
        hdc = GetDC(vPictBox.hwnd)
        ret = GetClientRect(vPictBox.hwnd, rcPic)
        ret = PlayEnhMetaFile(hdc, hemf, rcPic)
        ret = ReleaseDC(vPictBox.hwnd, hdc)
    End If
End Sub



PLOT using BTEST2's graph_paint
Back to top
View user's profile Send private message
DPlotAdmin
Site Admin


Joined: 24 Jun 2003
Posts: 2310
Location: Vicksburg, Mississippi

PostPosted: Wed Sep 13, 2006 3:05 pm    Post subject: Reply with quote

Quote:
Its interesting that the 2 routines display the plot differently.


Apples and... well, not oranges. Apples and bicycles. Making a metafile work in this way will be difficult, mostly because PlayEnhMetafile will stretch or compress the metafile as needed to fill the specified rectangle.

If you read the note in the Form_Load event of the btest4 demo it will give a clue about what's going on here:

Code:
    ' Size of the metafile to retrieve, in inches. The actual size specified isn't critical
    ' *unless* we want font sizes in our output to exactly match what we specify. What IS
    ' fairly important is that the aspect ratio matches the aspect ratio of the output -
    ' otherwise squares won't be square, fonts will be stretched.
    SizeX = 5#
    SizeY = (SizeX * (rcPic.Bottom - rcPic.Top + 1)) / (rcPic.Right - rcPic.Left + 1)


Unless your output rectangle is exactly 5 inches wide (actually I think it's, in Windows jargon, logical inches, not inches - but that's another subject), the picture will be stretched or compressed. After that there's really no way that scaling will work.
_________________
Visualize Your Data
support@dplot.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    DPlot Forum Index -> Basic All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group