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 |
This is a real use case for this plot feature:
- vertical line added at cursor location
- two markers are added to the mean maximal plot representing the points that have data (the plot is interpolated between the points)
- a badge at the cursor location shows duration (X axis), power (Y axis), model power as well as information about the two data points
In the actual application, the movement is a lot smoother, and the plots are a lot sharper.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Alex, Thanks. Stupid me. joskoot