From 522fc9740a26d6d9d6d0bf832f4dc5263fa96419 Mon Sep 17 00:00:00 2001 From: Alexis King Date: Mon, 15 Jun 2020 13:27:19 -0500 Subject: [PATCH] Add #:invert? option to error-bars --- plot-doc/plot/scribblings/renderer2d.scrbl | 8 ++++- plot-lib/info.rkt | 2 ++ plot-lib/plot/private/plot2d/point.rkt | 35 ++++++++++++++-------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/plot-doc/plot/scribblings/renderer2d.scrbl b/plot-doc/plot/scribblings/renderer2d.scrbl index 05ffe0f1..8a5c1835 100644 --- a/plot-doc/plot/scribblings/renderer2d.scrbl +++ b/plot-doc/plot/scribblings/renderer2d.scrbl @@ -150,6 +150,7 @@ An example of automatic scaling: [#:line-style line-style plot-pen-style/c (error-bar-line-style)] [#:width width (>=/c 0) (error-bar-width)] [#:alpha alpha (real-in 0 1) (error-bar-alpha)] + [#:invert? invert? boolean? #f] ) renderer2d?]{ Returns a renderer that draws error bars. The first and second element in each vector in @(racket bars) comprise the coordinate; the third is the height. @@ -158,7 +159,12 @@ The first and second element in each vector in @(racket bars) comprise the coord (error-bars (list (vector 2 4 12) (vector 4 16 20) (vector 6 36 10)))))] -} + +If @racket[invert?] is @racket[#t], the x and y coordinates are inverted, and the bars are drawn horizontally +rather than vertically. This is intended for use with the corresponding option of @racket[discrete-histogram] +and @racket[stacked-histogram]. + +@history[#:changed "1.1" @elem{Added the @racket[#:invert?] option.}]} @defproc[(candlesticks [candles (sequence/c (sequence/c #:min-count 5 real?))] diff --git a/plot-lib/info.rkt b/plot-lib/info.rkt index b84ca09b..8ab99707 100644 --- a/plot-lib/info.rkt +++ b/plot-lib/info.rkt @@ -17,3 +17,5 @@ (define pkg-desc "Plot non-GUI interface") (define pkg-authors '(ntoronto)) + +(define version "1.1") diff --git a/plot-lib/plot/private/plot2d/point.rkt b/plot-lib/plot/private/plot2d/point.rkt index f6678e5f..1c481c9f 100644 --- a/plot-lib/plot/private/plot2d/point.rkt +++ b/plot-lib/plot/private/plot2d/point.rkt @@ -188,21 +188,25 @@ (: error-bars-render-fun (-> (Listof Real) (Listof Real) (Listof Real) Plot-Color Nonnegative-Real Plot-Pen-Style - Nonnegative-Real Nonnegative-Real + Nonnegative-Real Nonnegative-Real Boolean 2D-Render-Proc)) -(define ((error-bars-render-fun xs ys hs color line-width line-style width alpha) area) +(define ((error-bars-render-fun xs ys hs color line-width line-style width alpha invert?) area) (define clip-rect (send area get-clip-rect)) (define radius (* 1/2 width)) + (define angle (if invert? (/ pi 2) 0)) + + (: maybe-invert (All (A) (-> A A (Vectorof A)))) + (define maybe-invert (if invert? (λ (x y) (vector y x)) vector)) (send area put-alpha alpha) (send area put-pen color line-width line-style) (for ([x (in-list xs)] [y (in-list ys)] [h (in-list hs)]) - (when (rect-contains? clip-rect (vector x y)) - (define v1 (vector x (- y h))) - (define v2 (vector x (+ y h))) + (when (rect-contains? clip-rect (maybe-invert x y)) + (define v1 (maybe-invert x (- y h))) + (define v2 (maybe-invert x (+ y h))) (send area put-line v1 v2) - (send area put-tick v1 radius 0) - (send area put-tick v2 radius 0))) + (send area put-tick v1 radius angle) + (send area put-tick v2 radius angle))) empty) @@ -214,7 +218,8 @@ #:line-width Nonnegative-Real #:line-style Plot-Pen-Style #:width Nonnegative-Real - #:alpha Nonnegative-Real] + #:alpha Nonnegative-Real + #:invert? Boolean] renderer2d)) (define (error-bars bars #:x-min [x-min #f] #:x-max [x-max #f] @@ -223,7 +228,8 @@ #:line-width [line-width (error-bar-line-width)] #:line-style [line-style (error-bar-line-style)] #:width [width (error-bar-width)] - #:alpha [alpha (error-bar-alpha)]) + #:alpha [alpha (error-bar-alpha)] + #:invert? [invert? #f]) (define fail/kw (make-raise-keyword-error 'error-bars)) (cond [(and x-min (not (rational? x-min))) (fail/kw "#f or rational" '#:x-min x-min)] @@ -246,9 +252,14 @@ [x-max (if x-max x-max (apply max* xs))] [y-min (if y-min y-min (apply min* (map - ys hs)))] [y-max (if y-max y-max (apply max* (map + ys hs)))]) - (renderer2d (vector (ivl x-min x-max) (ivl y-min y-max)) #f default-ticks-fun - (error-bars-render-fun xs ys hs - color line-width line-style width alpha)))]))])) + (: maybe-invert (All (A) (-> A A (Vectorof A)))) + (define maybe-invert (if invert? (λ (x y) (vector y x)) vector)) + (renderer2d + (maybe-invert (ivl x-min x-max) (ivl y-min y-max)) + #f + default-ticks-fun + (error-bars-render-fun xs ys hs + color line-width line-style width alpha invert?)))]))])) ;; =================================================================================================== ;; Candlesticks