diff --git a/plot-doc/plot/scribblings/contracts.scrbl b/plot-doc/plot/scribblings/contracts.scrbl index 31dbef6c..231abb24 100644 --- a/plot-doc/plot/scribblings/contracts.scrbl +++ b/plot-doc/plot/scribblings/contracts.scrbl @@ -202,7 +202,7 @@ Like @(racket plot-colors/c), but for fill styles. Like @(racket plot-colors/c), but for opacities. } -@defproc[(labels/c [in-contract contract?]) contract? #:value (maybe-function/c in-contract (listof (or/c string? #f)))]{ +@defproc[(labels/c [in-contract contract?]) contract? #:value (maybe-function/c in-contract (listof (or/c string? pict? #f)))]{ Like @racket[plot-colors/c], but for strings. This is used, for example, to label @racket[stacked-histogram]s. } diff --git a/plot-doc/plot/scribblings/renderer2d.scrbl b/plot-doc/plot/scribblings/renderer2d.scrbl index 8a5c1835..b03d7578 100644 --- a/plot-doc/plot/scribblings/renderer2d.scrbl +++ b/plot-doc/plot/scribblings/renderer2d.scrbl @@ -39,7 +39,15 @@ Every appearance keyword argument defaults to the value of a parameter. This allows whole families of plots to be altered with little work. For example, setting @(racket (line-color 3)) causes every subsequent renderer that draws connected lines to draw its lines in blue. -@bold{Label argument.} Lastly, there is @(racket #:label). If given, the @(racket function) renderer will generate a label entry that @(racket plot) puts in the legend. +@bold{Label argument.} Lastly, there is @(racket #:label). If given, the +@(racket function) renderer will generate a label entry that @(racket plot) +puts in the legend. The label argument can be a string or a @(racket pict). +For most use cases, the string will be sufficient, especially since it allows +using Unicode characters, and thus some mathematical notation. For more +complex cases, a @(racket pict) can be used, whic allows arbitrary text and +graphics to be used as label entries. + +@history[#:changed "7.9" "Added support for pictures for #:label"] Not every renderer-producing function has a @(racket #:label) argument; for example, @(racket error-bars). @@ -57,7 +65,7 @@ Not every renderer-producing function has a @(racket #:label) argument; for exam [#:size size (>=/c 0) (point-size)] [#:line-width line-width (>=/c 0) (point-line-width)] [#:alpha alpha (real-in 0 1) (point-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a @tech{renderer} that draws points. Use it, for example, to draw 2D scatter plots. @@ -114,6 +122,8 @@ For example: @item{To see the distribution of 1-dimensional data; as a substitute for box or violin plots.} @item{To anonymize spatial data, showing i.e. an office's neighborhood but hiding its address.} ] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(vector-field @@ -127,7 +137,7 @@ For example: [#:line-width line-width (>=/c 0) (vector-field-line-width)] [#:line-style line-style plot-pen-style/c (vector-field-line-style)] [#:alpha alpha (real-in 0 1) (vector-field-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that draws a vector field. @@ -139,6 +149,8 @@ An example of automatic scaling: @interaction[#:eval plot-eval (plot (vector-field (λ (x y) (vector (+ x y) (- x y))) -2 2 -2 2))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(error-bars @@ -196,10 +208,12 @@ fourth, and fifth elements in each vector comprise the open, high, low, and clos [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that plots a function of @italic{x}. For example, a parabola: @interaction[#:eval plot-eval (plot (function sqr -2 2))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(inverse [f (real? . -> . real?)] @@ -210,15 +224,17 @@ Returns a renderer that plots a function of @italic{x}. For example, a parabola: [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Like @(racket function), but regards @(racket f) as a function of @italic{y}. For example, a parabola, an inverse parabola, and the reflection line: @interaction[#:eval plot-eval (plot (list (axes) - (function sqr -2 2 #:label "y = x^2") + (function sqr -2 2 #:label "y = x²") (function (λ (x) x) #:color 0 #:style 'dot #:label "y = x") - (inverse sqr -2 2 #:color 3 #:label "x = y^2")))] + (inverse sqr -2 2 #:color 3 #:label "x = y²")))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(lines [vs (sequence/c (sequence/c #:min-count 2 real?))] @@ -228,7 +244,7 @@ For example, a parabola, an inverse parabola, and the reflection line: [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that draws lines. This is directly useful for plotting a time series, such as a random walk: @@ -241,6 +257,8 @@ This is directly useful for plotting a time series, such as a random walk: #:color 6 #:label "Random walk"))] The @(racket parametric) and @(racket polar) functions are defined using @(racket lines). + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(parametric [f (real? . -> . (sequence/c real?))] @@ -252,12 +270,14 @@ The @(racket parametric) and @(racket polar) functions are defined using @(racke [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that plots vector-valued functions of time. For example, the circle as a function of time can be plotted using @interaction[#:eval plot-eval - (plot (parametric (λ (t) (vector (cos t) (sin t))) 0 (* 2 pi)))] + (plot (parametric (λ (t) (vector (cos t) (sin t))) 0 (* 2 pi)))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(polar [f (real? . -> . real?)] @@ -269,13 +289,15 @@ For example, the circle as a function of time can be plotted using [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that plots functions from angle to radius. Note that the angle parameters @(racket θ-min) and @(racket θ-max) default to @(racket 0) and @(racket (* 2 pi)). For example, drawing a full circle: @interaction[#:eval plot-eval (plot (polar (λ (θ) 1)))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(density [xs (sequence/c real?)] @@ -288,7 +310,7 @@ For example, drawing a full circle: [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that plots an estimated density for the given points, which are optionally weighted by @racket[ws]. The bandwidth for the kernel is calculated as @(racket (* bw-adjust 1.06 sd (expt n -0.2))), where @(racket sd) is the standard deviation of the data and @(racket n) is the number of points. @@ -304,6 +326,8 @@ For example, to plot an estimated density of the triangle distribution: 2000 (λ (n) (- (+ (random) (random)) 1))) #:color 0 #:width 2 #:style 'dot #:label "Est. density")))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(hrule [y real?] @@ -312,10 +336,12 @@ For example, to plot an estimated density of the triangle distribution: [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Draws a horizontal line at @italic{y}. By default, the line spans the entire plot area width. + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(vrule [x real?] @@ -324,10 +350,12 @@ By default, the line spans the entire plot area width. [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Draws a vertical line at @italic{x}. By default, the line spans the entire plot area height. + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{2D Interval Renderers} @@ -348,12 +376,14 @@ These renderers each correspond with a line renderer, and graph the area between [#:line2-width line2-width (>=/c 0) (interval-line2-width)] [#:line2-style line2-style plot-pen-style/c (interval-line2-style)] [#:alpha alpha (real-in 0 1) (interval-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Corresponds with @(racket function). @interaction[#:eval plot-eval (plot (function-interval (λ (x) 0) (λ (x) (exp (* -1/2 (sqr x)))) -4 4 #:line1-style 'transparent))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(inverse-interval @@ -370,12 +400,14 @@ Corresponds with @(racket function). [#:line2-width line2-width (>=/c 0) (interval-line2-width)] [#:line2-style line2-style plot-pen-style/c (interval-line2-style)] [#:alpha alpha (real-in 0 1) (interval-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Corresponds with @(racket inverse). @interaction[#:eval plot-eval (plot (inverse-interval sin (λ (x) 0) (- pi) pi #:line2-style 'long-dash))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(lines-interval @@ -392,7 +424,7 @@ Corresponds with @(racket inverse). [#:line2-width line2-width (>=/c 0) (interval-line2-width)] [#:line2-style line2-style plot-pen-style/c (interval-line2-style)] [#:alpha alpha (real-in 0 1) (interval-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Corresponds with @(racket lines). @@ -402,6 +434,8 @@ Corresponds with @(racket lines). (lines-interval (list #(0 0) #(1 1/2)) (list #(0 1) #(1 3/2)) #:color 4 #:line1-color 4 #:line2-color 4 #:label "Parallelogram")))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(parametric-interval @@ -420,7 +454,7 @@ Corresponds with @(racket lines). [#:line2-width line2-width (>=/c 0) (interval-line2-width)] [#:line2-style line2-style plot-pen-style/c (interval-line2-style)] [#:alpha alpha (real-in 0 1) (interval-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Corresponds with @(racket parametric). @@ -430,6 +464,8 @@ Corresponds with @(racket parametric). (define (f2 t) (vector (* 1/2 (cos t)) (* 1/2 (sin t)))) (plot (parametric-interval f1 f2 (- pi) pi))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(polar-interval @@ -447,7 +483,7 @@ Corresponds with @(racket parametric). [#:line2-width line2-width (>=/c 0) (interval-line2-width)] [#:line2-style line2-style plot-pen-style/c (interval-line2-style)] [#:alpha alpha (real-in 0 1) (interval-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Corresponds with @(racket polar). @@ -456,6 +492,8 @@ Corresponds with @(racket polar). (define (f2 θ) (+ 1 (* 1/4 (cos (* 10 θ))))) (plot (list (polar-axes #:number 10) (polar-interval f1 f2 #:label "[f1,f2]")))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{2D Contour (Isoline) Renderers} @@ -469,12 +507,14 @@ Corresponds with @(racket polar). [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that plots a contour line, or a line of constant value (height). A circle of radius @(racket r), for example, is the line of constant value @(racket r) for the distance function: @interaction[#:eval plot-eval (plot (isoline (λ (x y) (sqrt (+ (sqr x) (sqr y)))) 1.5 -2 2 -2 2 #:label "z"))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } In this case, @(racket r) = @(racket 1.5). @@ -491,7 +531,7 @@ It may be renamed in the future, with @racket[isoline] as an alias. [#:widths widths (pen-widths/c (listof real?)) (contour-widths)] [#:styles styles (plot-pen-styles/c (listof real?)) (contour-styles)] [#:alphas alphas (alphas/c (listof real?)) (contour-alphas)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that plots contour lines, or lines of constant value (height). @@ -513,6 +553,8 @@ For example, #:colors '("blue" "red") #:widths '(4 1) #:styles '(solid dot)))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(contour-intervals @@ -527,7 +569,7 @@ For example, [#:contour-widths contour-widths (pen-widths/c (listof real?)) (contour-widths)] [#:contour-styles contour-styles (plot-pen-styles/c (listof real?)) (contour-styles)] [#:alphas alphas (alphas/c (listof ivl?)) (contour-interval-alphas)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that fills the area between contour lines, and additionally draws contour lines. @@ -537,6 +579,8 @@ For example, the canonical saddle, with its gradient field superimposed: -2 2 -2 2 #:label "z") (vector-field (λ (x y) (vector (* 2 x) (* -2 y))) #:color "black" #:label "Gradient")))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{2D Rectangle Renderers} @@ -551,7 +595,7 @@ For example, the canonical saddle, with its gradient field superimposed: [#:line-width line-width (>=/c 0) (rectangle-line-width)] [#:line-style line-style plot-pen-style/c (rectangle-line-style)] [#:alpha alpha (real-in 0 1) (rectangle-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that draws rectangles. @@ -563,6 +607,8 @@ edge of the plot area in the respective direction. For example, @interaction[#:eval plot-eval (plot (rectangles (list (vector (ivl -1 0) (ivl -1 1)) (vector (ivl 0 2) (ivl 1 2)))))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(area-histogram @@ -577,7 +623,7 @@ For example, [#:line-width line-width (>=/c 0) (rectangle-line-width)] [#:line-style line-style plot-pen-style/c (rectangle-line-style)] [#:alpha alpha (real-in 0 1) (rectangle-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer2d?]{ Returns a renderer that draws a histogram approximating the area under a curve. The @(racket #:samples) argument determines the accuracy of the calculated areas. @@ -602,7 +648,7 @@ The @(racket #:samples) argument determines the accuracy of the calculated areas [#:line-width line-width (>=/c 0) (rectangle-line-width)] [#:line-style line-style plot-pen-style/c (rectangle-line-style)] [#:alpha alpha (real-in 0 1) (rectangle-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] [#:add-ticks? add-ticks? boolean? #t] [#:far-ticks? far-ticks? boolean? #f] ) renderer2d?]{ @@ -643,6 +689,8 @@ For example, #:x-label "Breakfast Food" #:y-label "Cooking Time (minutes)" #:title "Cooking Times For Breakfast Food, Per Processor")] When interleaving many histograms, consider setting the @racket[discrete-histogram-skip] parameter to change @racket[skip]'s default value. + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(stacked-histogram diff --git a/plot-doc/plot/scribblings/renderer3d.scrbl b/plot-doc/plot/scribblings/renderer3d.scrbl index d00f0ab6..c78de086 100644 --- a/plot-doc/plot/scribblings/renderer3d.scrbl +++ b/plot-doc/plot/scribblings/renderer3d.scrbl @@ -34,7 +34,7 @@ See @secref["renderer2d-function-arguments"] for a detailed example. [#:size size (>=/c 0) (point-size)] [#:line-width line-width (>=/c 0) (point-line-width)] [#:alpha alpha (real-in 0 1) (point-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that draws points in 3D space. @@ -62,6 +62,8 @@ negative direction, so total spread along e.g. the x-axis is twice @racket[x-jit Note that adding random noise to data, via jittering or otherwise, is usually a bad idea. See the documentation for @racket[points] for examples where jittering may be appropriate. + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(vector-field3d @@ -76,13 +78,15 @@ See the documentation for @racket[points] for examples where jittering may be ap [#:line-width line-width (>=/c 0) (vector-field-line-width)] [#:line-style line-style plot-pen-style/c (vector-field-line-style)] [#:alpha alpha (real-in 0 1) (vector-field-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that draws a vector field in 3D space. The arguments are interpreted identically to the corresponding arguments to @racket[vector-field]. @examples[#:eval plot-eval (plot3d (vector-field3d (λ (x y z) (vector x z y)) -2 2 -2 2 -2 2))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{3D Line Renderers} @@ -96,10 +100,12 @@ The arguments are interpreted identically to the corresponding arguments to @rac [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that draws connected lines. The @racket[parametric3d] function is defined in terms of this one. + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(parametric3d @@ -113,7 +119,7 @@ The @racket[parametric3d] function is defined in terms of this one. [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots a vector-valued function of time. For example, @interaction[#:eval plot-eval @@ -121,6 +127,8 @@ Returns a renderer that plots a vector-valued function of time. For example, (plot3d (parametric3d (λ (t) (3d-polar->3d-cartesian (* t 80) t 1)) (- pi) pi #:samples 3000 #:alpha 0.5) #:altitude 25)] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{3D Surface Renderers} @@ -137,14 +145,16 @@ Returns a renderer that plots a vector-valued function of time. For example, [#:line-width line-width (>=/c 0) (surface-line-width)] [#:line-style line-style plot-pen-style/c (surface-line-style)] [#:alpha alpha (real-in 0 1) (surface-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots a two-input, one-output function. For example, @interaction[#:eval plot-eval (plot3d (list (surface3d (λ (x y) (+ (sqr x) (sqr y))) -1 1 -1 1 - #:label "z = x^2 + y^2") + #:label "z = x² + y²") (surface3d (λ (x y) (- (+ (sqr x) (sqr y)))) -1 1 -1 1 #:color 4 #:line-color 4 - #:label "z = -x^2 - y^2")))] + #:label "z = -x² - y²")))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(polar3d @@ -159,7 +169,7 @@ Returns a renderer that plots a two-input, one-output function. For example, [#:line-width line-width (>=/c 0) (surface-line-width)] [#:line-style line-style plot-pen-style/c (surface-line-style)] [#:alpha alpha (real-in 0 1) (surface-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots a function from longitude and latitude to radius. @racket[(f θ ϕ)] → @racket[r] @@ -181,6 +191,8 @@ Combining polar function renderers allows faking latitudes or longitudes in larg #:line-style 'transparent #:alpha 2/3) (polar3d f2 #:color "navajowhite" #:line-style 'transparent #:alpha 2/3))))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(parametric-surface3d @@ -199,7 +211,7 @@ Combining polar function renderers allows faking latitudes or longitudes in larg [#:line-width line-width (>=/c 0) (surface-line-width)] [#:line-style line-style plot-pen-style/c (surface-line-style)] [#:alpha alpha (real-in 0 1) (surface-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots a two-input, one-output function. @racket[(f s t)] → @racket['(x y z)] @@ -225,6 +237,8 @@ For example, #:label "torus2")) #:z-min -6 #:z-max 6 #:altitude 22)] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(polygons3d @@ -238,7 +252,7 @@ For example, [#:line-width line-width (>=/c 0) (surface-line-width)] [#:line-style line-style plot-pen-style/c (surface-line-style)] [#:alpha alpha (real-in 0 1) (surface-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that draws polygons. The @racket[parametric-surface3d] function is defined in terms of this one. @@ -248,6 +262,8 @@ The @racket[parametric-surface3d] function is defined in terms of this one. (list (list 1 0 0)(list 0 1 0)(list 0 0 0)))) #:angle 355 #:altitude 30)] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{3D Contour (Isoline) Renderers} @@ -262,7 +278,7 @@ The @racket[parametric-surface3d] function is defined in terms of this one. [#:width width (>=/c 0) (line-width)] [#:style style plot-pen-style/c (line-style)] [#:alpha alpha (real-in 0 1) (line-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots a single contour line on the surface of a function. @@ -273,6 +289,8 @@ This function is not terribly useful by itself, but can be when combined with ot (define (saddle x y) (- (sqr x) (sqr y))) (plot3d (list (surface3d saddle -1 1 -1 1) (isoline3d saddle 1/4 #:width 2 #:style 'long-dash)))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(contours3d @@ -286,7 +304,7 @@ This function is not terribly useful by itself, but can be when combined with ot [#:widths widths (pen-widths/c (listof real?)) (contour-widths)] [#:styles styles (plot-pen-styles/c (listof real?)) (contour-styles)] [#:alphas alphas (alphas/c (listof real?)) (contour-alphas)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots contour lines on the surface of a function. @@ -295,7 +313,9 @@ In particular, when @(racket levels) is @(racket 'auto), contour values correspo For example, @interaction[#:eval plot-eval (plot3d (contours3d (λ (x y) (+ (sqr x) (sqr y))) -1.1 1.1 -1.1 1.1 - #:label "z = x^2 + y^2"))] + #:label "z = x² + y²"))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(contour-intervals3d @@ -314,7 +334,7 @@ For example, [#:contour-widths contour-widths (pen-widths/c (listof real?)) (contour-widths)] [#:contour-styles contour-styles (plot-pen-styles/c (listof real?)) (contour-styles)] [#:alphas alphas (alphas/c (listof ivl?)) (contour-interval-alphas)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots contour intervals and contour lines on the surface of a function. The appearance keyword arguments are interpreted identically to the appearance keyword arguments to @(racket contour-intervals). @@ -322,7 +342,9 @@ The appearance keyword arguments are interpreted identically to the appearance k For example, @interaction[#:eval plot-eval (plot3d (contour-intervals3d (λ (x y) (+ (sqr x) (sqr y))) -1.1 1.1 -1.1 1.1 - #:label "z = x^2 + y^2"))] + #:label "z = x² + y²"))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{3D Isosurface Renderers} @@ -339,7 +361,7 @@ For example, [#:line-width line-width (>=/c 0) (surface-line-width)] [#:line-style line-style plot-pen-style/c (surface-line-style)] [#:alpha alpha (real-in 0 1) (surface-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots the surface of constant output value of the function @(racket f). The argument @(racket d) is the constant value. @@ -348,6 +370,8 @@ For example, a sphere is all the points in which the Euclidean distance function (λ (x y z) (sqrt (+ (sqr x) (sqr y) (sqr z)))) 1 -1 1 -1 1 -1 1) #:altitude 25)] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(isosurfaces3d @@ -364,7 +388,7 @@ For example, a sphere is all the points in which the Euclidean distance function [#:line-widths line-widths (pen-widths/c (listof real?)) (isosurface-line-widths)] [#:line-styles line-styles (plot-pen-styles/c (listof real?)) (isosurface-line-styles)] [#:alphas alphas (alphas/c (listof real?)) (isosurface-alphas)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that plots multiple isosurfaces. The appearance keyword arguments are interpreted similarly to those of @(racket contours). @@ -377,6 +401,8 @@ Use this to visualize functions from three inputs to one output; for example: #:z-min -2 #:z-max 2)] If it helps, think of the output of @(racket f) as a density or charge. + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @section{3D Rectangle Renderers} @@ -392,7 +418,7 @@ If it helps, think of the output of @(racket f) as a density or charge. [#:line-width line-width (>=/c 0) (rectangle3d-line-width)] [#:line-style line-style plot-pen-style/c (rectangle-line-style)] [#:alpha alpha (real-in 0 1) (rectangle-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] ) renderer3d?]{ Returns a renderer that draws rectangles. @@ -415,6 +441,8 @@ This can be used to draw histograms; for example, (vector x-ivl y-ivl (ivl 0 z))))) #:alpha 3/4 #:label "Appx. 2D Normal"))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(discrete-histogram3d @@ -430,7 +458,7 @@ This can be used to draw histograms; for example, [#:line-width line-width (>=/c 0) (rectangle3d-line-width)] [#:line-style line-style plot-pen-style/c (rectangle-line-style)] [#:alpha alpha (real-in 0 1) (rectangle-alpha)] - [#:label label (or/c string? #f) #f] + [#:label label (or/c string? pict? #f) #f] [#:add-x-ticks? add-x-ticks? boolean? #t] [#:add-y-ticks? add-y-ticks? boolean? #t] [#:x-far-ticks? x-far-ticks? boolean? #f] @@ -443,6 +471,8 @@ Missing pairs are not drawn; for example, (plot3d (discrete-histogram3d '(#(a a 1) #(a b 2) #(b b 3)) #:label "Missing (b,a)" #:color 4 #:line-color 4))] + +@history[#:changed "7.9" "Added support for pictures for #:label"] } @defproc[(stacked-histogram3d diff --git a/plot-lib/plot/private/common/legend.rkt b/plot-lib/plot/private/common/legend.rkt index 71583451..4d13b250 100644 --- a/plot-lib/plot/private/common/legend.rkt +++ b/plot-lib/plot/private/common/legend.rkt @@ -3,6 +3,7 @@ ;; Functions that create legend entries and lists of legend entries. (require racket/class racket/match racket/list racket/string + (only-in typed/pict pict) "type-doc.rkt" "math.rkt" "format.rkt" @@ -14,7 +15,7 @@ ;; =================================================================================================== ;; Line legends -(:: line-legend-entry (-> String Plot-Color Nonnegative-Real Plot-Pen-Style legend-entry)) +(:: line-legend-entry (-> (U String pict) Plot-Color Nonnegative-Real Plot-Pen-Style legend-entry)) (define (line-legend-entry label color width style) (legend-entry label (λ (pd x-size y-size) (define y (* 1/2 y-size)) @@ -22,7 +23,7 @@ (send pd set-alpha 1) (send pd draw-line (vector (ann 0 Real) y) (vector x-size y))))) -(:: line-legend-entries (-> String (Listof Real) (Listof String) +(:: line-legend-entries (-> (U String pict) (Listof Real) (Listof String) (Plot-Colors (Listof Real)) (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) @@ -51,7 +52,7 @@ ;; =================================================================================================== ;; Rectangle legends -(:: rectangle-legend-entry (-> String +(:: rectangle-legend-entry (-> (U String pict) Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style legend-entry)) @@ -63,7 +64,7 @@ (send pd draw-rect (vector (ivl 0 x-size) (ivl 0 y-size)))))) (:: rectangle-legend-entries - (-> String (Listof Real) + (-> (U String pict) (Listof Real) (Plot-Colors (Listof Real)) (Plot-Brush-Styles (Listof Real)) (Plot-Colors (Listof Real)) (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) (Listof legend-entry))) @@ -97,7 +98,7 @@ ;; =================================================================================================== ;; Interval legends -(:: interval-legend-entry (-> String +(:: interval-legend-entry (-> (U String pict) Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Plot-Color Nonnegative-Real Plot-Pen-Style @@ -123,7 +124,7 @@ (vector x-size (ann 0 Real)))))) (:: interval-legend-entries - (-> String (Listof ivl) (Listof String) + (-> (U String pict) (Listof ivl) (Listof String) (Plot-Colors (Listof ivl)) (Plot-Brush-Styles (Listof ivl)) (Plot-Colors (Listof ivl)) (Pen-Widths (Listof ivl)) (Plot-Pen-Styles (Listof ivl)) (Plot-Colors (Listof ivl)) (Pen-Widths (Listof ivl)) (Plot-Pen-Styles (Listof ivl)) @@ -178,7 +179,7 @@ ;; =================================================================================================== ;; Point legends -(:: point-legend-entry (-> String Point-Sym Plot-Color Plot-Color Nonnegative-Real Nonnegative-Real +(:: point-legend-entry (-> (U String pict) Point-Sym Plot-Color Plot-Color Nonnegative-Real Nonnegative-Real legend-entry)) (define (point-legend-entry label sym color fill-color size line-width) (legend-entry label (λ (pd x-size y-size) @@ -188,7 +189,7 @@ (send pd draw-glyphs (list (vector (* 1/2 x-size) (* 1/2 y-size))) sym size)))) -(:: arrow-legend-entry (-> String Plot-Color Nonnegative-Real Plot-Pen-Style legend-entry)) +(:: arrow-legend-entry (-> (U String pict) Plot-Color Nonnegative-Real Plot-Pen-Style legend-entry)) (define (arrow-legend-entry label color line-width line-style) (legend-entry label (λ (pd x-size y-size) (send pd set-pen color line-width line-style) diff --git a/plot-lib/plot/private/common/plot-device.rkt b/plot-lib/plot/private/common/plot-device.rkt index b3f7f471..f30bb5d7 100644 --- a/plot-lib/plot/private/common/plot-device.rkt +++ b/plot-lib/plot/private/common/plot-device.rkt @@ -8,6 +8,7 @@ (require typed/racket/draw typed/racket/class + (only-in typed/pict pict pict? pict-width pict-height pict-descent pict-ascent) racket/match racket/math racket/bool racket/list racket/vector "draw-attribs.rkt" "color-map.rkt" @@ -270,7 +271,7 @@ ;; Sets only the font size, not the face or family. (define/public (set-font-size size) - (set-font-attribs size + (set-font-attribs size (send (send dc get-font) get-face) (send (send dc get-font) get-family))) @@ -286,7 +287,13 @@ ;; Returns the extent of a string, as exact reals. (define/public (get-text-extent str) (define-values (w h b d) - (send dc get-text-extent str #f #t 0)) + (cond ((string? str) + (send dc get-text-extent str #f #t 0)) + ((pict? str) + (values (pict-width str) (pict-height str) + (pict-descent str) (pict-ascent str))) + (#t + (values 0 0 0 0)))) (values (inexact->exact w) (inexact->exact h) (inexact->exact b) (inexact->exact d))) @@ -587,13 +594,13 @@ (define/public (draw-legend legend-entries rect) (define n (length legend-entries)) - (match-define (list (legend-entry #{labels : (Listof String)} + (match-define (list (legend-entry #{labels : (Listof (U String pict))} #{draw-procs : (Listof Legend-Draw-Proc)}) ...) legend-entries) (match-define (vector (ivl x-min x-max) (ivl y-min y-max)) rect) - + (define old-size (send (send dc get-font) get-point-size)) (define old-face (send (send dc get-font) get-face)) (define old-family (send (send dc get-font) get-family)) @@ -602,16 +609,23 @@ (or (plot-legend-font-face) old-face) (or (plot-legend-font-family) old-family)) - (define-values (_1 label-y-size baseline _2) (get-text-extent (first labels))) - (define horiz-gap (get-text-width " ")) + (define-values (max-label-width max-label-height) + (for/fold ([width : Exact-Rational 0] + [max-height : Exact-Rational 0]) + ([label (in-list labels)]) + (define-values (w h b a) (get-text-extent label)) + (values (max w width) (max h max-height)))) + + (define-values (horiz-gap min-label-height baseline _1) + (get-text-extent " ")) + (define top-gap baseline) (define bottom-gap (* 1/2 baseline)) - (define baseline-skip (+ label-y-size baseline)) + (define baseline-skip (+ max-label-height baseline)) - (define max-label-x-size (apply max (map (λ ([label : String]) (get-text-width label)) labels))) - (define labels-x-size (+ max-label-x-size horiz-gap)) + (define labels-x-size (+ max-label-width horiz-gap)) - (define draw-y-size (max 0 (- label-y-size baseline))) + (define draw-y-size (max 0 (- min-label-height baseline))) (define draw-x-size (* 4 draw-y-size)) (define legend-x-size (+ horiz-gap @@ -661,11 +675,15 @@ (set-alpha (plot-foreground-alpha)) (set-clipping-rect legend-rect) - (for ([label (in-list labels)] [draw-proc (in-list draw-procs)] [i (in-naturals)]) - (define label-y-min (+ legend-y-min top-gap (* i baseline-skip))) - (draw-text label (vector (ann label-x-min Real) (ann label-y-min Real)) 'top-left 0 0 #t) + (for ([label (in-list labels)] [draw-proc (in-list draw-procs)] [i (in-naturals)]) + (define-values (_1 label-height _2 _3) (get-text-extent label)) + (define legend-entry-y-min (+ legend-y-min top-gap (* i baseline-skip))) + (define label-y-min (+ legend-entry-y-min (* 1/2 (- max-label-height label-height)))) + (if (pict? label) + (draw-pict label (vector (ann label-x-min Real) (ann label-y-min Real)) 'top-left 0) + (draw-text label (vector (ann label-x-min Real) (ann label-y-min Real)) 'top-left 0 0 #t)) - (define draw-y-min (+ label-y-min (* 1/2 baseline))) + (define draw-y-min (+ legend-entry-y-min (* 1/2 (- max-label-height draw-y-size)))) (define entry-pd (make-object plot-device% dc draw-x-min draw-y-min draw-x-size draw-y-size)) (send entry-pd reset-drawing-params #f) @@ -674,6 +692,6 @@ ;; reset plot font attributes (set-font-attribs old-size old-face old-family) - + (clear-clipping-rect)) )) ; end class diff --git a/plot-lib/plot/private/common/types.rkt b/plot-lib/plot/private/common/types.rkt index 0bc33f79..6fc16c56 100644 --- a/plot-lib/plot/private/common/types.rkt +++ b/plot-lib/plot/private/common/types.rkt @@ -79,7 +79,7 @@ (deftype (Pen-Widths A) (List-Generator A Nonnegative-Real)) (deftype (Plot-Brush-Styles A) (List-Generator A Plot-Brush-Style)) (deftype (Alphas A) (List-Generator A Nonnegative-Real)) -(deftype (Labels A) (List-Generator A (U String #f))) +(deftype (Labels A) (List-Generator A (U String pict #f))) (deftype Contour-Levels (U 'auto Positive-Integer (Listof Real))) @@ -90,7 +90,7 @@ (deftype Legend-Draw-Proc (-> (Instance Plot-Device%) Real Real Void)) -(struct legend-entry ([label : String] [draw : Legend-Draw-Proc]) #:transparent) +(struct legend-entry ([label : (U String pict)] [draw : Legend-Draw-Proc]) #:transparent) (define-type Plot-Device% (Class @@ -113,8 +113,8 @@ [set-font-size (-> Real Void)] [get-char-height (-> Exact-Rational)] [get-char-baseline (-> Exact-Rational)] - [get-text-extent (-> String (Values Exact-Rational Exact-Rational Exact-Rational Exact-Rational))] - [get-text-width (-> String Exact-Rational)] + [get-text-extent (-> (U String pict) (Values Exact-Rational Exact-Rational Exact-Rational Exact-Rational))] + [get-text-width (-> (U String pict) Exact-Rational)] [set-text-foreground (-> Plot-Color Void)] [set-clipping-rect (-> Rect Void)] [clear-clipping-rect (-> Void)] diff --git a/plot-lib/plot/private/plot2d/contour.rkt b/plot-lib/plot/private/plot2d/contour.rkt index 1a05bc86..d0a4ef1e 100644 --- a/plot-lib/plot/private/plot2d/contour.rkt +++ b/plot-lib/plot/private/plot2d/contour.rkt @@ -3,6 +3,7 @@ ;; Renderers for contour lines and contour intervals (require typed/racket/class racket/match racket/list racket/vector + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -15,7 +16,7 @@ (: isoline-render-proc (-> 2D-Sampler Real Positive-Integer Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((isoline-render-proc g z samples color width style alpha label) area) (match-define (vector x-ivl y-ivl) (send area get-bounds-rect)) @@ -46,7 +47,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (isoline f z [x-min #f] [x-max #f] [y-min #f] [y-max #f] #:samples [samples (contour-samples)] @@ -81,7 +82,7 @@ (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) (Alphas (Listof Real)) - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((contours-render-proc g levels samples colors widths styles alphas label) area) (let/ec return : (Treeof legend-entry) @@ -130,7 +131,7 @@ #:widths (Pen-Widths (Listof Real)) #:styles (Plot-Pen-Styles (Listof Real)) #:alphas (Alphas (Listof Real)) - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (contours f [x-min #f] [x-max #f] [y-min #f] [y-max #f] #:samples [samples (contour-samples)] @@ -163,7 +164,7 @@ (Plot-Colors (Listof ivl)) (Plot-Brush-Styles (Listof ivl)) (Plot-Colors (Listof Real)) (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) (Alphas (Listof ivl)) - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((contour-intervals-render-proc g levels samples colors styles contour-colors contour-widths contour-styles alphas label) @@ -240,7 +241,7 @@ #:contour-widths (Pen-Widths (Listof Real)) #:contour-styles (Plot-Pen-Styles (Listof Real)) #:alphas (Alphas (Listof ivl)) - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (contour-intervals f [x-min #f] [x-max #f] [y-min #f] [y-max #f] diff --git a/plot-lib/plot/private/plot2d/interval.rkt b/plot-lib/plot/private/plot2d/interval.rkt index d43d80f9..19993525 100644 --- a/plot-lib/plot/private/plot2d/interval.rkt +++ b/plot-lib/plot/private/plot2d/interval.rkt @@ -3,6 +3,7 @@ ;; Renderers for intervals between functions. (require typed/racket/class racket/match racket/math racket/list + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -17,7 +18,7 @@ Plot-Color Nonnegative-Real Plot-Pen-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((lines-interval-render-proc v1s v2s color style line1-color line1-width line1-style @@ -54,7 +55,7 @@ #:line2-width Nonnegative-Real #:line2-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (lines-interval v1s v2s #:x-min [x-min #f] #:x-max [x-max #f] @@ -110,7 +111,7 @@ #:line2-width Nonnegative-Real #:line2-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (parametric-interval f1 f2 t-min t-max @@ -167,7 +168,7 @@ #:line2-width Nonnegative-Real #:line2-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (polar-interval f1 f2 [θ-min 0] [θ-max (* 2 pi)] @@ -216,7 +217,7 @@ Plot-Color Nonnegative-Real Plot-Pen-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((function-interval-render-proc f1 f2 samples color style line1-color line1-width line1-style @@ -249,7 +250,7 @@ #:line2-width Nonnegative-Real #:line2-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (function-interval f1 f2 [x-min #f] [x-max #f] @@ -297,7 +298,7 @@ Plot-Color Nonnegative-Real Plot-Pen-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((inverse-interval-render-proc f1 f2 samples color style line1-color line1-width line1-style @@ -330,7 +331,7 @@ #:line2-width Nonnegative-Real #:line2-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (inverse-interval f1 f2 [y-min #f] [y-max #f] diff --git a/plot-lib/plot/private/plot2d/line.rkt b/plot-lib/plot/private/plot2d/line.rkt index d92bb173..7bcb2251 100644 --- a/plot-lib/plot/private/plot2d/line.rkt +++ b/plot-lib/plot/private/plot2d/line.rkt @@ -4,6 +4,7 @@ (require typed/racket/class racket/match racket/math racket/list racket/sequence plot/utils + (only-in typed/pict pict) (only-in math/statistics stddev) "../common/type-doc.rkt" "../common/utils.rkt") @@ -16,7 +17,7 @@ (: lines-render-proc (-> (Listof (Vectorof Real)) Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((lines-render-proc vs color width style alpha label) area) (send area put-alpha alpha) @@ -34,7 +35,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (lines vs #:x-min [x-min #f] #:x-max [x-max #f] @@ -74,7 +75,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (parametric f t-min t-max #:x-min [x-min #f] #:x-max [x-max #f] @@ -114,7 +115,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (polar f [θ-min 0] [θ-max (* 2 pi)] #:x-min [x-min #f] #:x-max [x-max #f] @@ -153,7 +154,7 @@ Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((rule-render-proc v v-min v-max h/v color width style alpha label) area) (match-define (vector (ivl x-min x-max) (ivl y-min y-max)) (send area get-bounds-rect)) @@ -178,7 +179,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (vrule x [y-min #f] [y-max #f] #:color [color (line-color)] @@ -204,7 +205,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (hrule y [x-min #f] [x-max #f] #:color [color (line-color)] @@ -229,7 +230,7 @@ (: function-render-proc (-> Sampler Positive-Integer Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((function-render-proc f samples color width style alpha label) area) (match-define (vector x-ivl y-ivl) (send area get-bounds-rect)) @@ -251,7 +252,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (function f [x-min #f] [x-max #f] #:y-min [y-min #f] #:y-max [y-max #f] @@ -286,7 +287,7 @@ (: inverse-render-proc (-> Sampler Positive-Integer Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((inverse-render-proc f samples color width style alpha label) area) (match-define (vector x-ivl y-ivl) (send area get-bounds-rect)) @@ -308,7 +309,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (inverse f [y-min #f] [y-max #f] #:x-min [x-min #f] #:x-max [x-max #f] @@ -351,7 +352,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (density xs [bw-adjust 1] [orig-ws #f] #:x-min [x-min #f] #:x-max [x-max #f] diff --git a/plot-lib/plot/private/plot2d/point.rkt b/plot-lib/plot/private/plot2d/point.rkt index 1c481c9f..4c74fd89 100644 --- a/plot-lib/plot/private/plot2d/point.rkt +++ b/plot-lib/plot/private/plot2d/point.rkt @@ -3,6 +3,7 @@ ;; Renderers for points and other point-like things. (require typed/racket/class racket/match racket/math racket/list + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -22,7 +23,7 @@ (: points-render-fun (-> (Listof (Vectorof Real)) Point-Sym Plot-Color Plot-Color Nonnegative-Real Nonnegative-Real Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((points-render-fun vs sym color fill-color size line-width alpha label) area) (send area put-alpha alpha) @@ -44,7 +45,7 @@ #:size Nonnegative-Real #:line-width Nonnegative-Real #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (points vs #:x-min [x-min #f] #:x-max [x-max #f] @@ -93,7 +94,7 @@ Positive-Integer (U Real 'auto 'normalized) Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((vector-field-render-fun f samples scale color line-width line-style alpha label) area) (match-define (vector (ivl x-min x-max) (ivl y-min y-max)) (send area get-bounds-rect)) @@ -156,7 +157,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (vector-field f [x-min #f] [x-max #f] [y-min #f] [y-max #f] #:samples [samples (vector-field-samples)] diff --git a/plot-lib/plot/private/plot2d/rectangle.rkt b/plot-lib/plot/private/plot2d/rectangle.rkt index 6580f459..7e235a7b 100644 --- a/plot-lib/plot/private/plot2d/rectangle.rkt +++ b/plot-lib/plot/private/plot2d/rectangle.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require racket/match typed/racket/class racket/list racket/sequence + (only-in typed/pict pict) (only-in racket/math infinite?) plot/utils "../common/type-doc.rkt" @@ -15,7 +16,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 2D-Render-Proc)) (define ((rectangles-render-proc rects color style line-color line-width line-style alpha label) area) @@ -44,7 +45,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (rectangles rects #:x-min [x-min #f] #:x-max [x-max #f] @@ -111,7 +112,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer2d)) (define (area-histogram f bin-bounds #:x-min [x-min #f] #:x-max [x-max #f] @@ -201,7 +202,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f) + #:label (U String pict #f) #:add-ticks? Boolean #:far-ticks? Boolean] renderer2d)) diff --git a/plot-lib/plot/private/plot3d/contour.rkt b/plot-lib/plot/private/plot3d/contour.rkt index 3f0808b1..d80ea120 100644 --- a/plot-lib/plot/private/plot3d/contour.rkt +++ b/plot-lib/plot/private/plot3d/contour.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require typed/racket/class racket/match racket/list + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -13,7 +14,7 @@ (: isoline3d-render-proc (-> 2D-Sampler Real Positive-Integer Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U #f String) + (U String pict #f) 3D-Render-Proc)) (define ((isoline3d-render-proc f z samples color width style alpha label) area) (match-define (vector x-ivl y-ivl z-ivl) (send area get-bounds-rect)) @@ -43,7 +44,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (isoline3d f z [x-min #f] [x-max #f] @@ -86,7 +87,7 @@ (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) (Alphas (Listof Real)) - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((contours3d-render-proc f levels samples colors widths styles alphas label) area) (match-define (vector x-ivl y-ivl z-ivl) (send area get-bounds-rect)) @@ -136,7 +137,7 @@ #:widths (Pen-Widths (Listof Real)) #:styles (Plot-Pen-Styles (Listof Real)) #:alphas (Alphas (Listof Real)) - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (contours3d f [x-min #f] [x-max #f] @@ -182,7 +183,7 @@ (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) (Alphas (Listof ivl)) - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((contour-intervals3d-render-proc f levels samples colors styles line-colors line-widths line-styles @@ -278,7 +279,7 @@ #:contour-widths (Pen-Widths (Listof Real)) #:contour-styles (Plot-Pen-Styles (Listof Real)) #:alphas (Alphas (Listof ivl)) - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (contour-intervals3d f [x-min #f] [x-max #f] diff --git a/plot-lib/plot/private/plot3d/isosurface.rkt b/plot-lib/plot/private/plot3d/isosurface.rkt index ed4e1157..32d7a94d 100644 --- a/plot-lib/plot/private/plot3d/isosurface.rkt +++ b/plot-lib/plot/private/plot3d/isosurface.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require typed/racket/class racket/match racket/list math/flonum racket/math + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -14,7 +15,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((isosurface3d-render-proc f d samples color style line-color line-width line-style alpha label) @@ -51,7 +52,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (isosurface3d f d [x-min #f] [x-max #f] [y-min #f] [y-max #f] [z-min #f] [z-max #f] #:samples [samples (plot3d-samples)] @@ -92,7 +93,7 @@ (Plot-Colors (Listof Real)) (Plot-Brush-Styles (Listof Real)) (Plot-Colors (Listof Real)) (Pen-Widths (Listof Real)) (Plot-Pen-Styles (Listof Real)) (Alphas (Listof Real)) - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((isosurfaces3d-render-proc f rd-min rd-max levels samples colors styles line-colors line-widths line-styles alphas label) @@ -155,7 +156,7 @@ #:line-widths (Pen-Widths (Listof Real)) #:line-styles (Plot-Pen-Styles (Listof Real)) #:alphas (Alphas (Listof Real)) - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (isosurfaces3d f [x-min #f] [x-max #f] [y-min #f] [y-max #f] [z-min #f] [z-max #f] #:d-min [d-min #f] #:d-max [d-max #f] @@ -196,7 +197,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((polar3d-render-proc f g samples color style line-color line-width line-style alpha label) area) @@ -268,7 +269,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (polar3d f #:x-min [x-min #f] #:x-max [x-max #f] diff --git a/plot-lib/plot/private/plot3d/line.rkt b/plot-lib/plot/private/plot3d/line.rkt index 04ad85ac..7ea0eefa 100644 --- a/plot-lib/plot/private/plot3d/line.rkt +++ b/plot-lib/plot/private/plot3d/line.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require typed/racket/class racket/match racket/list + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -12,7 +13,7 @@ (: lines3d-render-proc (-> (-> (Listof (Vectorof Real))) Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((lines3d-render-proc vs-fun color width style alpha label) area) (send area put-alpha alpha) @@ -26,7 +27,7 @@ (U #f Real) (U #f Real) (U #f Real) (U #f Real) (U #f Real) (U #f Real) Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) renderer3d)) (define (lines3d-renderer vs-thnk x-min x-max y-min y-max z-min z-max color width style alpha label) @@ -57,7 +58,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (lines3d vs #:x-min [x-min #f] #:x-max [x-max #f] @@ -95,7 +96,7 @@ #:width Nonnegative-Real #:style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (parametric3d f t-min t-max #:x-min [x-min #f] #:x-max [x-max #f] diff --git a/plot-lib/plot/private/plot3d/param-surf.rkt b/plot-lib/plot/private/plot3d/param-surf.rkt index 6b6f7683..8867f73f 100644 --- a/plot-lib/plot/private/plot3d/param-surf.rkt +++ b/plot-lib/plot/private/plot3d/param-surf.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require typed/racket/class racket/match racket/list + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -12,7 +13,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U #f String) + (U String pict #f) 3D-Render-Proc)) (define ((polygons3d-render-proc vs-fun color style line-color line-width line-style alpha label) area) @@ -30,7 +31,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U #f String) + (U String pict #f) renderer3d)) (define (polygons3d-renderer vs-thnk x-min x-max y-min y-max z-min z-max color style line-color line-width line-style alpha label) @@ -65,7 +66,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U #f String)] + #:label (U String pict #f)] renderer3d)) (define (polygons3d vs #:x-min [x-min #f] #:x-max [x-max #f] @@ -109,7 +110,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U #f String)] + #:label (U String pict #f)] renderer3d)) (define (parametric-surface3d f s-min s-max t-min t-max #:x-min [x-min #f] #:x-max [x-max #f] diff --git a/plot-lib/plot/private/plot3d/point.rkt b/plot-lib/plot/private/plot3d/point.rkt index 53af4957..17e3834b 100644 --- a/plot-lib/plot/private/plot3d/point.rkt +++ b/plot-lib/plot/private/plot3d/point.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require typed/racket/class racket/list racket/match + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -20,7 +21,7 @@ Plot-Color Plot-Color Nonnegative-Real Nonnegative-Real Nonnegative-Real - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((points3d-render-proc vs sym color fill-color size line-width alpha label) area) (send area put-alpha alpha) @@ -45,7 +46,7 @@ #:size Nonnegative-Real #:line-width Nonnegative-Real #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (points3d vs #:x-min [x-min #f] #:x-max [x-max #f] @@ -105,7 +106,7 @@ Positive-Integer (U Real 'auto 'normalized) Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((vector-field3d-render-fun f samples scale color line-width line-style alpha label) area) (match-define (vector (ivl x-min x-max) (ivl y-min y-max) (ivl z-min z-max)) @@ -171,7 +172,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (vector-field3d f [x-min #f] [x-max #f] [y-min #f] [y-max #f] [z-min #f] [z-max #f] #:samples [samples (vector-field3d-samples)] diff --git a/plot-lib/plot/private/plot3d/rectangle.rkt b/plot-lib/plot/private/plot3d/rectangle.rkt index 215af340..282146fe 100644 --- a/plot-lib/plot/private/plot3d/rectangle.rkt +++ b/plot-lib/plot/private/plot3d/rectangle.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require racket/match racket/list typed/racket/class racket/sequence + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -14,7 +15,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U String #f) + (U String pict #f) 3D-Render-Proc)) (define ((rectangles3d-render-proc rects color style line-color line-width line-style alpha label) area) @@ -38,7 +39,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f)] + #:label (U String pict #f)] renderer3d)) (define (rectangles3d rects #:x-min [x-min #f] #:x-max [x-max #f] @@ -132,7 +133,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U String #f) + #:label (U String pict #f) #:add-x-ticks? Boolean #:add-y-ticks? Boolean #:x-far-ticks? Boolean diff --git a/plot-lib/plot/private/plot3d/surface.rkt b/plot-lib/plot/private/plot3d/surface.rkt index 1a1f4d29..727af46e 100644 --- a/plot-lib/plot/private/plot3d/surface.rkt +++ b/plot-lib/plot/private/plot3d/surface.rkt @@ -1,6 +1,7 @@ #lang typed/racket/base (require typed/racket/class racket/match racket/list + (only-in typed/pict pict) plot/utils "../common/type-doc.rkt" "../common/utils.rkt") @@ -14,7 +15,7 @@ Plot-Color Plot-Brush-Style Plot-Color Nonnegative-Real Plot-Pen-Style Nonnegative-Real - (U #f String) + (U String pict #f) 3D-Render-Proc)) (define ((surface3d-render-proc f samples color style line-color line-width line-style alpha label) area) @@ -44,7 +45,7 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:alpha Nonnegative-Real - #:label (U #f String)] + #:label (U String pict #f)] renderer3d)) (define (surface3d f [x-min #f] [x-max #f] [y-min #f] [y-max #f] #:z-min [z-min #f] #:z-max [z-max #f] #:samples [samples (plot3d-samples)] diff --git a/plot-test/plot/tests/PRs/58.rkt b/plot-test/plot/tests/PRs/58.rkt new file mode 100644 index 00000000..50ad5968 --- /dev/null +++ b/plot-test/plot/tests/PRs/58.rkt @@ -0,0 +1,73 @@ +#lang racket +(require rackunit + plot + pict + racket/draw + racket/runtime-path + "../helpers.rkt") + +;; Tests for https://github.com/racket/plot/issues/58 + +;; Setup a draw context used for font metrics by the picts used in these tests +;; -- this will ensure that tests produce identical data sets on all +;; platforms. Note that this line will make the pict look unaligned -- remove +;; this line to restore "normal" behavior. + +(dc-for-text-size (new mock-record-dc%)) + +(define (do-plot-2d output-fn) + (output-fn + (list + (lines '([10 1] [20 1.5] [30 1.7]) + #:color "DarkOrange" + #:width 3 + #:label (vc-append (standard-fish 35 25 #:color "DarkOrange") (text "One Fish"))) + (points '([10 1.2] [15 1.0] [20 0.9] [25 0.7] [30 0.4]) + #:color "forestgreen" + #:fill-color "medium forest green" + #:line-width 3 + #:sym 'fullcircle1 + #:size 20 + #:label (vc-append (hc-append + (standard-fish 25 15 #:color "forestgreen") + (standard-fish 25 15 #:color "medium forest green")) + (text "Two Fish"))) + (lines '([10 .5] [20 1.6] [30 1.0]) + #:color "firebrick" + #:width 3 + #:label (vc-append (standard-fish 50 30 #:color "firebrick") + (text "Red Fish"))) + (function sin 10 30 + #:color "steel blue" + #:width 3 + #:label (vc-append (standard-fish 30 50 #:color "steel blue") + (text "Blue Fish"))) + + ))) + +(define (do-plot-3d output-fn) + (output-fn + (surface3d (λ (x y) (+ (/ 1.0 x)) + (/ 1.0 y)) + -2 2 -2 2 + #:color '(255 128 128) + #:line-color '(255 128 128) + #:line-width 1.5 + #:label (standard-fish 50 30)))) + +(define-runtime-path pr58-2d "./test-data/pr58-2d.dat") +(define-runtime-path pr58-3d "./test-data/pr58-3d.dat") + +(define pr58-test-suite + (test-suite + "PR#58: add support for pict legend entries" + (test-case "pr58 2d plots" + (check-draw-steps do-plot-2d pr58-2d)) + (test-case "pr58 3d plots" + (check-draw-steps-3d do-plot-3d pr58-3d)))) + +(module+ test + (require rackunit/text-ui) + (run-tests pr58-test-suite)) + + diff --git a/plot-test/plot/tests/PRs/test-data/pr58-2d.dat b/plot-test/plot/tests/PRs/test-data/pr58-2d.dat new file mode 100644 index 00000000..69a054bb Binary files /dev/null and b/plot-test/plot/tests/PRs/test-data/pr58-2d.dat differ diff --git a/plot-test/plot/tests/PRs/test-data/pr58-2d.png b/plot-test/plot/tests/PRs/test-data/pr58-2d.png new file mode 100644 index 00000000..333f14b6 Binary files /dev/null and b/plot-test/plot/tests/PRs/test-data/pr58-2d.png differ diff --git a/plot-test/plot/tests/PRs/test-data/pr58-3d.dat b/plot-test/plot/tests/PRs/test-data/pr58-3d.dat new file mode 100644 index 00000000..2aa426e5 Binary files /dev/null and b/plot-test/plot/tests/PRs/test-data/pr58-3d.dat differ diff --git a/plot-test/plot/tests/PRs/test-data/pr58-3d.png b/plot-test/plot/tests/PRs/test-data/pr58-3d.png new file mode 100644 index 00000000..a3ff3716 Binary files /dev/null and b/plot-test/plot/tests/PRs/test-data/pr58-3d.png differ diff --git a/plot-test/plot/tests/helpers.rkt b/plot-test/plot/tests/helpers.rkt index f00f36aa..2917f7d2 100644 --- a/plot-test/plot/tests/helpers.rkt +++ b/plot-test/plot/tests/helpers.rkt @@ -1,7 +1,7 @@ #lang racket (require plot racket/draw rackunit file/gzip file/gunzip) -(provide check-draw-steps check-draw-steps-3d) +(provide check-draw-steps check-draw-steps-3d mock-record-dc%) ;; Helper functions to write plot tests which verify that the renderers work ;; correctly. See `check-draw-steps` and `check-draw-steps-3d`.