summaryrefslogtreecommitdiffstats
path: root/charplot.scm
diff options
context:
space:
mode:
Diffstat (limited to 'charplot.scm')
-rw-r--r--charplot.scm26
1 files changed, 22 insertions, 4 deletions
diff --git a/charplot.scm b/charplot.scm
index 2a2a49a..2c64615 100644
--- a/charplot.scm
+++ b/charplot.scm
@@ -18,6 +18,7 @@
;each case.
(require 'sort)
+(require 'printf)
(define charplot:rows 24)
(define charplot:columns (output-port-width (current-output-port)))
@@ -43,6 +44,9 @@
(display str)
(charplot:printn! (- width (+ (string-length str) lpad)) #\ )))
+(define (charplot:number->string x)
+ (sprintf #f "%g" x))
+
(define (scale-it z scale)
(if (and (exact? z) (integer? z))
(quotient (* z (car scale)) (cadr scale))
@@ -86,8 +90,8 @@
(string-set! a (caar data) charplot:curve1)
(set! data (cdr data)))
(if (zero? (modulo (- ht xaxis) ystep))
- (let* ((v (number->string (/ (* (- ht xaxis) (cadr yscale))
- (car yscale))))
+ (let* ((v (charplot:number->string (/ (* (- ht xaxis) (cadr yscale))
+ (car yscale))))
(l (string-length v)))
(if (> l 10)
(display (substring v 0 10))
@@ -119,12 +123,16 @@
(charplot:center-print! xlabel (+ 12 fudge (- xstep/2)))
(do ((i fudge (+ i xstep)))
((> (+ i xstep) charplot:width))
- (charplot:center-print! (number->string (/ (* (- i yaxis) (cadr xscale))
- (car xscale)))
+ (charplot:center-print! (charplot:number->string
+ (/ (* (- i yaxis) (cadr xscale))
+ (car xscale)))
xstep))
(newline)))
(define (charplot:plot! data xlabel ylabel)
+ (cond ((array? data)
+ (set! data (map (lambda (lst) (cons (car lst) (cadr lst)))
+ (array->list data)))))
(let* ((xmax (apply max (map car data)))
(xmin (apply min (map car data)))
(xscale (find-scale charplot:width (- xmax xmin)))
@@ -139,4 +147,14 @@
data)
xlabel ylabel xmin xscale ymin yscale)))
+(define (plot-function func vlo vhi . npts)
+ (set! npts (if (null? npts) 100 (car npts)))
+ (let ((dats (make-array 0.0 npts 2)))
+ (array-index-map! (make-shared-array dats (lambda (idx) (list idx 0)) npts)
+ (lambda (idx) (+ vlo (* (- vhi vlo) (/ idx npts)))))
+ (array-map! (make-shared-array dats (lambda (idx) (list idx 1)) npts)
+ func
+ (make-shared-array dats (lambda (idx) (list idx 0)) npts))
+ (charplot:plot! dats "" "")))
+
(define plot! charplot:plot!)