Skip to content

Instantly share code, notes, and snippets.

@alex-hhh
Created February 6, 2018 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-hhh/2394d60e57953f82e363fc68fae877b7 to your computer and use it in GitHub Desktop.
Save alex-hhh/2394d60e57953f82e363fc68fae877b7 to your computer and use it in GitHub Desktop.
#lang racket
;; Updated plot library required, https://github.com/alex-hhh/plot/tree/ah/interactive-overlays
(require plot pict)
;; Helper function to add a pict as a plot overlay -- don't want to add "pict"
;; as a dependency to the "plot" package
(define (add-pict-overlay plot-snip x y pict)
(send plot-snip add-general-overlay x y
(lambda (dc x y) (draw-pict pict dc x y))
(pict-width pict)
(pict-height pict)))
;; Will be called when the mouse moves over the plot SNIP. X and Y are the
;; mouse position in plot coordinates and they will be #f if the cursor is
;; outside of the actual plot area
(define (on-hover snip event x y)
(when (and x y)
;; ... but only if we are still inside the plot area
(define sx (sin x)) ; the function value at X
(send snip set-overlay-renderers (list (vrule x) (hrule sx)))
(send snip refresh-overlays)))
;; Create the plot snip and add the hover callback
(define snip (plot-snip (function sin) #:x-min -10 #:x-max 10))
(send snip set-mouse-callback on-hover)
snip
@alex-hhh
Copy link
Author

alex-hhh commented Feb 6, 2018

plot-hover-6

@bennn
Copy link

bennn commented Feb 6, 2018

I don't know much about drawing contexts either, but yes I will try working on this

@alex-hhh
Copy link
Author

alex-hhh commented Feb 9, 2018

I think I know what is happening, I'll fix this sometimes next week.

@bennn
Copy link

bennn commented Feb 11, 2018

I tried to fix this, but no success yet. Happy to talk more.

@alex-hhh
Copy link
Author

I figured it out, the problem was that the plot functions set their own clipping region and have to be passed the correct plot bounds and dc areas. I updated the draw function to use the region received by the snip draw method, see racket/plot#37 for more details. This was not a problem for the original implementation, because I did not change the clipping region in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment