1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#lang typed/racket/base
(require (only-in typed/mred/mred Snip% Frame%)
typed/racket/draw typed/racket/class racket/match
plot/utils
plot/private/common/parameter-group
plot/private/common/draw
plot/private/common/deprecation-warning
plot/private/common/type-doc
plot/private/common/utils
plot/private/plot2d/plot-area
plot/private/no-gui/plot2d
plot/private/no-gui/plot2d-utils
"lazy-snip-typed.rkt"
typed/racket/unsafe)
(unsafe-provide plot-snip
plot-frame
plot)
;; ===================================================================================================
;; Plot to a snip
(:: plot-snip
(->* [(Treeof (U renderer2d nonrenderer))]
[#:x-min (U Real #f) #:x-max (U Real #f)
#:y-min (U Real #f) #:y-max (U Real #f)
#:width Positive-Integer
#:height Positive-Integer
#:title (U String #f)
#:x-label (U String #f)
#:y-label (U String #f)
#:legend-anchor Anchor]
(Instance Snip%)))
(define (plot-snip renderer-tree
#:x-min [x-min #f] #:x-max [x-max #f]
#:y-min [y-min #f] #:y-max [y-max #f]
#:width [width (plot-width)]
#:height [height (plot-height)]
#:title [title (plot-title)]
#:x-label [x-label (plot-x-label)]
#:y-label [y-label (plot-y-label)]
#:legend-anchor [legend-anchor (plot-legend-anchor)])
(define fail/kw (make-raise-keyword-error 'plot-snip))
(cond
[(and x-min (not (rational? x-min))) (fail/kw "#f or rational" '#:x-min x-min)]
[(and x-max (not (rational? x-max))) (fail/kw "#f or rational" '#:x-max x-max)]
[(and y-min (not (rational? y-min))) (fail/kw "#f or rational" '#:y-min y-min)]
[(and y-max (not (rational? y-max))) (fail/kw "#f or rational" '#:y-max y-max)]
[else
(parameterize ([plot-title title]
[plot-x-label x-label]
[plot-y-label y-label]
[plot-legend-anchor legend-anchor])
(define saved-plot-parameters (plot-parameters))
(define renderer-list (get-renderer-list renderer-tree))
(define bounds-rect (get-bounds-rect renderer-list x-min x-max y-min y-max))
(: make-bm (-> Boolean Rect Positive-Integer Positive-Integer
(Values (Instance Bitmap%) (U #f (Instance 2D-Plot-Area%)))))
(define (make-bm anim? bounds-rect width height)
(: area (U #f (Instance 2D-Plot-Area%)))
(define area #f)
(define bm
(parameterize/group ([plot-parameters saved-plot-parameters]
[plot-animating? (if anim? #t (plot-animating?))])
((if (plot-animating?) draw-bitmap draw-bitmap/supersampling)
(λ (dc)
(define-values (x-ticks x-far-ticks y-ticks y-far-ticks)
(get-ticks renderer-list bounds-rect))
(define new-area
(make-object 2d-plot-area%
bounds-rect x-ticks x-far-ticks y-ticks y-far-ticks
dc 0 0 width height))
(set! area new-area)
(plot-area new-area renderer-list))
width height)))
(values bm area))
(define-values (bm area) (make-bm #f bounds-rect width height))
(make-2d-plot-snip bm saved-plot-parameters make-bm bounds-rect area width height))]))
;; ===================================================================================================
;; Plot to a frame
(:: plot-frame
(->* [(Treeof (U renderer2d nonrenderer))]
[#:x-min (U Real #f) #:x-max (U Real #f)
#:y-min (U Real #f) #:y-max (U Real #f)
#:width Positive-Integer
#:height Positive-Integer
#:title (U String #f)
#:x-label (U String #f)
#:y-label (U String #f)
#:legend-anchor Anchor]
(Instance Frame%)))
(define (plot-frame renderer-tree
#:x-min [x-min #f] #:x-max [x-max #f]
#:y-min [y-min #f] #:y-max [y-max #f]
#:width [width (plot-width)]
#:height [height (plot-height)]
#:title [title (plot-title)]
#:x-label [x-label (plot-x-label)]
#:y-label [y-label (plot-y-label)]
#:legend-anchor [legend-anchor (plot-legend-anchor)])
(define fail/kw (make-raise-keyword-error 'plot-frame))
(cond
[(and x-min (not (rational? x-min))) (fail/kw "#f or rational" '#:x-min x-min)]
[(and x-max (not (rational? x-max))) (fail/kw "#f or rational" '#:x-max x-max)]
[(and y-min (not (rational? y-min))) (fail/kw "#f or rational" '#:y-min y-min)]
[(and y-max (not (rational? y-max))) (fail/kw "#f or rational" '#:y-max y-max)]
[else
;; make-snip will be called in a separate thread, make sure the
;; parameters have the correct values in that thread as well.
(define saved-plot-parameters (plot-parameters))
(: make-snip (-> Positive-Integer Positive-Integer (Instance Snip%)))
(define (make-snip width height)
(parameterize/group ([plot-parameters saved-plot-parameters])
(plot-snip
renderer-tree
#:x-min x-min #:x-max x-max #:y-min y-min #:y-max y-max #:width width #:height height
#:title title #:x-label x-label #:y-label y-label #:legend-anchor legend-anchor)))
(make-snip-frame make-snip width height (if title (format "Plot: ~a" title) "Plot"))]))
;; ===================================================================================================
;; Plot to a frame or a snip, depending on (plot-new-window?)
(:: plot
(->* [(Treeof (U renderer2d nonrenderer))]
[#:x-min (U Real #f) #:x-max (U Real #f)
#:y-min (U Real #f) #:y-max (U Real #f)
#:width Positive-Integer
#:height Positive-Integer
#:title (U String #f)
#:x-label (U String #f)
#:y-label (U String #f)
#:legend-anchor Anchor
#:out-file (U Path-String Output-Port #f)
#:out-kind (U 'auto Image-File-Format)
#:fgcolor Plot-Color
#:bgcolor Plot-Color
#:lncolor Plot-Color]
(U (Instance Snip%) Void)))
(define (plot renderer-tree
#:x-min [x-min #f] #:x-max [x-max #f]
#:y-min [y-min #f] #:y-max [y-max #f]
#:width [width (plot-width)]
#:height [height (plot-height)]
#:title [title (plot-title)]
#:x-label [x-label (plot-x-label)]
#:y-label [y-label (plot-y-label)]
#:legend-anchor [legend-anchor (plot-legend-anchor)]
#:out-file [out-file #f]
#:out-kind [out-kind 'auto]
#:fgcolor [fgcolor #f]
#:bgcolor [bgcolor #f]
#:lncolor [lncolor #f]) ; unused
(when fgcolor
(deprecation-warning "the plot #:fgcolor keyword argument" "plot-foreground"))
(when bgcolor
(deprecation-warning "the plot #:bgcolor keyword argument" "plot-background"))
(when lncolor
(deprecation-warning "the plot #:lncolor keyword argument"))
(define fail/kw (make-raise-keyword-error 'plot))
(cond
[(and x-min (not (rational? x-min))) (fail/kw "#f or rational" '#:x-min x-min)]
[(and x-max (not (rational? x-max))) (fail/kw "#f or rational" '#:x-max x-max)]
[(and y-min (not (rational? y-min))) (fail/kw "#f or rational" '#:y-min y-min)]
[(and y-max (not (rational? y-max))) (fail/kw "#f or rational" '#:y-max y-max)]
[else
(parameterize ([plot-foreground (if fgcolor fgcolor (plot-foreground))]
[plot-background (if bgcolor bgcolor (plot-background))])
(when out-file
(plot-file
renderer-tree out-file out-kind
#:x-min x-min #:x-max x-max #:y-min y-min #:y-max y-max #:width width #:height height
#:title title #:x-label x-label #:y-label y-label #:legend-anchor legend-anchor))
(cond [(plot-new-window?)
(define frame
(plot-frame
renderer-tree
#:x-min x-min #:x-max x-max #:y-min y-min #:y-max y-max #:width width #:height height
#:title title #:x-label x-label #:y-label y-label #:legend-anchor legend-anchor))
(send frame show #t)
(void)]
[else
(plot-snip
renderer-tree
#:x-min x-min #:x-max x-max #:y-min y-min #:y-max y-max #:width width #:height height
#:title title #:x-label x-label #:y-label y-label #:legend-anchor legend-anchor)]))]))
|