Function: org-plot/gnuplot-script

org-plot/gnuplot-script is a byte-compiled function defined in org-plot.el.gz.

Signature

(org-plot/gnuplot-script TABLE DATA-FILE NUM-COLS PARAMS &optional PREFACE)

Documentation

Write a gnuplot script for TABLE to DATA-FILE respecting options in PARAMS.

NUM-COLS controls the number of columns plotted in a 2-d plot. Optional argument PREFACE returns only option parameters in a manner suitable for prepending to a user-specified script.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-plot.el.gz
(defun org-plot/gnuplot-script (table data-file num-cols params &optional preface)
  "Write a gnuplot script for TABLE to DATA-FILE respecting options in PARAMS.
NUM-COLS controls the number of columns plotted in a 2-d plot.
Optional argument PREFACE returns only option parameters in a
manner suitable for prepending to a user-specified script."
  (let* ((type-name (plist-get params :plot-type))
	 (type (cdr (assoc type-name org-plot/preset-plot-types))))
    (unless type
      (user-error "Org-plot type `%s' is undefined" type-name))
    (let* ((sets (plist-get params :set))
	   (lines (plist-get params :line))
	   (title (plist-get params :title))
	   (file (plist-get params :file))
	   (time-ind (plist-get params :timeind))
	   (timefmt (plist-get params :timefmt))
	   (x-labels (plist-get params :xlabels))
	   (y-labels (plist-get params :ylabels))
	   (plot-str (or (plist-get type :plot-str)
			 "'%s' using %s%d%s with %s title '%s'"))
	   (plot-cmd (plist-get type :plot-cmd))
	   (plot-pre (plist-get type :plot-pre))
	   (script "reset")
	   ;; ats = add-to-script
	   (ats (lambda (line) (when line (setf script (concat script "\n" line)))))
	   plot-lines)


      ;; handle output file, background, and size
      (funcall ats (format "set term %s %s"
			   (if file (file-name-extension file) "GNUTERM")
			   (if (stringp org-plot/gnuplot-term-extra)
			       org-plot/gnuplot-term-extra
			     (funcall org-plot/gnuplot-term-extra type))))
      (when file ; output file
	(funcall ats (format "set output '%s'" (expand-file-name file))))

      (when plot-pre
	(funcall ats (funcall plot-pre table data-file num-cols params plot-str)))

      (funcall ats
	       (if (stringp org-plot/gnuplot-script-preamble)
		   org-plot/gnuplot-script-preamble
		 (funcall org-plot/gnuplot-script-preamble type)))

      (when title (funcall ats (format "set title '%s'" title))) ; title
      (mapc ats lines)					       ; line
      (dolist (el sets) (funcall ats (format "set %s" el)))      ; set
      ;; Unless specified otherwise, values are TAB separated.
      (unless (string-match-p "^set datafile separator" script)
	(funcall ats "set datafile separator \"\\t\""))
      (when x-labels			; x labels (xtics)
	(funcall ats
		 (format "set xtics (%s)"
			 (mapconcat (lambda (pair)
				      (format "\"%s\" %d" (cdr pair) (car pair)))
				    x-labels ", "))))
      (when y-labels			; y labels (ytics)
	(funcall ats
		 (format "set ytics (%s)"
			 (mapconcat (lambda (pair)
				      (format "\"%s\" %d" (cdr pair) (car pair)))
				    y-labels ", "))))
      (when time-ind			; timestamp index
	(funcall ats "set xdata time")
	(funcall ats (concat "set timefmt \""
			     (or timefmt	; timefmt passed to gnuplot
				 "%Y-%m-%d-%H:%M:%S") "\"")))
      (unless preface
	(let ((type-func (plist-get type :plot-func)))
	  (when type-func
	    (setq plot-lines
		  (funcall type-func table data-file num-cols params plot-str))))
	(funcall ats
		 (concat plot-cmd
			 (when plot-cmd " ")
			 (mapconcat #'identity
				    (reverse plot-lines)
				    ",\\\n    "))))
      script)))