Add tick labels on axes

This commit is contained in:
Dimitri Lozeve 2021-04-29 21:33:03 +02:00
parent 60fd538a67
commit 1c9f6e9a55
3 changed files with 38 additions and 24 deletions

BIN
demo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Before After
Before After

View file

@ -39,7 +39,7 @@
(def row (vector-ref canvas (1- i))) (def row (vector-ref canvas (1- i)))
(for/collect ((j (in-range (u8vector-length row)))) (for/collect ((j (in-range (u8vector-length row))))
(integer->char (+ (char->integer #\) (u8vector-ref row j)))))) (integer->char (+ (char->integer #\) (u8vector-ref row j))))))
(list->string (flatten (map (lambda (l) (append1 l #\newline)) chars)))) (string-join (map list->string chars) #\newline))
(def (indexf pred . lsts) (def (indexf pred . lsts)
(find pred (apply map list lsts))) (find pred (apply map list lsts)))

View file

@ -1,6 +1,7 @@
(export #t) (export #t)
(import :std/iter (import :std/format
:std/iter
:std/misc/list :std/misc/list
:std/misc/string :std/misc/string
:dlozeve/uniplot/braille :dlozeve/uniplot/braille
@ -23,33 +24,46 @@
(def (line-plot lsts (colors +default-colors+) (def (line-plot lsts (colors +default-colors+)
width: (width 160) height: (height 80) borders: (borders #t) width: (width 160) height: (height 80) borders: (borders #t)
xlabel: (xlabel #f)) xlabel: (xlabel #f))
(def canvases (define-values (xmin xmax ymin ymax canvases)
(match lsts (match lsts
([ys] [(draw-canvas (iota (length ys)) ys ([ys] (let ((xmin 0)
(scale-fn 0 (length ys)) (xmax (length ys))
(scale-fn (apply min ys) (apply max ys)) (ymin (apply min ys))
width: width height: height)]) (ymax (apply max ys)))
([xs ys] [(draw-canvas xs ys (values xmin xmax ymin ymax
(scale-fn (apply min xs) (apply max xs)) [(draw-canvas (iota (length ys)) ys
(scale-fn (apply min ys) (apply max ys)) (scale-fn xmin xmax)
width: width height: height)]) (scale-fn ymin ymax)
([xs . yss] width: width height: height)])))
(let* ((x-scale-fn (scale-fn (apply min xs) (apply max xs))) ([xs ys] (let ((xmin (apply min xs))
(xmax (apply max xs))
(ymin (apply min ys))
(ymax (apply max ys)))
(values xmin xmax ymin ymax
[(draw-canvas xs ys
(scale-fn xmin xmax)
(scale-fn ymin ymax)
width: width height: height)])))
([xs . yss] (let* ((xmin (apply min xs))
(xmax (apply max xs))
(all-ys (flatten yss)) (all-ys (flatten yss))
(min-ys (apply min all-ys)) (ymin (apply min all-ys))
(max-ys (apply max all-ys)) (ymax (apply max all-ys))
(y-scale-fn (scale-fn min-ys max-ys))) (x-scale-fn (scale-fn xmin xmax))
(y-scale-fn (scale-fn ymin ymax)))
(values xmin xmax ymin ymax
(for/collect ((ys yss)) (for/collect ((ys yss))
(draw-canvas xs ys x-scale-fn y-scale-fn (draw-canvas xs ys x-scale-fn y-scale-fn
width: width height: height)))))) width: width height: height)))))))
(def canvases-str (canvases->string canvases colors)) (def canvases-str (canvases->string canvases colors))
(def hor-size (u8vector-length (vector-ref (car canvases) 0))) (def hor-size (u8vector-length (vector-ref (car canvases) 0)))
(def plot (if borders (def plot (if borders
(str (str
" ┌" (make-string (+ 2 hor-size) #\─) "┐\n │ " (format " ┌─~a─┐\n~5,1F ┤ " (make-string hor-size #\─) ymax)
(string-subst canvases-str "\n" " │ \n │ ") (string-subst canvases-str "\n" " │\n │ ")
(make-string (+ 1 hor-size) #\ ) (format " │\n~5,1F ┤~a │\n" ymin (make-string (+ 1 hor-size) #\ ))
"│\n └" (make-string (+ 2 hor-size) #\─) "┘\n") (format " └─┬~a┬─┘\n" (make-string (- hor-size 2) #\─))
(format "\n ~5,1F~a~5,1F\n" xmin (make-string (- hor-size 6) #\ ) xmax))
canvases-str)) canvases-str))
(if xlabel (if xlabel
(str plot (make-string (quotient hor-size 2) #\ ) xlabel "\n") (str plot (make-string (quotient hor-size 2) #\ ) xlabel "\n")