org-plot.el 13 KB

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