Skip to content

Commit 522fc97

Browse files
committed
Add #:invert? option to error-bars
1 parent aacfb37 commit 522fc97

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

plot-doc/plot/scribblings/renderer2d.scrbl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ An example of automatic scaling:
150150
[#:line-style line-style plot-pen-style/c (error-bar-line-style)]
151151
[#:width width (>=/c 0) (error-bar-width)]
152152
[#:alpha alpha (real-in 0 1) (error-bar-alpha)]
153+
[#:invert? invert? boolean? #f]
153154
) renderer2d?]{
154155
Returns a renderer that draws error bars.
155156
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
158159
(error-bars (list (vector 2 4 12)
159160
(vector 4 16 20)
160161
(vector 6 36 10)))))]
161-
}
162+
163+
If @racket[invert?] is @racket[#t], the x and y coordinates are inverted, and the bars are drawn horizontally
164+
rather than vertically. This is intended for use with the corresponding option of @racket[discrete-histogram]
165+
and @racket[stacked-histogram].
166+
167+
@history[#:changed "1.1" @elem{Added the @racket[#:invert?] option.}]}
162168

163169
@defproc[(candlesticks
164170
[candles (sequence/c (sequence/c #:min-count 5 real?))]

plot-lib/info.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
(define pkg-desc "Plot non-GUI interface")
1818

1919
(define pkg-authors '(ntoronto))
20+
21+
(define version "1.1")

plot-lib/plot/private/plot2d/point.rkt

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,25 @@
188188

189189
(: error-bars-render-fun (-> (Listof Real) (Listof Real) (Listof Real)
190190
Plot-Color Nonnegative-Real Plot-Pen-Style
191-
Nonnegative-Real Nonnegative-Real
191+
Nonnegative-Real Nonnegative-Real Boolean
192192
2D-Render-Proc))
193-
(define ((error-bars-render-fun xs ys hs color line-width line-style width alpha) area)
193+
(define ((error-bars-render-fun xs ys hs color line-width line-style width alpha invert?) area)
194194
(define clip-rect (send area get-clip-rect))
195195
(define radius (* 1/2 width))
196+
(define angle (if invert? (/ pi 2) 0))
197+
198+
(: maybe-invert (All (A) (-> A A (Vectorof A))))
199+
(define maybe-invert (if invert? (λ (x y) (vector y x)) vector))
196200

197201
(send area put-alpha alpha)
198202
(send area put-pen color line-width line-style)
199203
(for ([x (in-list xs)] [y (in-list ys)] [h (in-list hs)])
200-
(when (rect-contains? clip-rect (vector x y))
201-
(define v1 (vector x (- y h)))
202-
(define v2 (vector x (+ y h)))
204+
(when (rect-contains? clip-rect (maybe-invert x y))
205+
(define v1 (maybe-invert x (- y h)))
206+
(define v2 (maybe-invert x (+ y h)))
203207
(send area put-line v1 v2)
204-
(send area put-tick v1 radius 0)
205-
(send area put-tick v2 radius 0)))
208+
(send area put-tick v1 radius angle)
209+
(send area put-tick v2 radius angle)))
206210

207211
empty)
208212

@@ -214,7 +218,8 @@
214218
#:line-width Nonnegative-Real
215219
#:line-style Plot-Pen-Style
216220
#:width Nonnegative-Real
217-
#:alpha Nonnegative-Real]
221+
#:alpha Nonnegative-Real
222+
#:invert? Boolean]
218223
renderer2d))
219224
(define (error-bars bars
220225
#:x-min [x-min #f] #:x-max [x-max #f]
@@ -223,7 +228,8 @@
223228
#:line-width [line-width (error-bar-line-width)]
224229
#:line-style [line-style (error-bar-line-style)]
225230
#:width [width (error-bar-width)]
226-
#:alpha [alpha (error-bar-alpha)])
231+
#:alpha [alpha (error-bar-alpha)]
232+
#:invert? [invert? #f])
227233
(define fail/kw (make-raise-keyword-error 'error-bars))
228234
(cond
229235
[(and x-min (not (rational? x-min))) (fail/kw "#f or rational" '#:x-min x-min)]
@@ -246,9 +252,14 @@
246252
[x-max (if x-max x-max (apply max* xs))]
247253
[y-min (if y-min y-min (apply min* (map - ys hs)))]
248254
[y-max (if y-max y-max (apply max* (map + ys hs)))])
249-
(renderer2d (vector (ivl x-min x-max) (ivl y-min y-max)) #f default-ticks-fun
250-
(error-bars-render-fun xs ys hs
251-
color line-width line-style width alpha)))]))]))
255+
(: maybe-invert (All (A) (-> A A (Vectorof A))))
256+
(define maybe-invert (if invert? (λ (x y) (vector y x)) vector))
257+
(renderer2d
258+
(maybe-invert (ivl x-min x-max) (ivl y-min y-max))
259+
#f
260+
default-ticks-fun
261+
(error-bars-render-fun xs ys hs
262+
color line-width line-style width alpha invert?)))]))]))
252263

253264
;; ===================================================================================================
254265
;; Candlesticks

0 commit comments

Comments
 (0)