org-plot.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.06b
  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. (defun org-plot/add-options-to-plist (p options)
  39. "Parse an OPTONS line and set values in the property list P."
  40. (let (o)
  41. (when options
  42. (let ((op '(("type" . :plot-type)
  43. ("script" . :script)
  44. ("line" . :line)
  45. ("set" . :set)
  46. ("title" . :title)
  47. ("ind" . :ind)
  48. ("deps" . :deps)
  49. ("with" . :with)
  50. ("file" . :file)
  51. ("labels" . :labels)
  52. ("map" . :map)))
  53. (multiples '("set" "line"))
  54. (regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
  55. (start 0)
  56. o)
  57. (while (setq o (pop 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 to the beginning of nearest table. First look
  76. back until hitting an empty line, then forward until a table is
  77. found."
  78. (interactive) (move-beginning-of-line 1)
  79. (while (not (or (org-at-table-p) (< 0 (forward-line 1)))))
  80. (goto-char (org-table-begin)))
  81. (defun org-plot/collect-options (&optional params)
  82. (interactive)
  83. (let ((line (thing-at-point 'line)))
  84. (if (string-match "#\\+PLOT: +\\(.*\\)$" line)
  85. (org-plot/add-options-to-plist params (match-string 1 line))
  86. params)))
  87. (defun org-plot-quote-tsv-field (s)
  88. "Quote field for export to gnuplot."
  89. (if (string-match org-table-number-regexp s) s
  90. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")))
  91. (defun org-plot/gnuplot-to-data (table data-file params)
  92. (with-temp-file
  93. data-file (insert (orgtbl-to-generic
  94. table
  95. (org-combine-plists
  96. '(:sep "\t" :fmt org-plot-quote-tsv-field)
  97. params))))
  98. nil)
  99. (defun org-plot/gnuplot-to-grid-data (table data-file params)
  100. (interactive)
  101. (let* ((ind (- (plist-get params :ind) 1))
  102. (deps (if (plist-member params :deps)
  103. (mapcar (lambda (val) (- val 1)) (plist-get params :deps))
  104. (let (collector)
  105. (dotimes (col (length (first table)))
  106. (setf collector (cons col collector)))
  107. collector)))
  108. row-vals (counter 0))
  109. (when (>= ind 0) ;; collect values of ind col
  110. (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
  111. (cons counter (nth ind row))) table)))
  112. (when (or deps (>= ind 0)) ;; remove non-plotting columns
  113. (setf deps (delq ind deps))
  114. (setf table (mapcar (lambda (row)
  115. (dotimes (col (length row))
  116. (unless (memq col deps)
  117. (setf (nth col row) nil)))
  118. (delq nil row))
  119. table)))
  120. ;; write table to gnuplot grid datafile format
  121. (with-temp-file data-file
  122. (let ((num-rows (length table)) (num-cols (length (first table)))
  123. front-edge back-edge)
  124. (flet ((gnuplot-row (col row value)
  125. (setf col (+ 1 col)) (setf row (+ 1 row))
  126. (format "%f %f %f\n%f %f %f\n"
  127. col (- row 0.5) value ;; lower edge
  128. col (+ row 0.5) value))) ;; upper edge
  129. (dotimes (col num-cols)
  130. (dotimes (row num-rows)
  131. (setf back-edge
  132. (concat back-edge
  133. (gnuplot-row (- col 1) row (string-to-number
  134. (nth col (nth row table))))))
  135. (setf front-edge
  136. (concat front-edge
  137. (gnuplot-row col row (string-to-number
  138. (nth col (nth row table)))))))
  139. ;; only insert once per row
  140. (insert back-edge) (insert "\n") ;; back edge
  141. (insert front-edge) (insert "\n") ;; front edge
  142. (setf back-edge "") (setf front-edge "")))))
  143. row-vals))
  144. (defun org-plot/gnuplot-script (data-file num-cols params)
  145. (let* ((type (plist-get params :plot-type))
  146. (with (if (equal type 'grid)
  147. 'pm3d
  148. (plist-get params :with)))
  149. (sets (plist-get params :set))
  150. (lines (plist-get params :line))
  151. (map (plist-get params :map))
  152. (title (plist-get params :title))
  153. (file (plist-get params :file))
  154. (ind (plist-get params :ind))
  155. (text-ind (plist-get params :textind))
  156. (deps (if (plist-member params :deps) (plist-get params :deps)))
  157. (col-labels (plist-get params :labels))
  158. (x-labels (plist-get params :xlabels))
  159. (y-labels (plist-get params :ylabels))
  160. (plot-str "'%s' using %s%d%s with %s title '%s'")
  161. (plot-cmd (case type
  162. ('2d "plot")
  163. ('3d "splot")
  164. ('grid "splot")))
  165. (script "reset") plot-lines)
  166. (flet ((add-to-script (line) (setf script (format "%s\n%s" script line))))
  167. (when file ;; output file
  168. (add-to-script (format "set term %s" (file-name-extension file)))
  169. (add-to-script (format "set output '%s'" file)))
  170. (case type ;; type
  171. ('2d ())
  172. ('3d (if map (add-to-script "set map")))
  173. ('grid (if map
  174. (add-to-script "set pm3d map")
  175. (add-to-script "set pm3d"))))
  176. (when title (add-to-script (format "set title '%s'" title))) ;; title
  177. (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
  178. (when sets ;; set
  179. (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
  180. (when x-labels ;; x labels (xtics)
  181. (add-to-script
  182. (format "set xtics (%s)"
  183. (mapconcat (lambda (pair)
  184. (format "\"%s\" %d" (cdr pair) (car pair)))
  185. x-labels ", "))))
  186. (when y-labels ;; y labels (ytics)
  187. (add-to-script
  188. (format "set ytics (%s)"
  189. (mapconcat (lambda (pair)
  190. (format "\"%s\" %d" (cdr pair) (car pair)))
  191. y-labels ", "))))
  192. (case type ;; plot command
  193. ('2d (dotimes (col num-cols)
  194. (unless (and (equal type '2d)
  195. (or (and ind (equal (+ 1 col) ind))
  196. (and deps (not (member (+ 1 col) deps)))))
  197. (setf plot-lines
  198. (cons
  199. (format plot-str data-file
  200. (or (and (not text-ind) ind
  201. (> ind 0) (format "%d:" ind)) "")
  202. (+ 1 col)
  203. (if text-ind (format ":xticlabel(%d)" ind) "")
  204. with
  205. (or (nth col col-labels) (format "%d" (+ 1 col))))
  206. plot-lines)))))
  207. ('3d
  208. (setq plot-lines (list (format "'%s' matrix with %s title ''"
  209. data-file with))))
  210. ('grid
  211. (setq plot-lines (list (format "'%s' with %s title ''"
  212. data-file with)))))
  213. (add-to-script
  214. (concat plot-cmd " " (mapconcat 'identity (reverse plot-lines) "\\\n ,")))
  215. script)))
  216. ;;-----------------------------------------------------------------------------
  217. ;; facad functions
  218. ;;;###autoload
  219. (defun org-plot/gnuplot (&optional params)
  220. "Plot table using gnuplot. Gnuplot options can be specified
  221. with PARAMS. If not given options will be taken from the +PLOT
  222. line directly before or after the table."
  223. (interactive)
  224. (require 'gnuplot)
  225. (save-window-excursion
  226. (delete-other-windows)
  227. (when (get-buffer "*gnuplot*") ;; reset *gnuplot* if it already running
  228. (save-excursion
  229. (set-buffer "*gnuplot*") (goto-char (point-max))
  230. (gnuplot-delchar-or-maybe-eof nil)))
  231. (org-plot/goto-nearest-table)
  232. ;; set default options
  233. (mapc
  234. (lambda (pair)
  235. (unless (plist-member params (car pair))
  236. (setf params (plist-put params (car pair) (cdr pair)))))
  237. org-plot/gnuplot-default-options)
  238. ;; collect table and table information
  239. (let* ((data-file (make-temp-file "org-plot"))
  240. (table (org-table-to-lisp))
  241. (num-cols (length (first table))))
  242. (while (equal 'hline (first table)) (setf table (cdr table)))
  243. (when (equal (second table) 'hline)
  244. (setf params (plist-put params :labels (first table))) ;; headers to labels
  245. (setf table (delq 'hline (cdr table)))) ;; clean non-data from table
  246. ;; collect options
  247. (save-excursion (while (and (equal 0 (forward-line -1))
  248. (looking-at "#\\+"))
  249. (setf params (org-plot/collect-options params))))
  250. ;; dump table to datafile (very different for grid)
  251. (case (plist-get params :plot-type)
  252. ('2d (org-plot/gnuplot-to-data table data-file params))
  253. ('3d (org-plot/gnuplot-to-data table data-file params))
  254. ('grid (let ((y-labels (org-plot/gnuplot-to-grid-data
  255. table data-file params)))
  256. (when y-labels (plist-put params :ylabels y-labels)))))
  257. ;; check for text ind column
  258. (let ((ind (- (plist-get params :ind) 1)))
  259. (when (and (>= ind 0) (equal '2d (plist-get params :plot-type)))
  260. (if (> (length
  261. (delq 0 (mapcar
  262. (lambda (el)
  263. (if (string-match org-table-number-regexp el)
  264. 0 1))
  265. (mapcar (lambda (row) (nth ind row)) table)))) 0)
  266. (plist-put params :textind t))))
  267. ;; write script
  268. (with-temp-buffer
  269. (if (plist-get params :script) ;; user script
  270. (progn (insert-file-contents)
  271. (goto-char (point-min))
  272. (while (re-search-forward "$datafile" nil t)
  273. (replace-match data-file nil nil)))
  274. (insert
  275. (org-plot/gnuplot-script data-file num-cols params)))
  276. ;; graph table
  277. (gnuplot-mode)
  278. (gnuplot-send-buffer-to-gnuplot))
  279. ;; cleanup
  280. (bury-buffer (get-buffer "*gnuplot*"))(delete-file data-file))))
  281. (provide 'org-plot)
  282. ;;; org-plot.el ends here