org-plot.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. ;;; org-plot.el --- Support for plotting from Org-mode
  2. ;; Copyright (C) 2008 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: 6.12a
  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-and-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. ("timefmt" . :timefmt)))
  57. (multiples '("set" "line"))
  58. (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
  59. (start 0)
  60. o)
  61. (while (setq o (pop op))
  62. (if (member (car o) multiples) ;; keys with multiple values
  63. (while (string-match
  64. (concat (regexp-quote (car o)) regexp)
  65. options start)
  66. (setq start (match-end 0))
  67. (setq p (plist-put p (cdr o)
  68. (cons (car (read-from-string
  69. (match-string 1 options)))
  70. (plist-get p (cdr o)))))
  71. p)
  72. (if (string-match (concat (regexp-quote (car o)) regexp)
  73. options)
  74. (setq p (plist-put p (cdr o)
  75. (car (read-from-string
  76. (match-string 1 options)))))))))))
  77. p)
  78. (defun org-plot/goto-nearest-table ()
  79. "Move the point forward to the beginning of nearest table.
  80. Return value is the point at the beginning of the table."
  81. (interactive) (move-beginning-of-line 1)
  82. (while (not (or (org-at-table-p) (< 0 (forward-line 1)))))
  83. (goto-char (org-table-begin)))
  84. (defun org-plot/collect-options (&optional params)
  85. "Collect options from an org-plot '#+Plot:' line.
  86. Accepts an optional property list PARAMS, to which the options
  87. will be added. Returns the resulting property list."
  88. (interactive)
  89. (let ((line (thing-at-point 'line)))
  90. (if (string-match "#\\+PLOT: +\\(.*\\)$" line)
  91. (org-plot/add-options-to-plist params (match-string 1 line))
  92. params)))
  93. (defun org-plot-quote-timestamp-field (s)
  94. "Convert field S from timestamp to Unix time and export to gnuplot."
  95. (format-time-string org-plot-timestamp-fmt (org-time-string-to-time s)))
  96. (defun org-plot-quote-tsv-field (s)
  97. "Quote field S for export to gnuplot."
  98. (if (string-match org-table-number-regexp s) s
  99. (if (string-match org-ts-regexp3 s)
  100. (org-plot-quote-timestamp-field s)
  101. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  102. (defun org-plot/gnuplot-to-data (table data-file params)
  103. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  104. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  105. (with-temp-file
  106. data-file
  107. (make-local-variable 'org-plot-timestamp-fmt)
  108. (setq org-plot-timestamp-fmt (or
  109. (plist-get params :timefmt)
  110. "%Y-%m-%d-%H:%M:%S"))
  111. (insert (orgtbl-to-generic
  112. table
  113. (org-combine-plists
  114. '(:sep "\t" :fmt org-plot-quote-tsv-field)
  115. params))))
  116. nil)
  117. (defun org-plot/gnuplot-to-grid-data (table data-file params)
  118. "Export the data in TABLE to DATA-FILE for gnuplot.
  119. This means, in a format appropriate for grid plotting by gnuplot.
  120. PARAMS specifies which columns of TABLE should be plotted as independant
  121. and dependant variables."
  122. (interactive)
  123. (let* ((ind (- (plist-get params :ind) 1))
  124. (deps (if (plist-member params :deps)
  125. (mapcar (lambda (val) (- val 1)) (plist-get params :deps))
  126. (let (collector)
  127. (dotimes (col (length (first table)))
  128. (setf collector (cons col collector)))
  129. collector)))
  130. row-vals (counter 0))
  131. (when (>= ind 0) ;; collect values of ind col
  132. (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
  133. (cons counter (nth ind row))) table)))
  134. (when (or deps (>= ind 0)) ;; remove non-plotting columns
  135. (setf deps (delq ind deps))
  136. (setf table (mapcar (lambda (row)
  137. (dotimes (col (length row))
  138. (unless (memq col deps)
  139. (setf (nth col row) nil)))
  140. (delq nil row))
  141. table)))
  142. ;; write table to gnuplot grid datafile format
  143. (with-temp-file data-file
  144. (let ((num-rows (length table)) (num-cols (length (first table)))
  145. front-edge back-edge)
  146. (flet ((gnuplot-row (col row value)
  147. (setf col (+ 1 col)) (setf row (+ 1 row))
  148. (format "%f %f %f\n%f %f %f\n"
  149. col (- row 0.5) value ;; lower edge
  150. col (+ row 0.5) value))) ;; upper edge
  151. (dotimes (col num-cols)
  152. (dotimes (row num-rows)
  153. (setf back-edge
  154. (concat back-edge
  155. (gnuplot-row (- col 1) row (string-to-number
  156. (nth col (nth row table))))))
  157. (setf front-edge
  158. (concat front-edge
  159. (gnuplot-row col row (string-to-number
  160. (nth col (nth row table)))))))
  161. ;; only insert once per row
  162. (insert back-edge) (insert "\n") ;; back edge
  163. (insert front-edge) (insert "\n") ;; front edge
  164. (setf back-edge "") (setf front-edge "")))))
  165. row-vals))
  166. (defun org-plot/gnuplot-script (data-file num-cols params)
  167. "Write a gnuplot script to DATA-FILE respecting the options set in PARAMS.
  168. NUM-COLS controls the number of columns plotted in a 2-d plot."
  169. (let* ((type (plist-get params :plot-type))
  170. (with (if (equal type 'grid)
  171. 'pm3d
  172. (plist-get params :with)))
  173. (sets (plist-get params :set))
  174. (lines (plist-get params :line))
  175. (map (plist-get params :map))
  176. (title (plist-get params :title))
  177. (file (plist-get params :file))
  178. (ind (plist-get params :ind))
  179. (time-ind (plist-get params :timeind))
  180. (timefmt (plist-get params :timefmt))
  181. (text-ind (plist-get params :textind))
  182. (deps (if (plist-member params :deps) (plist-get params :deps)))
  183. (col-labels (plist-get params :labels))
  184. (x-labels (plist-get params :xlabels))
  185. (y-labels (plist-get params :ylabels))
  186. (plot-str "'%s' using %s%d%s with %s title '%s'")
  187. (plot-cmd (case type
  188. ('2d "plot")
  189. ('3d "splot")
  190. ('grid "splot")))
  191. (script "reset") plot-lines)
  192. (flet ((add-to-script (line) (setf script (format "%s\n%s" script line))))
  193. (when file ;; output file
  194. (add-to-script (format "set term %s" (file-name-extension file)))
  195. (add-to-script (format "set output '%s'" file)))
  196. (case type ;; type
  197. ('2d ())
  198. ('3d (if map (add-to-script "set map")))
  199. ('grid (if map
  200. (add-to-script "set pm3d map")
  201. (add-to-script "set pm3d"))))
  202. (when title (add-to-script (format "set title '%s'" title))) ;; title
  203. (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
  204. (when sets ;; set
  205. (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
  206. (when x-labels ;; x labels (xtics)
  207. (add-to-script
  208. (format "set xtics (%s)"
  209. (mapconcat (lambda (pair)
  210. (format "\"%s\" %d" (cdr pair) (car pair)))
  211. x-labels ", "))))
  212. (when y-labels ;; y labels (ytics)
  213. (add-to-script
  214. (format "set ytics (%s)"
  215. (mapconcat (lambda (pair)
  216. (format "\"%s\" %d" (cdr pair) (car pair)))
  217. y-labels ", "))))
  218. (when time-ind ;; timestamp index
  219. (add-to-script "set xdata time")
  220. (add-to-script (concat "set timefmt \""
  221. (or timefmt ;; timefmt passed to gnuplot
  222. "%Y-%m-%d-%H:%M:%S") "\"")))
  223. (case type ;; plot command
  224. ('2d (dotimes (col num-cols)
  225. (unless (and (equal type '2d)
  226. (or (and ind (equal (+ 1 col) ind))
  227. (and deps (not (member (+ 1 col) deps)))))
  228. (setf plot-lines
  229. (cons
  230. (format plot-str data-file
  231. (or (and (not text-ind) ind
  232. (> ind 0) (format "%d:" ind)) "")
  233. (+ 1 col)
  234. (if text-ind (format ":xticlabel(%d)" ind) "")
  235. with
  236. (or (nth col col-labels) (format "%d" (+ 1 col))))
  237. plot-lines)))))
  238. ('3d
  239. (setq plot-lines (list (format "'%s' matrix with %s title ''"
  240. data-file with))))
  241. ('grid
  242. (setq plot-lines (list (format "'%s' with %s title ''"
  243. data-file with)))))
  244. (add-to-script
  245. (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) ",\\\n ")))
  246. script)))
  247. ;;-----------------------------------------------------------------------------
  248. ;; facade functions
  249. ;;;###autoload
  250. (defun org-plot/gnuplot (&optional params)
  251. "Plot table using gnuplot. Gnuplot options can be specified with PARAMS.
  252. If not given options will be taken from the +PLOT
  253. line directly before or after the table."
  254. (interactive)
  255. (require 'gnuplot)
  256. (save-window-excursion
  257. (delete-other-windows)
  258. (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running
  259. (save-excursion
  260. (set-buffer "*gnuplot*") (goto-char (point-max))
  261. (gnuplot-delchar-or-maybe-eof nil)))
  262. (org-plot/goto-nearest-table)
  263. ;; set default options
  264. (mapc
  265. (lambda (pair)
  266. (unless (plist-member params (car pair))
  267. (setf params (plist-put params (car pair) (cdr pair)))))
  268. org-plot/gnuplot-default-options)
  269. ;; collect table and table information
  270. (let* ((data-file (make-temp-file "org-plot"))
  271. (table (org-table-to-lisp))
  272. (num-cols (length (if (eq (first table) 'hline) (second table)
  273. (first table)))))
  274. (while (equal 'hline (first table)) (setf table (cdr table)))
  275. (when (equal (second table) 'hline)
  276. (setf params (plist-put params :labels (first table))) ;; headers to labels
  277. (setf table (delq 'hline (cdr table)))) ;; clean non-data from table
  278. ;; collect options
  279. (save-excursion (while (and (equal 0 (forward-line -1))
  280. (looking-at "#\\+"))
  281. (setf params (org-plot/collect-options params))))
  282. ;; dump table to datafile (very different for grid)
  283. (case (plist-get params :plot-type)
  284. ('2d (org-plot/gnuplot-to-data table data-file params))
  285. ('3d (org-plot/gnuplot-to-data table data-file params))
  286. ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data
  287. table data-file params)))
  288. (when y-labels (plist-put params :ylabels y-labels)))))
  289. ;; check for timestamp ind column
  290. (let ((ind (- (plist-get params :ind) 1)))
  291. (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
  292. (if (= (length
  293. (delq 0 (mapcar
  294. (lambda (el)
  295. (if (string-match org-ts-regexp3 el)
  296. 0 1))
  297. (mapcar (lambda (row) (nth ind row)) table)))) 0)
  298. (plist-put params :timeind t)
  299. ;; check for text ind column
  300. (if (> (length
  301. (delq 0 (mapcar
  302. (lambda (el)
  303. (if (string-match org-table-number-regexp el)
  304. 0 1))
  305. (mapcar (lambda (row) (nth ind row)) table)))) 0)
  306. (plist-put params :textind t)))))
  307. ;; write script
  308. (with-temp-buffer
  309. (if (plist-get params :script) ;; user script
  310. (progn (insert-file-contents (plist-get params :script))
  311. (goto-char (point-min))
  312. (while (re-search-forward "$datafile" nil t)
  313. (replace-match data-file nil nil)))
  314. (insert
  315. (org-plot/gnuplot-script data-file num-cols params)))
  316. ;; graph table
  317. (gnuplot-mode)
  318. (gnuplot-send-buffer-to-gnuplot))
  319. ;; cleanup
  320. (bury-buffer (get-buffer "*gnuplot*"))
  321. (delete-file data-file))))
  322. (provide 'org-plot)
  323. ;; arch-tag: 5763f7c6-0c75-416d-b070-398ee4ec0eca
  324. ;;; org-plot.el ends here