org-plot.el 12 KB

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