0

Howto remove the currency from the table or get ledger -j to work so that gnuplot can work with the data?

#+NAME: LedgerCli
#+BEGIN_SRC ledger :results raw :cmdline reg "Assets:Service Days" --exchange sd --format='| %D | %t | \n'
SOME LEDGER STATEMENTS

#+END_SRC
#+Name: ledgercli-table
#+RESULTS: LedgerCli
| 2019/12/01 | 12.0 sd |
| 2019/12/03 | -0.5 sd |
| 2019/12/05 | -0.5 sd |
| 2019/12/10 | -1.0 sd |
| 2019/12/17 | -1.0 sd |
| 2020/03/23 | -4.0 sd |
| 2020/04/01 | -0.1 sd |
| 2020/05/19 | -0.5 sd |
| 2020/05/26 | -0.3 sd |

#+begin_src gnuplot :var data=ledgercli-table :file gnuplot.png :exports results
  set xdata time
  set style data linespoints
  set timefmt "%Y/%m/%d"
  plot data u 1:2
#+end_src

#+RESULTS:
[[file:gnuplot.png]]

this results in

gnuplot> data = "/tmp/babel-sC46Yh/gnuplot-DvVaqU"
gnuplot> set term png

Terminal type is now 'png'
set output "gnuplot.png"
Options are 'nocrop enhanced size 640,480 font "arial,12.0" '
gnuplot> gnuplot> set xdata time
gnuplot> set style data linespoints
gnuplot> set timefmt "%Y/%m/%d"
gnuplot> plot data u 1:2
         warning: Skipping data file with no valid points
                        ^
         x range is invalid

If I manually remove all occurrences of sd from the table a plot gets produces - not what I want but that is a different problem.

0

There are various places where that could be done, e.g. at the ledger-cli level, you might be able to convince it to skip the sd part, or to produce a third column, identical to the second as far as the numbers are concerned but omitting the sd part and then having your gnuplot block look at columns 1 and 3. Since I don't know much about ledger, I will not pursue these further.

Here I'm going to add a source block to transform the input table to the form that the gnuplot block expects:

#+Name: ledgercli-table
#+RESULTS: LedgerCli
| 2019/12/01 | 12.0 sd |
| 2019/12/03 | -0.5 sd |
| 2019/12/05 | -0.5 sd |
| 2019/12/10 | -1.0 sd |
| 2019/12/17 | -1.0 sd |
| 2020/03/23 | -4.0 sd |
| 2020/04/01 | -0.1 sd |
| 2020/05/19 | -0.5 sd |
| 2020/05/26 | -0.3 sd |

#+begin_src python :var t=ledgercli-table :results output drawer
  print("#+Name: ledger-cli-table-no-currency")
  for l in t:
    print(f"| {l[0]} | {l[1].split(' ')[0]} |")

#+end_src

#+RESULTS:
:results:
#+Name: ledger-cli-table-no-currency
| 2019/12/01 | 12.0 |
| 2019/12/03 | -0.5 |
| 2019/12/05 | -0.5 |
| 2019/12/10 | -1.0 |
| 2019/12/17 | -1.0 |
| 2020/03/23 | -4.0 |
| 2020/04/01 | -0.1 |
| 2020/05/19 | -0.5 |
| 2020/05/26 | -0.3 |
:end:

#+begin_src gnuplot :var data=ledger-cli-table-no-currency :file gnuplot.png :exports results
  set xdata time
  set style data linespoints
  set timefmt "%Y/%m/%d"
  plot data u 1:2
#+end_src

#+RESULTS:
[[file:gnuplot.png]]

All you have to do is change the gnuplot block to read from the new table. Also, I used a python block to implement the transformation, but it could easily be done in any Org babel-supported language.

  • 1
    I had to change the code a little because of an old Python on Debian 10 print(("| {col1} | {col2} |").format( col1=l[0], col2=l[1].split(' ')[0] )) – driva May 27 '20 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.