Add legend

This commit is contained in:
Dimitri Lozeve 2021-04-29 22:32:35 +02:00
parent 1c9f6e9a55
commit 67926de45c
3 changed files with 31 additions and 17 deletions

BIN
demo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Before After
Before After

View file

@ -2,7 +2,8 @@
(export main) (export main)
(import :std/misc/string (import :std/format
:std/misc/string
:dlozeve/fancy/format :dlozeve/fancy/format
:dlozeve/uniplot/lineplot) :dlozeve/uniplot/lineplot)
@ -11,4 +12,6 @@
(ys1 (map cos xs)) (ys1 (map cos xs))
(ys2 (map (lambda (x) (/ 1 (+ 0.5 x))) xs)) (ys2 (map (lambda (x) (/ 1 (+ 0.5 x))) xs))
(ys3 (map (lambda (x) (sin (* 2 x))) xs))) (ys3 (map (lambda (x) (sin (* 2 x))) xs)))
(displayln (line-plot [xs ys1 ys2 ys3] xlabel: "time")))) (displayln (line-plot [xs ys1 ys2 ys3]
xlabel: "time"
names: ["cos(time)" "1 / (time + 0.5)" "sin(2 × time)"]))))

View file

@ -4,8 +4,8 @@
:std/iter :std/iter
:std/misc/list :std/misc/list
:std/misc/string :std/misc/string
:dlozeve/uniplot/braille :dlozeve/fancy/format
:dlozeve/fancy/format) :dlozeve/uniplot/braille)
(def +default-colors+ '(green blue red yellow cyan magenta white)) (def +default-colors+ '(green blue red yellow cyan magenta white))
@ -22,8 +22,8 @@
canvas) canvas)
(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)
xlabel: (xlabel #f)) xlabel: (xlabel "") names: (names []))
(define-values (xmin xmax ymin ymax canvases) (define-values (xmin xmax ymin ymax canvases)
(match lsts (match lsts
([ys] (let ((xmin 0) ([ys] (let ((xmin 0)
@ -57,14 +57,25 @@
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 vert-size (vector-length (car canvases)))
(str (def plot (str
(format " ┌─~a─┐\n~5,1F ┤ " (make-string hor-size #\─) ymax) (format " ┌─~a─┐\n~5,1F ┤ " (make-string hor-size #\─) ymax)
(string-subst canvases-str "\n" " │\n │ ") (string-subst canvases-str "\n" " │\n │ ")
(format " │\n~5,1F ┤~a │\n" ymin (make-string (+ 1 hor-size) #\ )) (format " │\n~5,1F ┤~a │\n" ymin (make-string (+ 1 hor-size) #\ ))
(format " └─┬~a┬─┘\n" (make-string (- hor-size 2) #\─)) (format " └─┬~a┬─┘\n" (make-string (- hor-size 2) #\─))
(format "\n ~5,1F~a~5,1F\n" xmin (make-string (- hor-size 6) #\ ) xmax)) (format "\n ~5,1F~a~5,1F\n" xmin (make-string (- hor-size 6) #\ ) xmax)))
canvases-str)) (def plot-with-legend (if (null? names)
(if xlabel plot
(str plot (make-string (quotient hor-size 2) #\ ) xlabel "\n") (str plot
plot)) (cursor-up (+ 4 vert-size))
(cursor-forward (+ 12 hor-size))
(apply str
(for/collect ((name names) (color colors))
(str (graphics-style [color]) name (graphics-style)
(cursor-down 1)
(cursor-back (string-length name)))))
(cursor-down (- (+ 4 vert-size) (length names)))
(cursor-back (+ 12 hor-size)))))
(if (string-empty? xlabel)
plot-with-legend
(str plot-with-legend (make-string (quotient hor-size 2) #\ ) xlabel "\n")))