org-plot.el 13 KB

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