Permalink
Browse files

Added plot-x-tick-label-angle, plot-x-tick-label-anchor, and similar …

…for y and far axes
  • Loading branch information...
1 parent 3862fae commit be49a59e4dc223029b918c69244e19735cb16f28 Neil Toronto committed Apr 30, 2012
@@ -40,6 +40,16 @@
(defparam plot-y-far-axis? boolean? #t)
(defparam plot-z-far-axis? boolean? #t)
+(defparam plot-x-tick-label-angle angle real? 0)
+(defparam plot-y-tick-label-angle angle real? 0)
+(defparam plot-x-far-tick-label-angle angle real? 0)
+(defparam plot-y-far-tick-label-angle angle real? 0)
+
+(defparam plot-x-tick-label-anchor anchor anchor/c 'top)
+(defparam plot-y-tick-label-anchor anchor anchor/c 'right)
+(defparam plot-x-far-tick-label-anchor anchor anchor/c 'bottom)
+(defparam plot-y-far-tick-label-anchor anchor anchor/c 'left)
+
(defparam plot-decorations? boolean? #t)
(define-parameter-group plot-axes?
@@ -48,6 +58,16 @@
plot-z-axis? plot-z-far-axis?)
#:struct list)
+(define-parameter-group plot-tick-labels
+ (plot-x-tick-label-anchor
+ plot-x-tick-label-angle
+ plot-x-far-tick-label-anchor
+ plot-x-far-tick-label-angle
+ plot-y-tick-label-anchor
+ plot-y-tick-label-angle
+ plot-y-far-tick-label-anchor
+ plot-y-far-tick-label-angle))
+
(define-parameter-group plot-appearance
(plot-width
plot-height
@@ -56,7 +76,8 @@
plot-line-width plot-tick-size
plot-font-size plot-font-family
plot-legend-anchor plot-legend-box-alpha
- plot-axes? plot-decorations?
+ plot-axes? plot-tick-labels
+ plot-decorations?
plot-animating?))
(defproc (pen-gap) real? #:document-body
@@ -9,6 +9,14 @@
;; General plot parameters
plot-x-axis? plot-y-axis? plot-z-axis?
plot-x-far-axis? plot-y-far-axis? plot-z-far-axis?
+ plot-x-tick-label-anchor
+ plot-y-tick-label-anchor
+ plot-x-far-tick-label-anchor
+ plot-y-far-tick-label-anchor
+ plot-x-tick-label-angle
+ plot-y-tick-label-angle
+ plot-x-far-tick-label-angle
+ plot-y-far-tick-label-angle
plot-width plot-height
plot-foreground plot-foreground-alpha
plot-background plot-background-alpha
@@ -71,6 +79,7 @@
;; Parameter groups
plot-parameters
plot-axes?
+ plot-tick-labels
plot-appearance
plot3d-appearance
plot-output
@@ -228,29 +228,37 @@
(define x-far-tick-label-offset (vneg x-tick-label-offset))
(define y-far-tick-label-offset (vneg y-tick-label-offset))
- (define (get-tick-label-params ticks tick-label-offset tick-value->dc anchor)
+ (define (get-tick-label-params ticks tick-label-offset tick-value->dc anchor angle)
(for/list ([t (in-list ticks)] #:when (pre-tick-major? t))
(match-define (tick p _ label) t)
- (list label (v+ (tick-value->dc p) tick-label-offset) anchor)))
+ (list label (v+ (tick-value->dc p) tick-label-offset) anchor (degrees->radians angle))))
(define (get-x-tick-label-params)
(if (plot-x-axis?)
- (get-tick-label-params x-ticks x-tick-label-offset x-tick-value->dc 'top)
+ (get-tick-label-params x-ticks x-tick-label-offset x-tick-value->dc
+ (plot-x-tick-label-anchor)
+ (plot-x-tick-label-angle))
empty))
(define (get-y-tick-label-params)
(if (plot-y-axis?)
- (get-tick-label-params y-ticks y-tick-label-offset y-tick-value->dc 'right)
+ (get-tick-label-params y-ticks y-tick-label-offset y-tick-value->dc
+ (plot-y-tick-label-anchor)
+ (plot-y-tick-label-angle))
empty))
(define (get-x-far-tick-label-params)
(if (and (plot-x-far-axis?) draw-x-far-tick-labels?)
- (get-tick-label-params x-far-ticks x-far-tick-label-offset x-far-tick-value->dc 'bottom)
+ (get-tick-label-params x-far-ticks x-far-tick-label-offset x-far-tick-value->dc
+ (plot-x-far-tick-label-anchor)
+ (plot-x-far-tick-label-angle))
empty))
(define (get-y-far-tick-label-params)
(if (and (plot-y-far-axis?) draw-y-far-tick-labels?)
- (get-tick-label-params y-far-ticks y-far-tick-label-offset y-far-tick-value->dc 'left)
+ (get-tick-label-params y-far-ticks y-far-tick-label-offset y-far-tick-value->dc
+ (plot-y-far-tick-label-anchor)
+ (plot-y-far-tick-label-angle))
empty))
;; -----------------------------------------------------------------------------------------------
@@ -267,22 +275,35 @@
(define max-x-far-tick-offset (if (plot-x-far-axis?) (max-tick-offset x-far-ticks) 0))
(define max-y-far-tick-offset (if (plot-y-far-axis?) (max-tick-offset y-far-ticks) 0))
- (define (max-tick-label-height ts)
- (if (ormap pre-tick-major? ts) char-height 0))
+ (define (get-relative-corners params)
+ (append* (map (match-lambda
+ [(list label _ anchor angle)
+ (send pd get-text-corners label #(0 0) anchor angle)])
+ params)))
- (define (max-tick-label-width ts)
- (apply max 0 (for/list ([t (in-list ts)] #:when (pre-tick-major? t))
- (send pd get-text-width (tick-label t)))))
+ (define max-x-tick-label-height
+ (if (plot-x-axis?)
+ (apply max 0 (map (λ (corner) (vector-ref corner 1))
+ (get-relative-corners (get-x-tick-label-params))))
+ 0))
- (define max-x-tick-label-height (if (plot-x-axis?) (max-tick-label-height x-ticks) 0))
- (define max-y-tick-label-width (if (plot-y-axis?) (max-tick-label-width y-ticks) 0))
+ (define max-y-tick-label-width
+ (if (plot-y-axis?)
+ (- (apply min 0 (map (λ (corner) (vector-ref corner 0))
+ (get-relative-corners (get-y-tick-label-params)))))
+ 0))
- (define max-x-far-tick-label-height (if (and (plot-x-far-axis?) draw-x-far-tick-labels?)
- (max-tick-label-height x-far-ticks)
- 0))
- (define max-y-far-tick-label-width (if (and (plot-y-far-axis?) draw-y-far-tick-labels?)
- (max-tick-label-width y-far-ticks)
- 0))
+ (define max-x-far-tick-label-height
+ (if (and (plot-x-far-axis?) draw-x-far-tick-labels?)
+ (- (apply min 0 (map (λ (corner) (vector-ref corner 1))
+ (get-relative-corners (get-x-far-tick-label-params)))))
+ 0))
+
+ (define max-y-far-tick-label-width
+ (if (and (plot-y-far-axis?) draw-y-far-tick-labels?)
+ (apply max 0 (map (λ (corner) (vector-ref corner 0))
+ (get-relative-corners (get-y-far-tick-label-params))))
+ 0))
(define (get-x-label-params)
(define offset (vector 0 (+ max-x-tick-offset max-x-tick-label-height half-char-height)))
@@ -293,7 +314,8 @@
(list (plot-y-label) (v- (view->dc (vector x-min y-mid)) offset) 'bottom (/ pi 2)))
(define (get-x-far-label-params)
- (define offset (vector 0 (+ max-x-far-tick-offset max-x-far-tick-label-height half-char-height)))
+ (define offset (vector 0 (+ max-x-far-tick-offset max-x-far-tick-label-height
+ half-char-height)))
(list (plot-x-far-label) (v- (view->dc (vector x-mid y-max)) offset) 'bottom))
(define (get-y-far-label-params)
@@ -57,7 +57,28 @@ See @(racket ->pen-color) and @(racket ->brush-color) for details on how PLoT in
@doc-apply[plot-x-far-label]
@doc-apply[plot-y-far-label]
@doc-apply[plot-z-far-label]{
-The tick labels for ``far'' axes. See @racket[plot-x-ticks] for a discussion of near and far axes.
+The axis labels for ``far'' axes. See @racket[plot-x-ticks] for a discussion of near and far axes.
+}
+
+@doc-apply[plot-x-tick-label-anchor]
+@doc-apply[plot-x-tick-label-angle]
+@doc-apply[plot-y-tick-label-anchor]
+@doc-apply[plot-y-tick-label-angle]
+@doc-apply[plot-x-far-tick-label-anchor]
+@doc-apply[plot-x-far-tick-label-angle]
+@doc-apply[plot-y-far-tick-label-anchor]
+@doc-apply[plot-y-far-tick-label-angle]{
+Anchor and angles for axis tick labels (2D only).
+Angles are in degrees.
+The anchor refers to the part of the label attached to the end of the tick line.
+
+Set these when labels would otherwise overlap; for example, in histograms with long category names.
+@interaction[#:eval plot-eval
+ (parameterize ([plot-x-tick-label-anchor 'top-right]
+ [plot-x-tick-label-angle 30])
+ (plot (discrete-histogram '(#(really-long-category-name-1 2)
+ #(long-category-name-2 1.75)
+ #(long-category-name-3 2.5)))))]
}
@doc-apply[plot-x-axis?]
@@ -101,10 +101,11 @@
;; an exact rational function and a floating-point function
;; the plot of the exact rational function's graph should be smooth
(time
- (plot (list (function (λ (x) x) #:label "Exact")
- (function (λ (x) (exact->inexact x)) #:color 2 #:label "Inexact"))
- #:x-min #e100000000000000.0 #:x-max #e100000000000000.1
- #:width 450))
+ (parameterize ([plot-x-tick-label-angle 15])
+ (plot (list (function (λ (x) x) #:label "Exact")
+ (function (λ (x) (exact->inexact x)) #:color 2 #:label "Inexact"))
+ #:x-min #e100000000000000.0 #:x-max #e100000000000000.1
+ #:width 450)))
(time
(plot (function cos 0 0.0000001)
@@ -228,3 +228,17 @@
[plot-y-ticks (currency-ticks)])
(plot (list (function sin -4 4)
(function-label sin 1/3))))
+
+(parameterize ((plot-x-tick-label-angle 45)
+ (plot-x-tick-label-anchor 'top-right)
+ (plot-y-tick-label-angle 45)
+ (plot-y-tick-label-anchor 'bottom-right)
+ (plot-x-far-tick-label-angle 45)
+ (plot-x-far-tick-label-anchor 'bottom-left)
+ (plot-y-far-tick-label-angle 45)
+ (plot-y-far-tick-label-anchor 'top-left)
+ (plot-x-far-label "x far axis")
+ (plot-y-far-label "y far axis"))
+ (plot (list (discrete-histogram '(#(asdglkj 5399) #(liegjd 5390) #(pqlcxkgfj 3534)))
+ (x-ticks (list (tick 1 #t "asdgwieasdgwefj")) #:far? #t)
+ (y-ticks (list (tick 2500 #t "asdgwegawegfgwiej")) #:far? #t))))

0 comments on commit be49a59

Please sign in to comment.