* lisp/image-mode.el: Fix scaling.
[emacs.git] / lisp / org / org-plot.el
blobf44694fa781b7de23441252e5bb527ec5230cd68
1 ;;; org-plot.el --- Support for plotting from Org-mode
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Eric Schulte <schulte dot eric at gmail dot com>
6 ;; Keywords: tables, plotting
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Borrows ideas and a couple of lines of code from org-exp.el.
28 ;; Thanks to the org-mode mailing list for testing and implementation
29 ;; and feature suggestions
31 ;;; Code:
32 (require 'org)
33 (require 'org-exp)
34 (require 'org-table)
35 (eval-when-compile
36 (require 'cl))
38 (declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg))
39 (declare-function gnuplot-mode "ext:gnuplot" ())
40 (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot" ())
42 (defvar org-plot/gnuplot-default-options
43 '((:plot-type . 2d)
44 (:with . lines)
45 (:ind . 0))
46 "Default options to gnuplot used by `org-plot/gnuplot'.")
48 (defvar org-plot-timestamp-fmt nil)
50 (defun org-plot/add-options-to-plist (p options)
51 "Parse an OPTIONS line and set values in the property list P.
52 Returns the resulting property list."
53 (let (o)
54 (when options
55 (let ((op '(("type" . :plot-type)
56 ("script" . :script)
57 ("line" . :line)
58 ("set" . :set)
59 ("title" . :title)
60 ("ind" . :ind)
61 ("deps" . :deps)
62 ("with" . :with)
63 ("file" . :file)
64 ("labels" . :labels)
65 ("map" . :map)
66 ("timeind" . :timeind)
67 ("timefmt" . :timefmt)))
68 (multiples '("set" "line"))
69 (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
70 (start 0)
72 (while (setq o (pop op))
73 (if (member (car o) multiples) ;; keys with multiple values
74 (while (string-match
75 (concat (regexp-quote (car o)) regexp)
76 options start)
77 (setq start (match-end 0))
78 (setq p (plist-put p (cdr o)
79 (cons (car (read-from-string
80 (match-string 1 options)))
81 (plist-get p (cdr o)))))
83 (if (string-match (concat (regexp-quote (car o)) regexp)
84 options)
85 (setq p (plist-put p (cdr o)
86 (car (read-from-string
87 (match-string 1 options)))))))))))
90 (defun org-plot/goto-nearest-table ()
91 "Move the point forward to the beginning of nearest table.
92 Return value is the point at the beginning of the table."
93 (interactive) (move-beginning-of-line 1)
94 (while (not (or (org-at-table-p) (< 0 (forward-line 1)))))
95 (goto-char (org-table-begin)))
97 (defun org-plot/collect-options (&optional params)
98 "Collect options from an org-plot '#+Plot:' line.
99 Accepts an optional property list PARAMS, to which the options
100 will be added. Returns the resulting property list."
101 (interactive)
102 (let ((line (thing-at-point 'line)))
103 (if (string-match "#\\+PLOT: +\\(.*\\)$" line)
104 (org-plot/add-options-to-plist params (match-string 1 line))
105 params)))
107 (defun org-plot-quote-timestamp-field (s)
108 "Convert field S from timestamp to Unix time and export to gnuplot."
109 (format-time-string org-plot-timestamp-fmt (org-time-string-to-time s)))
111 (defun org-plot-quote-tsv-field (s)
112 "Quote field S for export to gnuplot."
113 (if (string-match org-table-number-regexp s) s
114 (if (string-match org-ts-regexp3 s)
115 (org-plot-quote-timestamp-field s)
116 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
118 (defun org-plot/gnuplot-to-data (table data-file params)
119 "Export TABLE to DATA-FILE in a format readable by gnuplot.
120 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
121 (with-temp-file
122 data-file
123 (make-local-variable 'org-plot-timestamp-fmt)
124 (setq org-plot-timestamp-fmt (or
125 (plist-get params :timefmt)
126 "%Y-%m-%d-%H:%M:%S"))
127 (insert (orgtbl-to-generic
128 table
129 (org-combine-plists
130 '(:sep "\t" :fmt org-plot-quote-tsv-field)
131 params))))
132 nil)
134 (defun org-plot/gnuplot-to-grid-data (table data-file params)
135 "Export the data in TABLE to DATA-FILE for gnuplot.
136 This means in a format appropriate for grid plotting by gnuplot.
137 PARAMS specifies which columns of TABLE should be plotted as independent
138 and dependant variables."
139 (interactive)
140 (let* ((ind (- (plist-get params :ind) 1))
141 (deps (if (plist-member params :deps)
142 (mapcar (lambda (val) (- val 1)) (plist-get params :deps))
143 (let (collector)
144 (dotimes (col (length (first table)))
145 (setf collector (cons col collector)))
146 collector)))
147 row-vals (counter 0))
148 (when (>= ind 0) ;; collect values of ind col
149 (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
150 (cons counter (nth ind row))) table)))
151 (when (or deps (>= ind 0)) ;; remove non-plotting columns
152 (setf deps (delq ind deps))
153 (setf table (mapcar (lambda (row)
154 (dotimes (col (length row))
155 (unless (memq col deps)
156 (setf (nth col row) nil)))
157 (delq nil row))
158 table)))
159 ;; write table to gnuplot grid datafile format
160 (with-temp-file data-file
161 (let ((num-rows (length table)) (num-cols (length (first table)))
162 front-edge back-edge)
163 (flet ((gnuplot-row (col row value)
164 (setf col (+ 1 col)) (setf row (+ 1 row))
165 (format "%f %f %f\n%f %f %f\n"
166 col (- row 0.5) value ;; lower edge
167 col (+ row 0.5) value))) ;; upper edge
168 (dotimes (col num-cols)
169 (dotimes (row num-rows)
170 (setf back-edge
171 (concat back-edge
172 (gnuplot-row (- col 1) row (string-to-number
173 (nth col (nth row table))))))
174 (setf front-edge
175 (concat front-edge
176 (gnuplot-row col row (string-to-number
177 (nth col (nth row table)))))))
178 ;; only insert once per row
179 (insert back-edge) (insert "\n") ;; back edge
180 (insert front-edge) (insert "\n") ;; front edge
181 (setf back-edge "") (setf front-edge "")))))
182 row-vals))
184 (defun org-plot/gnuplot-script (data-file num-cols params &optional preface)
185 "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS.
186 NUM-COLS controls the number of columns plotted in a 2-d plot.
187 Optional argument PREFACE returns only option parameters in a
188 manner suitable for prepending to a user-specified script."
189 (let* ((type (plist-get params :plot-type))
190 (with (if (equal type 'grid)
191 'pm3d
192 (plist-get params :with)))
193 (sets (plist-get params :set))
194 (lines (plist-get params :line))
195 (map (plist-get params :map))
196 (title (plist-get params :title))
197 (file (plist-get params :file))
198 (ind (plist-get params :ind))
199 (time-ind (plist-get params :timeind))
200 (timefmt (plist-get params :timefmt))
201 (text-ind (plist-get params :textind))
202 (deps (if (plist-member params :deps) (plist-get params :deps)))
203 (col-labels (plist-get params :labels))
204 (x-labels (plist-get params :xlabels))
205 (y-labels (plist-get params :ylabels))
206 (plot-str "'%s' using %s%d%s with %s title '%s'")
207 (plot-cmd (case type
208 ('2d "plot")
209 ('3d "splot")
210 ('grid "splot")))
211 (script "reset") plot-lines)
212 (flet ((add-to-script (line) (setf script (format "%s\n%s" script line))))
213 (when file ;; output file
214 (add-to-script (format "set term %s" (file-name-extension file)))
215 (add-to-script (format "set output '%s'" file)))
216 (case type ;; type
217 ('2d ())
218 ('3d (if map (add-to-script "set map")))
219 ('grid (if map
220 (add-to-script "set pm3d map")
221 (add-to-script "set pm3d"))))
222 (when title (add-to-script (format "set title '%s'" title))) ;; title
223 (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
224 (when sets ;; set
225 (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
226 (when x-labels ;; x labels (xtics)
227 (add-to-script
228 (format "set xtics (%s)"
229 (mapconcat (lambda (pair)
230 (format "\"%s\" %d" (cdr pair) (car pair)))
231 x-labels ", "))))
232 (when y-labels ;; y labels (ytics)
233 (add-to-script
234 (format "set ytics (%s)"
235 (mapconcat (lambda (pair)
236 (format "\"%s\" %d" (cdr pair) (car pair)))
237 y-labels ", "))))
238 (when time-ind ;; timestamp index
239 (add-to-script "set xdata time")
240 (add-to-script (concat "set timefmt \""
241 (or timefmt ;; timefmt passed to gnuplot
242 "%Y-%m-%d-%H:%M:%S") "\"")))
243 (unless preface
244 (case type ;; plot command
245 ('2d (dotimes (col num-cols)
246 (unless (and (equal type '2d)
247 (or (and ind (equal (+ 1 col) ind))
248 (and deps (not (member (+ 1 col) deps)))))
249 (setf plot-lines
250 (cons
251 (format plot-str data-file
252 (or (and ind (> ind 0)
253 (not text-ind)
254 (format "%d:" ind)) "")
255 (+ 1 col)
256 (if text-ind (format ":xticlabel(%d)" ind) "")
257 with
258 (or (nth col col-labels) (format "%d" (+ 1 col))))
259 plot-lines)))))
260 ('3d
261 (setq plot-lines (list (format "'%s' matrix with %s title ''"
262 data-file with))))
263 ('grid
264 (setq plot-lines (list (format "'%s' with %s title ''"
265 data-file with)))))
266 (add-to-script
267 (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n "))))
268 script)))
270 ;;-----------------------------------------------------------------------------
271 ;; facade functions
272 ;;;###autoload
273 (defun org-plot/gnuplot (&optional params)
274 "Plot table using gnuplot. Gnuplot options can be specified with PARAMS.
275 If not given options will be taken from the +PLOT
276 line directly before or after the table."
277 (interactive)
278 (require 'gnuplot)
279 (save-window-excursion
280 (delete-other-windows)
281 (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running
282 (with-current-buffer "*gnuplot*"
283 (goto-char (point-max))
284 (gnuplot-delchar-or-maybe-eof nil)))
285 (org-plot/goto-nearest-table)
286 ;; set default options
287 (mapc
288 (lambda (pair)
289 (unless (plist-member params (car pair))
290 (setf params (plist-put params (car pair) (cdr pair)))))
291 org-plot/gnuplot-default-options)
292 ;; collect table and table information
293 (let* ((data-file (make-temp-file "org-plot"))
294 (table (org-table-to-lisp))
295 (num-cols (length (if (eq (first table) 'hline) (second table)
296 (first table)))))
297 (while (equal 'hline (first table)) (setf table (cdr table)))
298 (when (equal (second table) 'hline)
299 (setf params (plist-put params :labels (first table))) ;; headers to labels
300 (setf table (delq 'hline (cdr table)))) ;; clean non-data from table
301 ;; collect options
302 (save-excursion (while (and (equal 0 (forward-line -1))
303 (looking-at "[[:space:]]*#\\+"))
304 (setf params (org-plot/collect-options params))))
305 ;; dump table to datafile (very different for grid)
306 (case (plist-get params :plot-type)
307 ('2d (org-plot/gnuplot-to-data table data-file params))
308 ('3d (org-plot/gnuplot-to-data table data-file params))
309 ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data
310 table data-file params)))
311 (when y-labels (plist-put params :ylabels y-labels)))))
312 ;; check for timestamp ind column
313 (let ((ind (- (plist-get params :ind) 1)))
314 (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
315 (if (= (length
316 (delq 0 (mapcar
317 (lambda (el)
318 (if (string-match org-ts-regexp3 el)
319 0 1))
320 (mapcar (lambda (row) (nth ind row)) table)))) 0)
321 (plist-put params :timeind t)
322 ;; check for text ind column
323 (if (or (string= (plist-get params :with) "hist")
324 (> (length
325 (delq 0 (mapcar
326 (lambda (el)
327 (if (string-match org-table-number-regexp el)
328 0 1))
329 (mapcar (lambda (row) (nth ind row)) table)))) 0))
330 (plist-put params :textind t)))))
331 ;; write script
332 (with-temp-buffer
333 (if (plist-get params :script) ;; user script
334 (progn (insert
335 (org-plot/gnuplot-script data-file num-cols params t))
336 (insert "\n")
337 (insert-file-contents (plist-get params :script))
338 (goto-char (point-min))
339 (while (re-search-forward "$datafile" nil t)
340 (replace-match data-file nil nil)))
341 (insert
342 (org-plot/gnuplot-script data-file num-cols params)))
343 ;; graph table
344 (gnuplot-mode)
345 (gnuplot-send-buffer-to-gnuplot))
346 ;; cleanup
347 (bury-buffer (get-buffer "*gnuplot*"))
348 (run-with-idle-timer 0.1 nil (lambda () (delete-file data-file))))))
350 (provide 'org-plot)
352 ;;; org-plot.el ends here