2016-06-21 21:19:11 +00:00
|
|
|
;;; org-plot.el --- Support for Plotting from Org -*- lexical-binding: t; -*-
|
2008-10-12 13:25:56 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 2008-2024 Free Software Foundation, Inc.
|
2008-10-12 13:25:56 +00:00
|
|
|
;;
|
|
|
|
;; Author: Eric Schulte <schulte dot eric at gmail dot com>
|
2022-07-27 15:08:54 +00:00
|
|
|
;; Maintainer: TEC <orgmode@tec.tecosaur.net>
|
2008-10-12 13:25:56 +00:00
|
|
|
;; Keywords: tables, plotting
|
2021-09-26 07:44:29 +00:00
|
|
|
;; URL: https://orgmode.org
|
2008-10-12 13:25:56 +00:00
|
|
|
;;
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2008-10-12 13:25:56 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Borrows ideas and a couple of lines of code from org-exp.el.
|
|
|
|
|
2016-08-23 20:13:56 +00:00
|
|
|
;; Thanks to the Org mailing list for testing and implementation and
|
|
|
|
;; feature suggestions
|
2008-10-12 13:25:56 +00:00
|
|
|
|
|
|
|
;;; Code:
|
2016-07-25 14:03:40 +00:00
|
|
|
|
2022-08-04 13:53:05 +00:00
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2016-07-25 14:03:40 +00:00
|
|
|
(require 'cl-lib)
|
2008-10-12 13:25:56 +00:00
|
|
|
(require 'org)
|
|
|
|
(require 'org-table)
|
|
|
|
|
|
|
|
(declare-function gnuplot-delchar-or-maybe-eof "ext:gnuplot" (arg))
|
|
|
|
(declare-function gnuplot-mode "ext:gnuplot" ())
|
|
|
|
(declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot" ())
|
|
|
|
|
|
|
|
(defvar org-plot/gnuplot-default-options
|
|
|
|
'((:plot-type . 2d)
|
|
|
|
(:with . lines)
|
|
|
|
(:ind . 0))
|
2010-07-16 21:23:01 +00:00
|
|
|
"Default options to gnuplot used by `org-plot/gnuplot'.")
|
2008-10-12 13:25:56 +00:00
|
|
|
|
2024-07-24 10:31:32 +00:00
|
|
|
(defvar org-plot-timestamp-fmt "%Y-%m-%d-%H:%M:%S"
|
|
|
|
"Default time format to be passed to Gnuplot.
|
|
|
|
Can be changed via timefmt plot option.")
|
2008-11-03 12:44:42 +00:00
|
|
|
|
2008-10-12 13:25:56 +00:00
|
|
|
(defun org-plot/add-options-to-plist (p options)
|
|
|
|
"Parse an OPTIONS line and set values in the property list P.
|
|
|
|
Returns the resulting property list."
|
2016-06-21 21:19:11 +00:00
|
|
|
(when options
|
2020-07-08 11:26:07 +00:00
|
|
|
(let ((op '(("type" . :plot-type)
|
|
|
|
("script" . :script)
|
|
|
|
("line" . :line)
|
|
|
|
("set" . :set)
|
|
|
|
("title" . :title)
|
|
|
|
("ind" . :ind)
|
|
|
|
("deps" . :deps)
|
|
|
|
("with" . :with)
|
|
|
|
("file" . :file)
|
|
|
|
("labels" . :labels)
|
|
|
|
("map" . :map)
|
|
|
|
("timeind" . :timeind)
|
|
|
|
("timefmt" . :timefmt)
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
("min" . :ymin)
|
|
|
|
("ymin" . :ymin)
|
2020-10-24 18:10:53 +00:00
|
|
|
("max" . :ymax)
|
|
|
|
("ymax" . :ymax)
|
|
|
|
("xmin" . :xmin)
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
("xmax" . :xmax)
|
|
|
|
("ticks" . :ticks)
|
2020-07-08 11:26:07 +00:00
|
|
|
("trans" . :transpose)
|
|
|
|
("transpose" . :transpose)))
|
2016-06-21 21:19:11 +00:00
|
|
|
(multiples '("set" "line"))
|
|
|
|
(regexp ":\\([\"][^\"]+?[\"]\\|[(][^)]+?[)]\\|[^ \t\n\r;,.]*\\)")
|
|
|
|
(start 0))
|
|
|
|
(dolist (o op)
|
|
|
|
(if (member (car o) multiples) ;; keys with multiple values
|
|
|
|
(while (string-match
|
|
|
|
(concat (regexp-quote (car o)) regexp)
|
|
|
|
options start)
|
|
|
|
(setq start (match-end 0))
|
|
|
|
(setq p (plist-put p (cdr o)
|
|
|
|
(cons (car (read-from-string
|
|
|
|
(match-string 1 options)))
|
|
|
|
(plist-get p (cdr o)))))
|
|
|
|
p)
|
|
|
|
(if (string-match (concat (regexp-quote (car o)) regexp)
|
|
|
|
options)
|
|
|
|
(setq p (plist-put p (cdr o)
|
|
|
|
(car (read-from-string
|
|
|
|
(match-string 1 options))))))))))
|
2008-10-12 13:25:56 +00:00
|
|
|
p)
|
|
|
|
|
|
|
|
(defun org-plot/goto-nearest-table ()
|
|
|
|
"Move the point forward to the beginning of nearest table.
|
|
|
|
Return value is the point at the beginning of the table."
|
|
|
|
(interactive) (move-beginning-of-line 1)
|
|
|
|
(while (not (or (org-at-table-p) (< 0 (forward-line 1)))))
|
|
|
|
(goto-char (org-table-begin)))
|
|
|
|
|
|
|
|
(defun org-plot/collect-options (&optional params)
|
2015-09-21 04:23:36 +00:00
|
|
|
"Collect options from an org-plot `#+Plot:' line.
|
2008-10-12 13:25:56 +00:00
|
|
|
Accepts an optional property list PARAMS, to which the options
|
|
|
|
will be added. Returns the resulting property list."
|
|
|
|
(interactive)
|
|
|
|
(let ((line (thing-at-point 'line)))
|
|
|
|
(if (string-match "#\\+PLOT: +\\(.*\\)$" line)
|
|
|
|
(org-plot/add-options-to-plist params (match-string 1 line))
|
|
|
|
params)))
|
|
|
|
|
org-plot and timestamps
On 28 Oct 2008, Eric Schulte wrote:
> Hi Charles, The patch looks great, I vote we drop it 'as is' into
> org-plot.el. The only enhancement that comes to mind would be to
> expose the `time-ind' variable as a plot option. Allowing users to
> specify different time formats, but really if they're that
> sophisticated in their use of gnuplot, then they can do that on their
> own through the `set:' option. Thanks for the addition! -- Eric
If I understand you correctly, then what we want to expose is the
"timefmt" that is put into the data file and then passed to gnuplot.
The following patch is a complete one against current which adds this
(it works for me, at least), and documents the new +PLOT option
behavior.
The one thing I'm not sure about is my method of passing the timefmt
parameter all the way into the temp file creation function. I made a
buffer-local variable there to do it. That seemed the least intrusive
way to accomplish this, but it seemed like poor practice. Thoughts?
You know, I really ought to allow a person to customize this variable (I
mean, using customize).
------------------------------------------------------------------------
Changes in origin/master
Modified doc/org.texi
2008-10-30 15:35:43 +00:00
|
|
|
(defun org-plot-quote-timestamp-field (s)
|
|
|
|
"Convert field S from timestamp to Unix time and export to gnuplot."
|
|
|
|
(format-time-string org-plot-timestamp-fmt (org-time-string-to-time s)))
|
|
|
|
|
2008-10-12 13:25:56 +00:00
|
|
|
(defun org-plot-quote-tsv-field (s)
|
|
|
|
"Quote field S for export to gnuplot."
|
|
|
|
(if (string-match org-table-number-regexp s) s
|
org-plot and timestamps
On 28 Oct 2008, Eric Schulte wrote:
> Hi Charles, The patch looks great, I vote we drop it 'as is' into
> org-plot.el. The only enhancement that comes to mind would be to
> expose the `time-ind' variable as a plot option. Allowing users to
> specify different time formats, but really if they're that
> sophisticated in their use of gnuplot, then they can do that on their
> own through the `set:' option. Thanks for the addition! -- Eric
If I understand you correctly, then what we want to expose is the
"timefmt" that is put into the data file and then passed to gnuplot.
The following patch is a complete one against current which adds this
(it works for me, at least), and documents the new +PLOT option
behavior.
The one thing I'm not sure about is my method of passing the timefmt
parameter all the way into the temp file creation function. I made a
buffer-local variable there to do it. That seemed the least intrusive
way to accomplish this, but it seemed like poor practice. Thoughts?
You know, I really ought to allow a person to customize this variable (I
mean, using customize).
------------------------------------------------------------------------
Changes in origin/master
Modified doc/org.texi
2008-10-30 15:35:43 +00:00
|
|
|
(if (string-match org-ts-regexp3 s)
|
2008-12-16 16:26:01 +00:00
|
|
|
(org-plot-quote-timestamp-field s)
|
org-plot and timestamps
On 28 Oct 2008, Eric Schulte wrote:
> Hi Charles, The patch looks great, I vote we drop it 'as is' into
> org-plot.el. The only enhancement that comes to mind would be to
> expose the `time-ind' variable as a plot option. Allowing users to
> specify different time formats, but really if they're that
> sophisticated in their use of gnuplot, then they can do that on their
> own through the `set:' option. Thanks for the addition! -- Eric
If I understand you correctly, then what we want to expose is the
"timefmt" that is put into the data file and then passed to gnuplot.
The following patch is a complete one against current which adds this
(it works for me, at least), and documents the new +PLOT option
behavior.
The one thing I'm not sure about is my method of passing the timefmt
parameter all the way into the temp file creation function. I made a
buffer-local variable there to do it. That seemed the least intrusive
way to accomplish this, but it seemed like poor practice. Thoughts?
You know, I really ought to allow a person to customize this variable (I
mean, using customize).
------------------------------------------------------------------------
Changes in origin/master
Modified doc/org.texi
2008-10-30 15:35:43 +00:00
|
|
|
(concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
|
2008-10-12 13:25:56 +00:00
|
|
|
|
|
|
|
(defun org-plot/gnuplot-to-data (table data-file params)
|
|
|
|
"Export TABLE to DATA-FILE in a format readable by gnuplot.
|
|
|
|
Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
|
|
|
|
(with-temp-file
|
2008-12-04 14:33:43 +00:00
|
|
|
data-file
|
2024-07-24 10:31:32 +00:00
|
|
|
(let ((org-plot-timestamp-fmt
|
|
|
|
(or
|
|
|
|
(plist-get params :timefmt)
|
|
|
|
org-plot-timestamp-fmt
|
|
|
|
"%Y-%m-%d-%H:%M:%S")))
|
|
|
|
(insert (orgtbl-to-generic
|
|
|
|
table
|
|
|
|
(org-combine-plists
|
|
|
|
'(:sep "\t" :fmt org-plot-quote-tsv-field)
|
|
|
|
params)))))
|
2008-10-12 13:25:56 +00:00
|
|
|
nil)
|
|
|
|
|
|
|
|
(defun org-plot/gnuplot-to-grid-data (table data-file params)
|
|
|
|
"Export the data in TABLE to DATA-FILE for gnuplot.
|
2010-01-21 15:15:40 +00:00
|
|
|
This means in a format appropriate for grid plotting by gnuplot.
|
2008-12-16 16:26:49 +00:00
|
|
|
PARAMS specifies which columns of TABLE should be plotted as independent
|
2019-11-12 13:44:05 +00:00
|
|
|
and dependent variables."
|
2008-10-12 13:25:56 +00:00
|
|
|
(interactive)
|
|
|
|
(let* ((ind (- (plist-get params :ind) 1))
|
|
|
|
(deps (if (plist-member params :deps)
|
|
|
|
(mapcar (lambda (val) (- val 1)) (plist-get params :deps))
|
|
|
|
(let (collector)
|
2016-07-25 14:34:48 +00:00
|
|
|
(dotimes (col (length (nth 0 table)))
|
2008-10-12 13:25:56 +00:00
|
|
|
(setf collector (cons col collector)))
|
|
|
|
collector)))
|
2012-08-09 14:13:18 +00:00
|
|
|
(counter 0)
|
|
|
|
row-vals)
|
2008-10-12 13:25:56 +00:00
|
|
|
(when (>= ind 0) ;; collect values of ind col
|
|
|
|
(setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter))
|
2020-02-18 21:57:37 +00:00
|
|
|
(cons counter (nth ind row)))
|
|
|
|
table)))
|
2008-10-12 13:25:56 +00:00
|
|
|
(when (or deps (>= ind 0)) ;; remove non-plotting columns
|
|
|
|
(setf deps (delq ind deps))
|
|
|
|
(setf table (mapcar (lambda (row)
|
|
|
|
(dotimes (col (length row))
|
|
|
|
(unless (memq col deps)
|
|
|
|
(setf (nth col row) nil)))
|
|
|
|
(delq nil row))
|
|
|
|
table)))
|
|
|
|
;; write table to gnuplot grid datafile format
|
|
|
|
(with-temp-file data-file
|
2016-07-25 14:34:48 +00:00
|
|
|
(let ((num-rows (length table)) (num-cols (length (nth 0 table)))
|
2012-08-09 14:13:18 +00:00
|
|
|
(gnuplot-row (lambda (col row value)
|
|
|
|
(setf col (+ 1 col)) (setf row (+ 1 row))
|
|
|
|
(format "%f %f %f\n%f %f %f\n"
|
|
|
|
col (- row 0.5) value ;; lower edge
|
|
|
|
col (+ row 0.5) value))) ;; upper edge
|
2008-10-12 13:25:56 +00:00
|
|
|
front-edge back-edge)
|
2012-08-09 14:13:18 +00:00
|
|
|
(dotimes (col num-cols)
|
|
|
|
(dotimes (row num-rows)
|
|
|
|
(setf back-edge
|
|
|
|
(concat back-edge
|
|
|
|
(funcall gnuplot-row (- col 1) row
|
|
|
|
(string-to-number (nth col (nth row table))))))
|
|
|
|
(setf front-edge
|
|
|
|
(concat front-edge
|
|
|
|
(funcall gnuplot-row col row
|
|
|
|
(string-to-number (nth col (nth row table)))))))
|
|
|
|
;; only insert once per row
|
|
|
|
(insert back-edge) (insert "\n") ;; back edge
|
|
|
|
(insert front-edge) (insert "\n") ;; front edge
|
|
|
|
(setf back-edge "") (setf front-edge ""))))
|
2008-10-12 13:25:56 +00:00
|
|
|
row-vals))
|
|
|
|
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(defun org--plot/values-stats (nums &optional hard-min hard-max)
|
2021-04-26 14:45:07 +00:00
|
|
|
"Rudimentary statistics about NUMS, useful for guessing axis ticks.
|
|
|
|
If HARD-MIN or HARD-MAX are set, they will be used instead of the min/max
|
|
|
|
of the NUMS."
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(let* ((minimum (or hard-min (apply #'min nums)))
|
|
|
|
(maximum (or hard-max (apply #'max nums)))
|
|
|
|
(range (- maximum minimum))
|
2020-09-05 13:05:36 +00:00
|
|
|
(rangeOrder (if (= range 0) 0
|
2020-12-23 18:26:17 +00:00
|
|
|
(ceiling (- 1 (log range 10)))))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(range-factor (expt 10 rangeOrder))
|
2020-09-05 13:05:36 +00:00
|
|
|
(nice-min (if (= range 0) (car nums)
|
|
|
|
(/ (float (floor (* minimum range-factor))) range-factor)))
|
|
|
|
(nice-max (if (= range 0) (car nums)
|
|
|
|
(/ (float (ceiling (* maximum range-factor))) range-factor))))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
`(:min ,minimum :max ,maximum :range ,range
|
2021-09-29 07:22:47 +00:00
|
|
|
:range-factor ,range-factor
|
|
|
|
:nice-min ,nice-min :nice-max ,nice-max :nice-range ,(- nice-max nice-min))))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
|
|
|
|
(defun org--plot/sensible-tick-num (table &optional hard-min hard-max)
|
2021-04-26 14:45:07 +00:00
|
|
|
"From a the values in a TABLE of data, guess an appropriate number of ticks.
|
|
|
|
If HARD-MIN and HARD-MAX can be used to fix the ends of the axis."
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(let* ((row-data
|
|
|
|
(mapcar (lambda (row) (org--plot/values-stats
|
2021-09-29 07:22:47 +00:00
|
|
|
(mapcar #'string-to-number (cdr row))
|
|
|
|
hard-min
|
|
|
|
hard-max)) table))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(row-normalised-ranges (mapcar (lambda (r-data)
|
|
|
|
(let ((val (round (*
|
|
|
|
(plist-get r-data :range-factor)
|
|
|
|
(plist-get r-data :nice-range)))))
|
|
|
|
(if (= (% val 10) 0) (/ val 10) val)))
|
|
|
|
row-data))
|
|
|
|
(range-prime-decomposition (mapcar #'org--plot/prime-factors row-normalised-ranges))
|
|
|
|
(weighted-factors (sort (apply #'org--plot/merge-alists #'+ 0
|
|
|
|
(mapcar (lambda (factors) (org--plot/item-frequencies factors t))
|
|
|
|
range-prime-decomposition))
|
|
|
|
(lambda (a b) (> (cdr a) (cdr b))))))
|
|
|
|
(apply #'* (org--plot/nice-frequency-pick weighted-factors))))
|
|
|
|
|
|
|
|
(defun org--plot/nice-frequency-pick (frequencies)
|
2021-04-26 14:45:07 +00:00
|
|
|
"From a list of FREQUENCIES, try to sensibly pick a sample of the most frequent."
|
2021-09-16 21:40:12 +00:00
|
|
|
;; TODO this mosly works decently, but could do with some tweaking to work more consistently.
|
2020-12-23 18:26:17 +00:00
|
|
|
(cl-case (length frequencies)
|
|
|
|
(1 (list (car (nth 0 frequencies))))
|
|
|
|
(2 (if (<= 3 (/ (cdr (nth 0 frequencies))
|
|
|
|
(cdr (nth 1 frequencies))))
|
|
|
|
(make-list 2
|
|
|
|
(car (nth 0 frequencies)))
|
|
|
|
(list (car (nth 0 frequencies))
|
|
|
|
(car (nth 1 frequencies)))))
|
|
|
|
(t
|
|
|
|
(let* ((total-count (apply #'+ (mapcar #'cdr frequencies)))
|
|
|
|
(n-freq (mapcar (lambda (freq) `(,(car freq) . ,(/ (float (cdr freq)) total-count))) frequencies))
|
|
|
|
(f-pick (list (car (car n-freq))))
|
|
|
|
(1-2-ratio (/ (cdr (nth 0 n-freq))
|
|
|
|
(cdr (nth 1 n-freq))))
|
|
|
|
(2-3-ratio (/ (cdr (nth 1 n-freq))
|
|
|
|
(cdr (nth 2 n-freq))))
|
|
|
|
(1-3-ratio (* 1-2-ratio 2-3-ratio))
|
|
|
|
(1-val (car (nth 0 n-freq)))
|
|
|
|
(2-val (car (nth 1 n-freq)))
|
|
|
|
(3-val (car (nth 2 n-freq))))
|
|
|
|
(when (> 1-2-ratio 4) (push 1-val f-pick))
|
|
|
|
(when (and (< 1-2-ratio 2-val)
|
|
|
|
(< (* (apply #'* f-pick) 2-val) 30))
|
|
|
|
(push 2-val f-pick))
|
|
|
|
(when (and (< 1-3-ratio 3-val)
|
|
|
|
(< (* (apply #'* f-pick) 3-val) 30))
|
|
|
|
(push 3-val f-pick))
|
|
|
|
f-pick))))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
|
|
|
|
(defun org--plot/merge-alists (function default alist1 alist2 &rest alists)
|
2021-04-26 14:45:07 +00:00
|
|
|
"Using FUNCTION, combine the elements of ALIST1, ALIST2 and any other ALISTS.
|
|
|
|
When an element is only present in one alist, DEFAULT is used as the second
|
|
|
|
argument for the FUNCTION."
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(when (> (length alists) 0)
|
|
|
|
(setq alist2 (apply #'org--plot/merge-alists function default alist2 alists)))
|
2020-10-24 17:43:01 +00:00
|
|
|
(cl-flet ((keys (alist) (mapcar #'car alist))
|
|
|
|
(lookup (key alist) (or (cdr (assoc key alist)) default)))
|
|
|
|
(cl-loop with keys = (cl-union (keys alist1) (keys alist2) :test 'equal)
|
|
|
|
for k in keys collect
|
|
|
|
(cons k (funcall function (lookup k alist1) (lookup k alist2))))))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
|
2022-07-14 09:55:52 +00:00
|
|
|
(defun org--plot/item-frequencies (values &optional normalize)
|
2021-04-26 14:45:07 +00:00
|
|
|
"Return an alist indicating the frequency of values in VALUES list.
|
2022-07-14 09:55:52 +00:00
|
|
|
When NORMALIZE is non-nil, the count is divided by the number of values."
|
|
|
|
(let ((normaliser (if normalize (float (length values)) 1)))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(cl-loop for (n . m) in (seq-group-by #'identity values)
|
|
|
|
collect (cons n (/ (length m) normaliser)))))
|
|
|
|
|
|
|
|
(defun org--plot/prime-factors (value)
|
2022-07-25 19:54:49 +00:00
|
|
|
"Return the prime decomposition of VALUE, e.g. for 12, (3 2 2)."
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
(let ((factors '(1)) (i 1))
|
|
|
|
(while (/= 1 value)
|
|
|
|
(setq i (1+ i))
|
|
|
|
(when (eq 0 (% value i))
|
|
|
|
(push i factors)
|
|
|
|
(setq value (/ value i))
|
|
|
|
(setq i (1- i))
|
|
|
|
))
|
2020-10-24 17:43:01 +00:00
|
|
|
(cl-subseq factors 0 -1)))
|
org-plot.el: add utility functions for range,ticks
* lisp/org-plot.el (org-plot/add-options-to-plist): Add the options :ymin
:ymax :xmin :xmax, as well as :min and :max as aliases to the y{min,max}
options. The :ticks option is also added, for specifying how many ticks
should be used.
(org--plot/values-stats, org--plot/sensible-tick-num,
org--plot/nice-frequency-pick, org--plot/merge-alists,
org--plot/item-frequencies, org--plot/prime-factors): New utility
functions added to allow for somewhat sensible determination of a :ticks
value when none is provided. This turns out to be harder than expected,
and so a number of functions are used to attempt to do so. The essence
of the method used, is to round values and find their prime
decompositions. From this we try to select the most common components
to give a reasonable step size. We also add a 'ticks' parameter for
manually setting the number of ticks, and (y)min/max parameters
similarly.
2020-07-08 20:47:40 +00:00
|
|
|
|
2022-11-15 22:49:56 +00:00
|
|
|
(defgroup org-plot nil
|
|
|
|
"Options for plotting in Org mode."
|
|
|
|
:tag "Org Plot"
|
|
|
|
:group 'org)
|
|
|
|
|
2020-07-08 14:26:21 +00:00
|
|
|
(defcustom org-plot/gnuplot-script-preamble ""
|
2021-04-26 14:45:07 +00:00
|
|
|
"String of function to be inserted before the gnuplot plot command is run.
|
|
|
|
|
2024-06-18 05:09:27 +00:00
|
|
|
Note that this is in addition to, not instead of other content generated
|
|
|
|
in `org-plot/gnuplot-script'. If a function, it is called with the
|
|
|
|
parameters used by the current plot type (see
|
|
|
|
`org-plot/preset-plot-types'), and must return a string to be used."
|
2020-07-08 14:26:21 +00:00
|
|
|
:group 'org-plot
|
|
|
|
:type '(choice string function))
|
|
|
|
|
2020-07-08 20:27:18 +00:00
|
|
|
(defcustom org-plot/preset-plot-types
|
2020-07-30 10:36:11 +00:00
|
|
|
'((2d :plot-cmd "plot"
|
|
|
|
:check-ind-type t
|
|
|
|
:plot-func
|
|
|
|
(lambda (_table data-file num-cols params plot-str)
|
2020-07-08 20:27:18 +00:00
|
|
|
(let* ((type (plist-get params :plot-type))
|
|
|
|
(with (if (eq type 'grid) 'pm3d (plist-get params :with)))
|
|
|
|
(ind (plist-get params :ind))
|
|
|
|
(deps (if (plist-member params :deps) (plist-get params :deps)))
|
2021-05-09 15:47:27 +00:00
|
|
|
(text-ind (or (plist-get params :textind)
|
|
|
|
(eq (plist-get params :with) 'histograms)))
|
2020-07-08 20:27:18 +00:00
|
|
|
(col-labels (plist-get params :labels))
|
|
|
|
res)
|
|
|
|
(dotimes (col num-cols res)
|
|
|
|
(unless (and (eq type '2d)
|
|
|
|
(or (and ind (equal (1+ col) ind))
|
|
|
|
(and deps (not (member (1+ col) deps)))))
|
|
|
|
(setf res
|
|
|
|
(cons
|
|
|
|
(format plot-str data-file
|
|
|
|
(or (and ind (> ind 0)
|
|
|
|
(not text-ind)
|
|
|
|
(format "%d:" ind)) "")
|
|
|
|
(1+ col)
|
|
|
|
(if text-ind (format ":xticlabel(%d)" ind) "")
|
|
|
|
with
|
|
|
|
(or (nth col col-labels)
|
|
|
|
(format "%d" (1+ col))))
|
|
|
|
res)))))))
|
2020-07-30 10:36:11 +00:00
|
|
|
(3d :plot-cmd "splot"
|
|
|
|
:plot-pre (lambda (_table _data-file _num-cols params _plot-str)
|
|
|
|
(if (plist-get params :map) "set map"))
|
|
|
|
:plot-func
|
|
|
|
(lambda (_table data-file _num-cols params _plot-str)
|
2020-07-08 20:27:18 +00:00
|
|
|
(let* ((type (plist-get params :plot-type))
|
|
|
|
(with (if (eq type 'grid) 'pm3d (plist-get params :with))))
|
|
|
|
(list (format "'%s' matrix with %s title ''"
|
|
|
|
data-file with)))))
|
2020-07-30 10:36:11 +00:00
|
|
|
(grid :plot-cmd "splot"
|
|
|
|
:plot-pre (lambda (_table _data-file _num-cols params _plot-str)
|
|
|
|
(if (plist-get params :map) "set pm3d map" "set map"))
|
2024-06-18 05:09:27 +00:00
|
|
|
:data-dump (lambda (table data-file _num-cols params)
|
2020-07-30 10:36:11 +00:00
|
|
|
(let ((y-labels (org-plot/gnuplot-to-grid-data
|
|
|
|
table data-file params)))
|
|
|
|
(when y-labels (plist-put params :ylabels y-labels))))
|
|
|
|
:plot-func
|
|
|
|
(lambda (table data-file _num-cols params _plot-str)
|
2020-07-08 20:27:18 +00:00
|
|
|
(let* ((type (plist-get params :plot-type))
|
|
|
|
(with (if (eq type 'grid) 'pm3d (plist-get params :with))))
|
2020-07-30 10:36:11 +00:00
|
|
|
(list (format "'%s' with %s title ''"
|
|
|
|
data-file with)))))
|
|
|
|
(radar :plot-func
|
|
|
|
(lambda (table _data-file _num-cols params plot-str)
|
2020-07-08 21:21:44 +00:00
|
|
|
(list (org--plot/radar table params)))))
|
2021-09-16 21:40:12 +00:00
|
|
|
"List of plists describing the available plot types.
|
2021-02-24 04:35:25 +00:00
|
|
|
The car is the type name, and the property :plot-func must be
|
|
|
|
set. The value of :plot-func is a lambda which yields plot-lines
|
2021-04-26 14:45:07 +00:00
|
|
|
\(a list of strings) as the cdr.
|
2020-07-30 10:36:11 +00:00
|
|
|
|
2021-02-24 04:35:25 +00:00
|
|
|
All lambda functions have the parameters of
|
|
|
|
`org-plot/gnuplot-script' and PLOT-STR passed to them. i.e. they
|
|
|
|
are called with the following signature: (TABLE DATA-FILE
|
|
|
|
NUM-COLS PARAMS PLOT-STR)
|
2020-07-30 10:36:11 +00:00
|
|
|
|
|
|
|
Potentially useful parameters in PARAMS include:
|
|
|
|
:set :line :map :title :file :ind :timeind :timefmt :textind
|
|
|
|
:deps :labels :xlabels :ylabels :xmin :xmax :ymin :ymax :ticks
|
|
|
|
|
2021-02-24 04:35:25 +00:00
|
|
|
In addition to :plot-func, the following optional properties may
|
|
|
|
be set.
|
2020-07-30 10:36:11 +00:00
|
|
|
|
|
|
|
- :plot-cmd - A gnuplot command appended to each plot-line.
|
2021-02-24 04:35:25 +00:00
|
|
|
Accepts string or nil. Default value: nil.
|
2020-07-30 10:36:11 +00:00
|
|
|
|
|
|
|
- :check-ind-type - Whether the types of ind values should be checked.
|
|
|
|
Accepts boolean.
|
|
|
|
|
|
|
|
- :plot-str - the formula string passed to :plot-func as PLOT-STR
|
2021-02-24 04:35:25 +00:00
|
|
|
Accepts string. Default value: \"'%s' using %s%d%s with %s title '%s'\"
|
2020-07-30 10:36:11 +00:00
|
|
|
|
2021-02-24 04:35:25 +00:00
|
|
|
- :data-dump - Function to dump the table to a datafile for ease of
|
|
|
|
use.
|
2020-07-30 10:36:11 +00:00
|
|
|
|
2024-06-18 05:09:27 +00:00
|
|
|
Accepts function with arguments:
|
|
|
|
(table data-file num-cols params)
|
2021-02-24 04:35:25 +00:00
|
|
|
|
|
|
|
- :plot-pre - Gnuplot code to be inserted early into the script, just
|
|
|
|
after term and output have been set.
|
|
|
|
|
|
|
|
Accepts string, nil, or lambda function which returns string
|
|
|
|
or nil. Defaults to nil."
|
2020-07-08 20:27:18 +00:00
|
|
|
:group 'org-plot
|
2021-10-01 15:33:32 +00:00
|
|
|
:type 'alist)
|
2020-07-08 20:27:18 +00:00
|
|
|
|
2020-07-08 21:21:44 +00:00
|
|
|
(defvar org--plot/radar-template
|
|
|
|
"### spider plot/chart with gnuplot
|
|
|
|
# also known as: radar chart, web chart, star chart, cobweb chart,
|
|
|
|
# radar plot, web plot, star plot, cobweb plot, etc. ...
|
|
|
|
set datafile separator ' '
|
|
|
|
set size square
|
|
|
|
unset tics
|
|
|
|
set angles degree
|
|
|
|
set key bmargin center horizontal
|
|
|
|
unset border
|
|
|
|
|
2021-09-16 21:40:12 +00:00
|
|
|
# Load data and setup
|
2020-07-08 21:21:44 +00:00
|
|
|
load \"%s\"
|
|
|
|
|
|
|
|
# General settings
|
|
|
|
DataColCount = words($Data[1])-1
|
2020-10-24 17:47:57 +00:00
|
|
|
AxesCount = |$Data|-HeaderLines-1
|
2020-07-08 21:21:44 +00:00
|
|
|
AngleOffset = 90
|
|
|
|
Max = 1
|
|
|
|
d=0.1*Max
|
|
|
|
Direction = -1 # counterclockwise=1, clockwise = -1
|
|
|
|
|
|
|
|
# Tic settings
|
|
|
|
TicCount = %s
|
|
|
|
TicOffset = 0.1
|
|
|
|
TicValue(axis,i) = real(i)*(word($Settings[axis],3)-word($Settings[axis],2)) \\
|
|
|
|
/ word($Settings[axis],4)+word($Settings[axis],2)
|
|
|
|
TicLabelPosX(axis,i) = PosX(axis,i/TicCount) + PosY(axis, TicOffset)
|
|
|
|
TicLabelPosY(axis,i) = PosY(axis,i/TicCount) - PosX(axis, TicOffset)
|
|
|
|
TicLen = 0.03
|
|
|
|
TicdX(axis,i) = 0.5*TicLen*cos(alpha(axis)-90)
|
|
|
|
TicdY(axis,i) = 0.5*TicLen*sin(alpha(axis)-90)
|
|
|
|
|
|
|
|
# Label
|
|
|
|
LabOffset = 0.10
|
|
|
|
LabX(axis) = PosX(axis+1,Max+2*d) + PosY(axis, LabOffset)
|
|
|
|
LabY(axis) = PosY($0+1,Max+2*d)
|
|
|
|
|
|
|
|
# Functions
|
|
|
|
alpha(axis) = (axis-1)*Direction*360.0/AxesCount+AngleOffset
|
|
|
|
PosX(axis,R) = R*cos(alpha(axis))
|
|
|
|
PosY(axis,R) = R*sin(alpha(axis))
|
|
|
|
Scale(axis,value) = real(value-word($Settings[axis],2))/(word($Settings[axis],3)-word($Settings[axis],2))
|
|
|
|
|
|
|
|
# Spider settings
|
|
|
|
set style arrow 1 dt 1 lw 1.0 @fgal head filled size 0.06,25 # style for axes
|
|
|
|
set style arrow 2 dt 2 lw 0.5 @fgal nohead # style for weblines
|
|
|
|
set style arrow 3 dt 1 lw 1 @fgal nohead # style for axis tics
|
|
|
|
set samples AxesCount
|
|
|
|
set isosamples TicCount
|
|
|
|
set urange[1:AxesCount]
|
|
|
|
set vrange[1:TicCount]
|
|
|
|
set style fill transparent solid 0.2
|
|
|
|
|
|
|
|
set xrange[-Max-4*d:Max+4*d]
|
|
|
|
set yrange[-Max-4*d:Max+4*d]
|
|
|
|
plot \\
|
|
|
|
'+' u (0):(0):(PosX($0,Max+d)):(PosY($0,Max+d)) w vec as 1 not, \\
|
|
|
|
$Data u (LabX($0)): \\
|
|
|
|
(LabY($0)):1 every ::HeaderLines w labels center enhanced @fgt not, \\
|
|
|
|
for [i=1:DataColCount] $Data u (PosX($0+1,Scale($0+1,column(i+1)))): \\
|
|
|
|
(PosY($0+1,Scale($0+1,column(i+1)))) every ::HeaderLines w filledcurves lt i title word($Data[1],i+1), \\
|
|
|
|
%s
|
|
|
|
# '++' u (PosX($1,$2/TicCount)-TicdX($1,$2/TicCount)): \\
|
|
|
|
# (PosY($1,$2/TicCount)-TicdY($1,$2/TicCount)): \\
|
|
|
|
# (2*TicdX($1,$2/TicCount)):(2*TicdY($1,$2/TicCount)) \\
|
|
|
|
# w vec as 3 not, \\
|
|
|
|
### end of code
|
|
|
|
")
|
|
|
|
|
|
|
|
(defvar org--plot/radar-ticks
|
|
|
|
" '++' u (PosX($1,$2/TicCount)):(PosY($1,$2/TicCount)): \\
|
|
|
|
(PosX($1+1,$2/TicCount)-PosX($1,$2/TicCount)): \\
|
|
|
|
(PosY($1+1,$2/TicCount)-PosY($1,$2/TicCount)) w vec as 2 not, \\
|
|
|
|
'++' u (TicLabelPosX(%s,$2)):(TicLabelPosY(%s,$2)): \\
|
|
|
|
(sprintf('%%g',TicValue(%s,$2))) w labels font ',8' @fgat not")
|
|
|
|
|
|
|
|
(defvar org--plot/radar-setup-template
|
|
|
|
"# Data
|
|
|
|
$Data <<HEREHAVESOMEDATA
|
|
|
|
%s
|
|
|
|
HEREHAVESOMEDATA
|
|
|
|
HeaderLines = 1
|
|
|
|
|
|
|
|
# Settings for scale and offset adjustments
|
|
|
|
# axis min max tics axisLabelXoff axisLabelYoff
|
|
|
|
$Settings <<EOD
|
|
|
|
%s
|
|
|
|
EOD
|
|
|
|
")
|
|
|
|
|
|
|
|
(defun org--plot/radar (table params)
|
2021-04-26 14:45:07 +00:00
|
|
|
"Create gnuplot code for a radar plot of TABLE with PARAMS."
|
2020-07-08 21:21:44 +00:00
|
|
|
(let* ((data
|
2020-12-23 18:26:17 +00:00
|
|
|
(concat "\"" (mapconcat #'identity (plist-get params :labels) "\" \"") "\""
|
2020-07-08 21:21:44 +00:00
|
|
|
"\n"
|
2020-12-23 18:26:17 +00:00
|
|
|
(mapconcat (lambda (row)
|
|
|
|
(format
|
|
|
|
"\"%s\" %s"
|
|
|
|
(car row)
|
|
|
|
(mapconcat #'identity (cdr row) " ")))
|
|
|
|
(append table (list (car table)))
|
|
|
|
"\n")))
|
2020-07-08 21:21:44 +00:00
|
|
|
(ticks (or (plist-get params :ticks)
|
|
|
|
(org--plot/sensible-tick-num table
|
|
|
|
(plist-get params :ymin)
|
|
|
|
(plist-get params :ymax))))
|
|
|
|
(settings
|
2020-12-23 18:26:17 +00:00
|
|
|
(mapconcat (lambda (row)
|
|
|
|
(let ((data (org--plot/values-stats
|
|
|
|
(mapcar #'string-to-number (cdr row)))))
|
|
|
|
(format
|
|
|
|
"\"%s\" %s %s %s"
|
|
|
|
(car row)
|
|
|
|
(or (plist-get params :ymin)
|
|
|
|
(plist-get data :nice-min))
|
|
|
|
(or (plist-get params :ymax)
|
|
|
|
(plist-get data :nice-max))
|
|
|
|
(if (eq ticks 0) 2 ticks)
|
|
|
|
)))
|
|
|
|
(append table (list (car table)))
|
|
|
|
"\n"))
|
2020-07-08 21:21:44 +00:00
|
|
|
(setup-file (make-temp-file "org-plot-setup")))
|
2020-10-24 17:43:01 +00:00
|
|
|
(let ((coding-system-for-write 'utf-8))
|
|
|
|
(write-region (format org--plot/radar-setup-template data settings) nil setup-file nil :silent))
|
2020-07-08 21:21:44 +00:00
|
|
|
(format org--plot/radar-template
|
|
|
|
setup-file
|
|
|
|
(if (eq ticks 0) 2 ticks)
|
|
|
|
(if (eq ticks 0) ""
|
|
|
|
(apply #'format org--plot/radar-ticks
|
|
|
|
(make-list 3 (if (and (plist-get params :ymin)
|
|
|
|
(plist-get params :ymax))
|
|
|
|
;; FIXME multi-drawing of tick labels with "1"
|
|
|
|
"1" "$1")))))))
|
|
|
|
|
2020-07-08 21:00:03 +00:00
|
|
|
(defcustom org-plot/gnuplot-term-extra ""
|
|
|
|
"String or function which provides the extra term options.
|
|
|
|
E.g. a value of \"size 1050,650\" would cause
|
2020-07-08 21:05:20 +00:00
|
|
|
\"set term ... size 1050,650\" to be used.
|
2024-06-18 05:09:27 +00:00
|
|
|
If a function, it is called with the parameters used by the current plot
|
|
|
|
type, see `org-plot/preset-plot-types'."
|
2020-07-08 21:00:03 +00:00
|
|
|
:group 'org-plot
|
|
|
|
:type '(choice string function))
|
|
|
|
|
2020-07-30 10:36:11 +00:00
|
|
|
(defun org-plot/gnuplot-script (table data-file num-cols params &optional preface)
|
2021-04-26 14:45:07 +00:00
|
|
|
"Write a gnuplot script for TABLE to DATA-FILE respecting options in PARAMS.
|
2009-07-08 05:01:00 +00:00
|
|
|
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."
|
2020-07-30 10:36:11 +00:00
|
|
|
(let* ((type-name (plist-get params :plot-type))
|
|
|
|
(type (cdr (assoc type-name org-plot/preset-plot-types))))
|
|
|
|
(unless type
|
2021-04-26 14:45:07 +00:00
|
|
|
(user-error "Org-plot type `%s' is undefined" type-name))
|
2020-07-30 10:36:11 +00:00
|
|
|
(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
|
2021-04-26 15:38:52 +00:00
|
|
|
(funcall ats (format "set output '%s'" (expand-file-name file))))
|
2020-07-30 10:36:11 +00:00
|
|
|
|
|
|
|
(when plot-pre
|
|
|
|
(funcall ats (funcall plot-pre table data-file num-cols params plot-str)))
|
2020-07-08 20:27:18 +00:00
|
|
|
|
2012-08-10 13:21:52 +00:00
|
|
|
(funcall ats
|
2020-07-30 10:36:11 +00:00
|
|
|
(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)))
|
2008-10-12 13:25:56 +00:00
|
|
|
|
2021-04-26 14:48:28 +00:00
|
|
|
(defun org-plot/redisplay-img-in-buffer (img-file)
|
|
|
|
"Find any overlays for IMG-FILE in the current Org buffer, and refresh them."
|
|
|
|
(dolist (img-overlay org-inline-image-overlays)
|
|
|
|
(when (string= img-file (plist-get (cdr (overlay-get img-overlay 'display)) :file))
|
2022-08-22 17:22:27 +00:00
|
|
|
(when (and (file-exists-p img-file)
|
|
|
|
(fboundp 'image-flush))
|
2022-09-06 12:50:59 +00:00
|
|
|
(image-flush (overlay-get img-overlay 'display))))))
|
2021-04-26 14:48:28 +00:00
|
|
|
|
2008-10-12 13:25:56 +00:00
|
|
|
;;-----------------------------------------------------------------------------
|
|
|
|
;; facade functions
|
|
|
|
;;;###autoload
|
|
|
|
(defun org-plot/gnuplot (&optional params)
|
2010-07-16 21:23:01 +00:00
|
|
|
"Plot table using gnuplot. Gnuplot options can be specified with PARAMS.
|
2008-10-12 13:25:56 +00:00
|
|
|
If not given options will be taken from the +PLOT
|
|
|
|
line directly before or after the table."
|
|
|
|
(interactive)
|
2023-01-23 15:06:46 +00:00
|
|
|
(org-require-package 'gnuplot)
|
2008-10-12 13:25:56 +00:00
|
|
|
(save-window-excursion
|
2024-04-07 10:34:13 +00:00
|
|
|
;; `gnuplot-send-buffer-to-gnuplot' will display *gnuplot* buffer
|
|
|
|
;; if `gnuplot-display-process' is non-nil. Make it visible while
|
|
|
|
;; gnuplot is processing the data, preferably as a split, and
|
|
|
|
;; restore old window configuration after gnuplot finishes.
|
|
|
|
(ignore-errors (delete-other-windows))
|
2015-03-15 16:12:01 +00:00
|
|
|
(when (get-buffer "*gnuplot*") ; reset *gnuplot* if it already running
|
2009-11-05 21:51:50 +00:00
|
|
|
(with-current-buffer "*gnuplot*"
|
2014-11-02 13:50:47 +00:00
|
|
|
(goto-char (point-max))))
|
2021-04-26 15:41:40 +00:00
|
|
|
(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))))
|
2008-10-12 13:25:56 +00:00
|
|
|
;; collect table and table information
|
2024-06-15 04:55:19 +00:00
|
|
|
(let* ((table (let ((tbl (save-excursion
|
2021-05-22 20:04:01 +00:00
|
|
|
(org-plot/goto-nearest-table)
|
2021-05-09 10:11:49 +00:00
|
|
|
(org-table-to-lisp))))
|
2020-07-08 11:26:07 +00:00
|
|
|
(when (pcase (plist-get params :transpose)
|
2020-12-23 05:01:56 +00:00
|
|
|
(`y t)
|
|
|
|
(`yes t)
|
|
|
|
(`t t))
|
2020-07-30 10:25:19 +00:00
|
|
|
(if (not (memq 'hline tbl))
|
2020-07-08 11:26:07 +00:00
|
|
|
(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)
|
2020-07-30 10:36:11 +00:00
|
|
|
(nth 0 table))))
|
2024-06-18 05:09:27 +00:00
|
|
|
(type (cdr (assoc (plist-get params :plot-type)
|
|
|
|
org-plot/preset-plot-types)))
|
2024-06-15 04:55:19 +00:00
|
|
|
gnuplot-script data-file)
|
2020-07-30 10:36:11 +00:00
|
|
|
|
|
|
|
(unless type
|
2021-04-26 14:45:07 +00:00
|
|
|
(user-error "Org-plot type `%s' is undefined" (plist-get params :plot-type)))
|
2020-07-30 10:36:11 +00:00
|
|
|
|
2015-03-15 16:12:01 +00:00
|
|
|
(when (eq (cadr table) 'hline)
|
2016-07-25 14:34:48 +00:00
|
|
|
(setf params
|
2020-06-12 16:42:34 +00:00
|
|
|
(plist-put params :labels (car table))) ; headers to labels
|
2015-03-15 16:12:01 +00:00
|
|
|
(setf table (delq 'hline (cdr table)))) ; clean non-data from table
|
|
|
|
;; Collect options.
|
2008-10-12 13:25:56 +00:00
|
|
|
(save-excursion (while (and (equal 0 (forward-line -1))
|
2010-03-01 15:22:18 +00:00
|
|
|
(looking-at "[[:space:]]*#\\+"))
|
2008-10-12 13:25:56 +00:00
|
|
|
(setf params (org-plot/collect-options params))))
|
2024-06-18 05:09:27 +00:00
|
|
|
;; Ensure that the user can override any plot parameter, and
|
|
|
|
;; that the parameters set by the plot type in
|
|
|
|
;; `org-plot/preset-plot-types' is respected.
|
|
|
|
(setq params (org-combine-plists type params))
|
2020-07-30 10:36:11 +00:00
|
|
|
;; Dump table to datafile
|
2021-10-17 06:34:10 +00:00
|
|
|
(let ((dump-func (plist-get type :data-dump)))
|
2024-06-15 04:55:19 +00:00
|
|
|
;; Use a stable temporary file to ensure that 'replot' upon
|
|
|
|
;; resizing a GUI gnuplot terminal window works.
|
|
|
|
(setq data-file (org-babel-temp-stable-file
|
|
|
|
(list (or dump-func 'org-plot/gnuplot-to-data)
|
|
|
|
table num-cols params)
|
|
|
|
"org-plot"))
|
2021-10-17 06:34:10 +00:00
|
|
|
(if dump-func
|
|
|
|
(funcall dump-func table data-file num-cols params)
|
|
|
|
(org-plot/gnuplot-to-data table data-file params)))
|
2020-06-14 15:52:41 +00:00
|
|
|
;; Check type of ind column (timestamp? text?)
|
2020-07-30 10:36:11 +00:00
|
|
|
(when (plist-get params :check-ind-type)
|
2020-06-14 15:52:41 +00:00
|
|
|
(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
|
2015-03-15 16:12:01 +00:00
|
|
|
;; Write script.
|
2022-05-31 16:42:57 +00:00
|
|
|
(setq gnuplot-script
|
|
|
|
(org-plot/gnuplot-script
|
|
|
|
table data-file num-cols params (plist-get params :script)))
|
2008-10-12 13:25:56 +00:00
|
|
|
(with-temp-buffer
|
2015-03-15 16:12:01 +00:00
|
|
|
(if (plist-get params :script) ; user script
|
2022-05-31 16:42:57 +00:00
|
|
|
(progn (insert gnuplot-script "\n")
|
2020-07-08 10:34:46 +00:00
|
|
|
(insert-file-contents (plist-get params :script))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward "\\$datafile" nil t)
|
|
|
|
(replace-match data-file nil nil)))
|
2022-05-31 16:42:57 +00:00
|
|
|
(insert gnuplot-script))
|
2015-03-15 16:12:01 +00:00
|
|
|
;; Graph table.
|
2008-10-12 13:25:56 +00:00
|
|
|
(gnuplot-mode)
|
2021-04-28 16:47:01 +00:00
|
|
|
(condition-case nil
|
|
|
|
(gnuplot-send-buffer-to-gnuplot)
|
|
|
|
(buffer-read-only nil)))
|
2015-03-15 16:12:01 +00:00
|
|
|
;; Cleanup.
|
2021-04-26 14:48:28 +00:00
|
|
|
(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)))))))
|
2008-10-12 13:25:56 +00:00
|
|
|
|
|
|
|
(provide 'org-plot)
|
|
|
|
|
2012-10-02 06:50:46 +00:00
|
|
|
;; Local variables:
|
|
|
|
;; generated-autoload-file: "org-loaddefs.el"
|
|
|
|
;; End:
|
|
|
|
|
2008-10-12 13:25:56 +00:00
|
|
|
;;; org-plot.el ends here
|