diff --git a/README.org b/README.org index 3e57e92..fcbba92 100644 --- a/README.org +++ b/README.org @@ -4,6 +4,9 @@ Unicode plotting library for [[https://cons.io/][Gerbil Scheme]]. Depends on [[https://github.com/dlozeve/fancy][Fancy]] for colors and display. +#+ATTR_HTML: :width 100% :style margin-left: auto; margin-right: auto; +[[./demo.png]] + ** Features - Braille canvas for plotting diff --git a/demo.png b/demo.png new file mode 100644 index 0000000..db79963 Binary files /dev/null and b/demo.png differ diff --git a/demo.ss b/demo.ss index 3a32cc5..b159bb2 100755 --- a/demo.ss +++ b/demo.ss @@ -9,5 +9,6 @@ (def (main . args) (let* ((xs (iota 1000 0 0.01)) (ys1 (map cos xs)) - (ys2 (map sin xs))) - (displayln (line-plot [xs ys1 ys2])))) + (ys2 (map (lambda (x) (/ 1 (+ 0.5 x))) xs)) + (ys3 (map (lambda (x) (sin (* 2 x))) xs))) + (displayln (line-plot [xs ys1 ys2 ys3])))) diff --git a/uniplot/braille.ss b/uniplot/braille.ss index b13c2a4..a095bf5 100644 --- a/uniplot/braille.ss +++ b/uniplot/braille.ss @@ -44,7 +44,7 @@ (def (indexf pred . lsts) (find pred (apply map list lsts))) -(def (canvases->string canvases (colors '(red green cyan yellow magenta white))) +(def (canvases->string canvases colors) (def canvases-str (map canvas->string canvases)) (def size (string-length (car canvases-str))) (apply str diff --git a/uniplot/lineplot.ss b/uniplot/lineplot.ss index ece0da4..80e0099 100644 --- a/uniplot/lineplot.ss +++ b/uniplot/lineplot.ss @@ -6,6 +6,8 @@ :dlozeve/uniplot/braille :dlozeve/fancy/format) +(def +default-colors+ '(green blue red yellow cyan magenta white)) + (def (scale-fn lo hi) (lambda (x) (/ (- x lo) (- hi lo)))) @@ -18,20 +20,18 @@ #t)) canvas) -(def (line-plot lsts width: (width 160) height: (height 80)) - (match lsts - ([ys] (canvas->string - (draw-canvas (iota (length ys)) ys - (scale-fn 0 (length ys)) - (scale-fn (apply min ys) (apply max ys)) - width: width height: height))) - ([xs ys] (canvas->string - (draw-canvas xs ys - (scale-fn (apply min xs) (apply max xs)) - (scale-fn (apply min ys) (apply max ys)) - width: width height: height))) - ([xs . yss] - (canvases->string +(def (line-plot lsts (colors +default-colors+) width: (width 160) height: (height 80)) + (canvases->string + (match lsts + ([ys] [(draw-canvas (iota (length ys)) ys + (scale-fn 0 (length ys)) + (scale-fn (apply min ys) (apply max ys)) + width: width height: height)]) + ([xs ys] [(draw-canvas xs ys + (scale-fn (apply min xs) (apply max xs)) + (scale-fn (apply min ys) (apply max ys)) + width: width height: height)]) + ([xs . yss] (let* ((x-scale-fn (scale-fn (apply min xs) (apply max xs))) (all-ys (flatten yss)) (min-ys (apply min all-ys)) @@ -39,4 +39,5 @@ (y-scale-fn (scale-fn min-ys max-ys))) (for/collect ((ys yss)) (draw-canvas xs ys x-scale-fn y-scale-fn - width: width height: height))))))) + width: width height: height))))) + colors))