emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: John Kitchin <jkitchin@andrew.cmu.edu>
To: Tom <adatgyujto@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Can org show live charts?
Date: Tue, 21 Oct 2014 19:15:59 -0400	[thread overview]
Message-ID: <m2siihui8g.fsf@andrew.cmu.edu> (raw)
In-Reply-To: <loom.20141021T213224-760@post.gmane.org> (Tom's message of "Tue, 21 Oct 2014 19:39:15 +0000 (UTC)")

I think the answer is sort of. I am no idle timer guru, and this code
would probably not do what you want except for this file. The idea is to
have a named table, use it as a data source in a named code block that
generates the image. Then, make an elisp function that goes to that code
block and run it, and set an idle timer to run the elisp function. I
have this in my init file:

;; refresh images after running a block
(add-hook 'org-babel-after-execute-hook
	  (lambda () (org-display-inline-images nil t)))

which refreshes inline images after each execution. This works, but it
might take some tinkering to get your idle time ok. 1 second was much
too low, 5 seconds is not too bad, although there is a notable lag when
the graph is made, and the buffer moves around a bit. there is no error
checking for data in the table, so if it crashes the plotting block, you
get an error.

* "live" graphics in org-mode

#+tblname: tbldata
|   x |   y |
|-----+-----|
|   1 |   1 |
|   1 |   0 |
|   2 |   4 |
|   3 |   5 |
| 0.2 | 0.3 |


#+RESULTS: make-table-graph
#+BEGIN_SRC org
[[1, 1], [1, 0], [2, 4], [3, 5], [0.2, 0.3]]
[[./live-chart.png]]
#+END_SRC

* Code

#+NAME: make-table-graph
#+BEGIN_SRC python :var data=tbldata :results org
import matplotlib.pyplot as plt
plt.plot([x[0] for x in data], [x[1] for x in data])
plt.savefig('live-chart.png')
print data
print '[[./live-chart.png]]'
#+END_SRC

Now, for the elisp part.

#+BEGIN_SRC emacs-lisp
(defun update-graph ()
  (save-excursion
    (goto-char (point-min))
    (re-search-forward "#\\+NAME: make-table-graph")
    (forward-line 2)
    (org-babel-execute-src-block)))

(defvar my-timer nil)

(setq my-timer (run-with-idle-timer
		5 ; idle for this many seconds
		t ; repeat indefinitely
		'update-graph))
#+END_SRC

#+RESULTS:
: [nil 0 5 0 t update-graph nil idle 0]

#+BEGIN_SRC emacs-lisp
(cancel-timer my-timer)
#+END_SRC

Tom <adatgyujto@gmail.com> writes:

> Reading the documentation it is not clear for me if live
> charts are supported.
>
> By live chart I mean having a table data and below that
> an inserted image in the buffer which shows the data rendered
> from the table, and the image is updated automatically every time
> the the table is changed (and, say, emacs is idle for a while).
>
> So I'm not talking about exporting, but having the live rendered
> chart image right in the buffer,
>
> Can org do this? I don't see any technical obstacles, but
> I haven't seen this explicitly mentioned in the docs, that's
> why I'm asking.
>
>
>

-- 
-----------------------------------
John Kitchin
Professor
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
http://kitchingroup.cheme.cmu.edu

  reply	other threads:[~2014-10-21 23:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-21 19:39 Can org show live charts? Tom
2014-10-21 23:15 ` John Kitchin [this message]
2014-10-22 19:36   ` Tom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2siihui8g.fsf@andrew.cmu.edu \
    --to=jkitchin@andrew.cmu.edu \
    --cc=adatgyujto@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).