2012-01-12 23:48:28 +01:00
|
|
|
;;
|
|
|
|
;; Copyright (C) 2011-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
;;
|
|
|
|
;; This program is free software; you can redistribute it and/or modify it
|
|
|
|
;; under the terms of the GNU General Public License as published by the
|
|
|
|
;; Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
;; later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program; if not, write to the Free Software Foundation,
|
|
|
|
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
(define-module (mu plot)
|
2012-07-14 11:46:42 +02:00
|
|
|
:use-module (mu)
|
2012-01-12 23:48:28 +01:00
|
|
|
:use-module (ice-9 popen)
|
2012-10-27 13:43:35 +02:00
|
|
|
:export ( mu:plot ;; alias for mu:plot-histogram
|
|
|
|
mu:plot-histogram
|
|
|
|
))
|
2012-01-12 23:48:28 +01:00
|
|
|
|
|
|
|
(define (export-pairs pairs)
|
|
|
|
"Write a temporary file with the list of PAIRS in table format, and
|
|
|
|
return the file name."
|
|
|
|
(let* ((datafile (tmpnam))
|
|
|
|
(output (open datafile (logior O_CREAT O_WRONLY) #O0600)))
|
|
|
|
(for-each
|
|
|
|
(lambda(pair)
|
|
|
|
(display (format #f "~a ~a\n" (car pair) (cdr pair)) output))
|
|
|
|
pairs)
|
|
|
|
(close output)
|
|
|
|
datafile))
|
|
|
|
|
2012-10-27 13:43:35 +02:00
|
|
|
(define (find-program-in-path prog)
|
|
|
|
"Find exutable program PROG in PATH; return the full path, or #f if
|
|
|
|
not found."
|
|
|
|
(let* ((path (getenv "PATH"))
|
|
|
|
(progdir (search-path path prog)))
|
|
|
|
(if (not prog)
|
|
|
|
#f
|
|
|
|
(let ((fullpath (string-append progdir "/" prog)))
|
|
|
|
(if (access? fullpath X_OK) ;; is
|
|
|
|
fullpath
|
|
|
|
#f)))))
|
|
|
|
|
|
|
|
|
|
|
|
(define* (mu:plot-histogram data title x-label y-label #:optional (text-only #f))
|
2012-10-18 23:50:22 +02:00
|
|
|
"Plot DATA with TITLE, X-LABEL and X-LABEL. If TEXT-ONLY is true,
|
2012-10-27 13:43:35 +02:00
|
|
|
display using raw text, otherwise, use a graphical window. DATA is a
|
|
|
|
list of cons-pairs (X . Y)."
|
|
|
|
(if (not (find-program-in-path "gnuplot"))
|
|
|
|
(error "cannot find 'gnuplot' in path"))
|
|
|
|
|
2012-01-12 23:48:28 +01:00
|
|
|
(let ((datafile (export-pairs data))
|
|
|
|
(gnuplot (open-pipe "gnuplot -p" OPEN_WRITE)))
|
|
|
|
(display (string-append
|
|
|
|
"reset\n"
|
2012-10-18 23:50:22 +02:00
|
|
|
"set term " (if text-only "dumb" "wxt") "\n"
|
2012-01-12 23:48:28 +01:00
|
|
|
"set title \"" title "\"\n"
|
|
|
|
"set xlabel \"" x-label "\"\n"
|
|
|
|
"set ylabel \"" y-label "\"\n"
|
|
|
|
"set boxwidth 0.9\n"
|
|
|
|
"plot \"" datafile "\" using 2:xticlabels(1) with boxes fs solid\n")
|
|
|
|
gnuplot)
|
2012-10-18 23:50:22 +02:00
|
|
|
(close-pipe gnuplot)))
|
2012-10-27 13:43:35 +02:00
|
|
|
|
|
|
|
;; backward compatibility
|
|
|
|
(define mu-plot mu:plot-histogram)
|