Function: org-plot/gnuplot

org-plot/gnuplot is an autoloaded, interactive and byte-compiled function defined in org-plot.el.gz.

Signature

(org-plot/gnuplot &optional PARAMS)

Documentation

Plot table using gnuplot. Gnuplot options can be specified with PARAMS.

If not given options will be taken from the +PLOT line directly before or after the table.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-plot.el.gz
;;-----------------------------------------------------------------------------
;; facade functions
;;;###autoload
(defun org-plot/gnuplot (&optional params)
  "Plot table using gnuplot.  Gnuplot options can be specified with PARAMS.
If not given options will be taken from the +PLOT
line directly before or after the table."
  (interactive)
  (require 'gnuplot)
  (save-window-excursion
    (delete-other-windows)
    (when (get-buffer "*gnuplot*") ; reset *gnuplot* if it already running
      (with-current-buffer "*gnuplot*"
	(goto-char (point-max))))
    (save-excursion
      (org-plot/goto-nearest-table)
      ;; Set default options.
      (dolist (pair org-plot/gnuplot-default-options)
        (unless (plist-member params (car pair))
          (setf params (plist-put params (car pair) (cdr pair)))))
      ;; Collect options.
      (while (and (equal 0 (forward-line -1))
                  (looking-at "[[:space:]]*#\\+"))
        (setf params (org-plot/collect-options params))))
    ;; collect table and table information
    (let* ((data-file (make-temp-file "org-plot"))
           (table (let ((tbl (save-excursion
                               (org-plot/goto-nearest-table)
                               (org-table-to-lisp))))
		    (when (pcase (plist-get params :transpose)
			    (`y   t)
			    (`yes t)
			    (`t   t))
		      (if (not (memq 'hline tbl))
			  (setq tbl (apply #'cl-mapcar #'list tbl))
			;; When present, remove hlines as they can't (currentily) be easily transposed.
			(setq tbl (apply #'cl-mapcar #'list
					 (remove 'hline tbl)))
			(push 'hline (cdr tbl))))
		    tbl))
	   (num-cols (length (if (eq (nth 0 table) 'hline) (nth 1 table)
			       (nth 0 table))))
	   (type (assoc (plist-get params :plot-type)
			org-plot/preset-plot-types))
           gnuplot-script)

      (unless type
	(user-error "Org-plot type `%s' is undefined" (plist-get params :plot-type)))

      (run-with-idle-timer 0.1 nil #'delete-file data-file)
      (when (eq (cadr table) 'hline)
	(setf params
	      (plist-put params :labels (car table))) ; headers to labels
	(setf table (delq 'hline (cdr table)))) ; clean non-data from table
      ;; Collect options.
      (save-excursion (while (and (equal 0 (forward-line -1))
				  (looking-at "[[:space:]]*#\\+"))
			(setf params (org-plot/collect-options params))))
      ;; Dump table to datafile
      (let ((dump-func (plist-get type :data-dump)))
        (if dump-func
	    (funcall dump-func table data-file num-cols params)
	  (org-plot/gnuplot-to-data table data-file params)))
      ;; Check type of ind column (timestamp? text?)
      (when (plist-get params :check-ind-type)
	(let* ((ind (1- (plist-get params :ind)))
	       (ind-column (mapcar (lambda (row) (nth ind row)) table)))
	  (cond ((< ind 0) nil) ; ind is implicit
		((cl-every (lambda (el)
			     (string-match org-ts-regexp3 el))
			   ind-column)
		 (plist-put params :timeind t)) ; ind holds timestamps
		((or (string= (plist-get params :with) "hist")
		     (cl-notevery (lambda (el)
				    (string-match org-table-number-regexp el))
				  ind-column))
		 (plist-put params :textind t))))) ; ind holds text
      ;; Write script.
      (setq gnuplot-script
            (org-plot/gnuplot-script
             table data-file num-cols params (plist-get params :script)))
      (with-temp-buffer
	(if (plist-get params :script)	; user script
	    (progn (insert gnuplot-script "\n")
		   (insert-file-contents (plist-get params :script))
		   (goto-char (point-min))
		   (while (re-search-forward "\\$datafile" nil t)
		     (replace-match data-file nil nil)))
	  (insert gnuplot-script))
	;; Graph table.
	(gnuplot-mode)
        (condition-case nil
            (gnuplot-send-buffer-to-gnuplot)
          (buffer-read-only nil)))
      ;; Cleanup.
      (bury-buffer (get-buffer "*gnuplot*"))
      ;; Refresh any displayed images
      (when (plist-get params :file)
        (org-plot/redisplay-img-in-buffer (expand-file-name (plist-get params :file)))))))