;;; org-plot.el --- Support for plotting from Org-mode
-;; Copyright (C) 2008 Free Software Foundation, Inc.
+;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
;;
;; Author: Eric Schulte <schulte dot eric at gmail dot com>
;; Keywords: tables, plotting
;; Homepage: http://orgmode.org
-;; Version: 6.12a
+;; Version: 7.4
;;
;; This file is part of GNU Emacs.
;;
(require 'org)
(require 'org-exp)
(require 'org-table)
-(eval-and-compile
+(eval-when-compile
(require 'cl))
(declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg))
'((:plot-type . 2d)
(:with . lines)
(:ind . 0))
- "Default options to gnuplot used by `org-plot/gnuplot'")
+ "Default options to gnuplot used by `org-plot/gnuplot'.")
(defvar org-plot-timestamp-fmt nil)
("file" . :file)
("labels" . :labels)
("map" . :map)
- ("timefmt" . :timefmt)))
+ ("timeind" . :timeind)
+ ("timefmt" . :timefmt)))
(multiples '("set" "line"))
(regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
(start 0)
"Quote field S for export to gnuplot."
(if (string-match org-table-number-regexp s) s
(if (string-match org-ts-regexp3 s)
- (org-plot-quote-timestamp-field s)
+ (org-plot-quote-timestamp-field s)
(concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
(defun org-plot/gnuplot-to-data (table data-file params)
"Export TABLE to DATA-FILE in a format readable by gnuplot.
Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
(with-temp-file
- data-file
+ data-file
(make-local-variable 'org-plot-timestamp-fmt)
(setq org-plot-timestamp-fmt (or
- (plist-get params :timefmt)
- "%Y-%m-%d-%H:%M:%S"))
+ (plist-get params :timefmt)
+ "%Y-%m-%d-%H:%M:%S"))
(insert (orgtbl-to-generic
- table
- (org-combine-plists
- '(:sep "\t" :fmt org-plot-quote-tsv-field)
- params))))
+ table
+ (org-combine-plists
+ '(:sep "\t" :fmt org-plot-quote-tsv-field)
+ params))))
nil)
(defun org-plot/gnuplot-to-grid-data (table data-file params)
"Export the data in TABLE to DATA-FILE for gnuplot.
-This means, in a format appropriate for grid plotting by gnuplot.
-PARAMS specifies which columns of TABLE should be plotted as independant
+This means in a format appropriate for grid plotting by gnuplot.
+PARAMS specifies which columns of TABLE should be plotted as independent
and dependant variables."
(interactive)
(let* ((ind (- (plist-get params :ind) 1))
(setf back-edge "") (setf front-edge "")))))
row-vals))
-(defun org-plot/gnuplot-script (data-file num-cols params)
+(defun org-plot/gnuplot-script (data-file num-cols params &optional preface)
"Write a gnuplot script to DATA-FILE respecting the options set in PARAMS.
-NUM-COLS controls the number of columns plotted in a 2-d plot."
+NUM-COLS controls the number of columns plotted in a 2-d plot.
+Optional argument PREFACE returns only option parameters in a
+manner suitable for prepending to a user-specified script."
(let* ((type (plist-get params :plot-type))
(with (if (equal type 'grid)
'pm3d
(title (plist-get params :title))
(file (plist-get params :file))
(ind (plist-get params :ind))
- (time-ind (plist-get params :timeind))
- (timefmt (plist-get params :timefmt))
+ (time-ind (plist-get params :timeind))
+ (timefmt (plist-get params :timefmt))
(text-ind (plist-get params :textind))
(deps (if (plist-member params :deps) (plist-get params :deps)))
(col-labels (plist-get params :labels))
(format "\"%s\" %d" (cdr pair) (car pair)))
y-labels ", "))))
(when time-ind ;; timestamp index
- (add-to-script "set xdata time")
- (add-to-script (concat "set timefmt \""
- (or timefmt ;; timefmt passed to gnuplot
- "%Y-%m-%d-%H:%M:%S") "\"")))
- (case type ;; plot command
+ (add-to-script "set xdata time")
+ (add-to-script (concat "set timefmt \""
+ (or timefmt ;; timefmt passed to gnuplot
+ "%Y-%m-%d-%H:%M:%S") "\"")))
+ (unless preface
+ (case type ;; plot command
('2d (dotimes (col num-cols)
(unless (and (equal type '2d)
(or (and ind (equal (+ 1 col) ind))
(setf plot-lines
(cons
(format plot-str data-file
- (or (and (not text-ind) ind
- (> ind 0) (format "%d:" ind)) "")
+ (or (and ind (> ind 0)
+ (not text-ind)
+ (format "%d:" ind)) "")
(+ 1 col)
(if text-ind (format ":xticlabel(%d)" ind) "")
with
('grid
(setq plot-lines (list (format "'%s' with %s title ''"
data-file with)))))
- (add-to-script
- (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n ")))
+ (add-to-script
+ (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n "))))
script)))
;;-----------------------------------------------------------------------------
;; facade functions
;;;###autoload
(defun org-plot/gnuplot (&optional params)
- "Plot table using gnuplot. Gnuplot options can be specified with PARAMS.
+ "Plot table using gnuplot. Gnuplot options can be specified with PARAMS.
If not given options will be taken from the +PLOT
line directly before or after the table."
(interactive)
(save-window-excursion
(delete-other-windows)
(when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running
- (save-excursion
- (set-buffer "*gnuplot*") (goto-char (point-max))
+ (with-current-buffer "*gnuplot*"
+ (goto-char (point-max))
(gnuplot-delchar-or-maybe-eof nil)))
(org-plot/goto-nearest-table)
;; set default options
(setf table (delq 'hline (cdr table)))) ;; clean non-data from table
;; collect options
(save-excursion (while (and (equal 0 (forward-line -1))
- (looking-at "#\\+"))
+ (looking-at "[[:space:]]*#\\+"))
(setf params (org-plot/collect-options params))))
;; dump table to datafile (very different for grid)
(case (plist-get params :plot-type)
(when y-labels (plist-put params :ylabels y-labels)))))
;; check for timestamp ind column
(let ((ind (- (plist-get params :ind) 1)))
- (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
- (if (= (length
- (delq 0 (mapcar
+ (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
+ (if (= (length
+ (delq 0 (mapcar
(lambda (el)
(if (string-match org-ts-regexp3 el)
0 1))
(mapcar (lambda (row) (nth ind row)) table)))) 0)
(plist-put params :timeind t)
- ;; check for text ind column
- (if (> (length
- (delq 0 (mapcar
- (lambda (el)
- (if (string-match org-table-number-regexp el)
- 0 1))
- (mapcar (lambda (row) (nth ind row)) table)))) 0)
- (plist-put params :textind t)))))
+ ;; check for text ind column
+ (if (or (string= (plist-get params :with) "hist")
+ (> (length
+ (delq 0 (mapcar
+ (lambda (el)
+ (if (string-match org-table-number-regexp el)
+ 0 1))
+ (mapcar (lambda (row) (nth ind row)) table)))) 0))
+ (plist-put params :textind t)))))
;; write script
(with-temp-buffer
(if (plist-get params :script) ;; user script
- (progn (insert-file-contents (plist-get params :script))
- (goto-char (point-min))
- (while (re-search-forward "$datafile" nil t)
- (replace-match data-file nil nil)))
+ (progn (insert
+ (org-plot/gnuplot-script data-file num-cols params t))
+ (insert "\n")
+ (insert-file-contents (plist-get params :script))
+ (goto-char (point-min))
+ (while (re-search-forward "$datafile" nil t)
+ (replace-match data-file nil nil)))
(insert
(org-plot/gnuplot-script data-file num-cols params)))
;; graph table
(gnuplot-send-buffer-to-gnuplot))
;; cleanup
(bury-buffer (get-buffer "*gnuplot*"))
- (delete-file data-file))))
+ (run-with-idle-timer 0.1 nil (lambda () (delete-file data-file))))))
(provide 'org-plot)
-;; arch-tag: 5763f7c6-0c75-416d-b070-398ee4ec0eca
;;; org-plot.el ends here