* Re: Can org show live charts?
2014-10-21 19:39 Can org show live charts? Tom
@ 2014-10-21 23:15 ` John Kitchin
2014-10-22 19:36 ` Tom
0 siblings, 1 reply; 3+ messages in thread
From: John Kitchin @ 2014-10-21 23:15 UTC (permalink / raw)
To: Tom; +Cc: emacs-orgmode
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
^ permalink raw reply [flat|nested] 3+ messages in thread