Last active
February 7, 2018 08:57
-
-
Save alex-hhh/64f8af9c4bd5b5c65ada62dd9157e938 to your computer and use it in GitHub Desktop.
Plot interactive overlay demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 x y) | |
;; Clear previous overlays, as we'll set up new ones ... | |
(send snip clear-overlays) | |
(when (and x y) | |
;; ... but only if we are still inside the plot area | |
(define sx (sin x)) ; the function value at X | |
;; Draw a cross line at the current function position, xrule is more | |
;; efficient than drawing both a vrule or a hrule | |
(send snip add-xrule-overlay x sx) | |
;; Or we could draw the lines separately... | |
;; (send snip add-vrule-overlay x) | |
;; (send snip add-hrule-overlay sx) | |
;; Add a marker at the current function position, we also provide a label | |
;; with the function value | |
(send snip add-mark-overlay x sx #:radius 10 #:label (~r sx #:precision 2)) | |
;; Add a picture at the current mouse location | |
(define p (vl-append (text (format "X Value ~a" (~r x #:precision 2))) | |
(text (format "Y Value ~a" (~r sx #:precision 2))))) | |
(add-pict-overlay snip x y p)) | |
;; Finally, refresh the plot | |
(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another use case for this feature: the two plots are "linked" so that hovering the mouse on one plot will update the "current" line on both of them.