Merge remote-tracking branch 'srht/master'
[worg.git] / org-contrib / orgtbl-ascii-plot.org
blob6758345fa96cba28c6cb95c8043ba4c9ba066538
1 #+TITLE: orgtbl-ascii-plot.el -- plots in ascii-art
2 #+OPTIONS: ^:{} author:Thierry Banel author:Michael Brand toc:nil
4 # This file is released by its authors and contributors under the GNU
5 # Free Documentation license v1.3 or later, code examples are released
6 # under the GNU General Public License v3 or later.
8 What about text-only plots in tables ?
10 All in Emacs, without any external package.
12 * A few examples
14 : | ! |  x |    sin(x/4) |              |
15 : |---+----+-------------+--------------|
16 : | # |  0 |           0 | WWWWWW       |
17 : | # |  1 |  0.32719470 | WWWWWWWW     |
18 : | # |  2 |  0.61836980 | WWWWWWWWWh   |
19 : | # |  3 |  0.84147098 | WWWWWWWWWWW  |
20 : | # |  4 |  0.97193790 | WWWWWWWWWWWV |
21 : | # |  5 |  0.99540796 | WWWWWWWWWWWW |
22 : | # |  6 |  0.90929743 | WWWWWWWWWWWu |
23 : | # |  7 |  0.72308588 | WWWWWWWWWW-  |
24 : | # |  8 |  0.45727263 | WWWWWWWWh    |
25 : | # |  9 |  0.14112001 | WWWWWWV      |
26 : | # | 10 | -0.19056796 | WWWWH        |
27 : | # | 11 | -0.50127705 | WWW          |
28 : | # | 12 | -0.75680250 | Wu           |
29 : | # | 13 | -0.92901450 | ;            |
30 : | # | 14 | -0.99895492 |              |
31 : | # | 15 | -0.95892427 | :            |
32 : | # | 16 | -0.81332939 | W.           |
33 : | # | 17 | -0.57819824 | WWu          |
34 : | # | 18 | -0.27941550 | WWWW-        |
35 : | # | 19 | 0.050127010 | WWWWWW-      |
36 : | # | 20 |  0.37415123 | WWWWWWWW:    |
37 : | # | 21 |  0.65698660 | WWWWWWWWWH   |
38 : | # | 22 |  0.86749687 | WWWWWWWWWWW: |
39 : | # | 23 |  0.98250779 | WWWWWWWWWWWH |
40 : | # | 24 |  0.98935825 | WWWWWWWWWWWH |
41 : | # | 25 |  0.88729411 | WWWWWWWWWWW- |
42 : | # | 26 |  0.68755122 | WWWWWWWWWW.  |
43 : | # | 27 |  0.41211849 | WWWWWWWWu    |
44 : | # | 28 | 0.091317236 | WWWWWWu      |
45 : | # | 29 | -0.23953677 | WWWWl        |
46 : | # | 30 | -0.54402111 | WWh          |
47 : | # | 31 | -0.78861628 | W-           |
48 : #+TBLFM: $3=sin($x/3);R::$4='(orgtbl-ascii-draw $3 -1 1)
50 : | ! |  x | x^2 |              |
51 : |---+----+-----+--------------|
52 : | # |  0 |   0 |              |
53 : | # |  1 |   1 | .            |
54 : | # |  2 |   4 | :            |
55 : | # |  3 |   9 | u            |
56 : | # |  4 |  16 | W            |
57 : | # |  5 |  25 | Wu           |
58 : | # |  6 |  36 | WW:          |
59 : | # |  7 |  49 | WWH          |
60 : | # |  8 |  64 | WWWV         |
61 : | # |  9 |  81 | WWWWH        |
62 : | # | 10 | 100 | WWWWWW       |
63 : | # | 11 | 121 | WWWWWWW-     |
64 : | # | 12 | 144 | WWWWWWWWl    |
65 : | # | 13 | 169 | WWWWWWWWWW.  |
66 : | # | 14 | 196 | WWWWWWWWWWWV |
67 : #+TBLFM: $3=$x*$x::$4='(orgtbl-ascii-draw $3 0 200)
69 : | ! |  x |   sqrt(x) |              |
70 : |---+----+-----------+--------------|
71 : | # |  0 |         0 |              |
72 : | # |  1 |         1 | WWW          |
73 : | # |  2 | 1.4142136 | WWWW:        |
74 : | # |  3 | 1.7320508 | WWWWW:       |
75 : | # |  4 |         2 | WWWWWW       |
76 : | # |  5 | 2.2360680 | WWWWWWh      |
77 : | # |  6 | 2.4494897 | WWWWWWW-     |
78 : | # |  7 | 2.6457513 | WWWWWWWH     |
79 : | # |  8 | 2.8284271 | WWWWWWWWu    |
80 : | # |  9 |         3 | WWWWWWWWW    |
81 : | # | 10 | 3.1622777 | WWWWWWWWWu   |
82 : | # | 11 | 3.3166248 | WWWWWWWWWH   |
83 : | # | 12 | 3.4641016 | WWWWWWWWWW;  |
84 : | # | 13 | 3.6055513 | WWWWWWWWWWV  |
85 : | # | 14 | 3.7416574 | WWWWWWWWWWW: |
86 : | # | 15 | 3.8729833 | WWWWWWWWWWWl |
87 : | # | 16 |         4 | WWWWWWWWWWWW |
88 : #+TBLFM: $3=sqrt($x)::$4='(orgtbl-ascii-draw $3 0 4)
90 : | ! |  x |         1/x |              |
91 : |---+----+-------------+--------------|
92 : | # |  1 |           1 | WWWWWWWWWWWW |
93 : | # |  2 |         0.5 | WWWWWW       |
94 : | # |  3 |  0.33333333 | WWWW         |
95 : | # |  4 |        0.25 | WWW          |
96 : | # |  5 |         0.2 | WW;          |
97 : | # |  6 |  0.16666667 | WW           |
98 : | # |  7 |  0.14285714 | Wh           |
99 : | # |  8 |       0.125 | Wu           |
100 : | # |  9 |  0.11111111 | W-           |
101 : | # | 10 |         0.1 | W:           |
102 : | # | 11 | 0.090909091 | W.           |
103 : | # | 12 | 0.083333333 | W            |
104 : | # | 13 | 0.076923077 | H            |
105 : | # | 14 | 0.071428571 | H            |
106 : | # | 15 | 0.066666667 | V            |
107 : | # | 16 |      0.0625 | V            |
108 : | # | 17 | 0.058823529 | h            |
109 : #+TBLFM: $3=1/$x::$4='(orgtbl-ascii-draw $3 0 1)
111 * How to
113 1- Install it.
114    - Through ELPA (Emacs package system). Add the repository to your =.emacs=:
115      : (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
116      List all packages with =M-x packages-list-packages=,
117      look for =orgtbl-ascii-plot= and install it.
118    - Alternatively, download the lisp file
119      https://github.com/tbanel/orgtblasciiplot/blob/master/orgtbl-ascii-plot.el
120      and load it (for example by typing
121      =M-x load-file RET orgtbl-ascii-plot.el RET=)
123 2- Put the cursor on the column to plot.
125 3- Type
126    - either =C-c " a=
127    - or =M-x orgtbl-ascii-plot=
128    This adds a new ascii-art column.
130 4- The width in characters of the plot may be specified
131    with a numeric prefix:
132    - =Esc 30 C-c " a=
133    - =Esc 30 M-x orgtbl-ascii-plot=
135 * Additional details
137 The column containing the plot has a column formula.
138 When the data column changes, refresh the plot with =C-u C-c *=.
140 The plotting column may be handled in the same way as any other:
141 - remove with =M-S-left=
142 - moved left with =M-left=
143 - moved right with =M-right=
145 The column formula may be changed. Here is how it works:
146   : '(orgtbl-ascii-draw $3 15 75 12)
147   - =$3= is the reference to the data column, here the third column.
148   - =15= is the minimum value to be displayed
149     (values lower than that will display "too small")
150   - =75= is the maximum value to be displayed
151     (values higher than that will display "too large")
152   - =12= is the width of the column,
153     it is optional, and defaults to =12=
155 * Lisp code (pure ascii)
157 Moved here:
158 https://github.com/tbanel/orgtblasciiplot/blob/master/orgtbl-ascii-plot.el
160 * Example of extension: unicode characters
161 Here are two examples of different styles.
163 Unicode block characters are used to give a smooth effect.
164 See https://en.wikipedia.org/wiki/Block_Elements.
165 Use one of those drawing functions:
166 - =orgtbl-ascii-draw=   (the default ascii)
167 - =orgtbl-uc-draw-grid= (unicode with a grid effect)
168 - =orgtbl-uc-draw-cont= (smooth unicode)
170 This is best viewed with the "DejaVu Sans Mono" font (use =M-x set-default-font=).
172 Be aware that unicode support is not available everywhere.
173 For instance, LaTex export will not work.
174 If you plan to export your Org document,
175 either draw pure ascii plots,
176 or use ascii plots only for quick and throwable visualization.
178 : | ! |  x |    sin(x/4) | ascii        | uc grid      | uc cont      |
179 : |---+----+-------------+--------------+--------------+--------------|
180 : | # |  0 |           0 | WWWWWW       | ▉▉▉▉▉▉       | ██████       |
181 : | # |  1 |  0.32719470 | WWWWWWWH     | ▉▉▉▉▉▉▉▊     | ███████▉     |
182 : | # |  2 |  0.61836980 | WWWWWWWWWh   | ▉▉▉▉▉▉▉▉▉▋   | █████████▋   |
183 : | # |  3 |  0.84147098 | WWWWWWWWWWW  | ▉▉▉▉▉▉▉▉▉▉▉  | ███████████  |
184 : | # |  4 |  0.97193790 | WWWWWWWWWWWV | ▉▉▉▉▉▉▉▉▉▉▉▊ | ███████████▊ |
185 : | # |  5 |  0.99540796 | WWWWWWWWWWWW | ▉▉▉▉▉▉▉▉▉▉▉▉ | ████████████ |
186 : | # |  6 |  0.90929743 | WWWWWWWWWWW; | ▉▉▉▉▉▉▉▉▉▉▉▍ | ███████████▍ |
187 : | # |  7 |  0.72308588 | WWWWWWWWWW-  | ▉▉▉▉▉▉▉▉▉▉▎  | ██████████▎  |
188 : | # |  8 |  0.45727263 | WWWWWWWWh    | ▉▉▉▉▉▉▉▉▋    | ████████▊    |
189 : | # |  9 |  0.14112001 | WWWWWWV      | ▉▉▉▉▉▉▊      | ██████▊      |
190 : | # | 10 | -0.19056796 | WWWWV        | ▉▉▉▉▊        | ████▊        |
191 : | # | 11 | -0.50127705 | WWH          | ▉▉▊          | ██▉          |
192 : | # | 12 | -0.75680250 | W;           | ▉▍           | █▍           |
193 : | # | 13 | -0.92901450 | ;            | ▎            | ▍            |
194 : | # | 14 | -0.99895492 |              |              |              |
195 : | # | 15 | -0.95892427 | :            | ▏            | ▏            |
196 : | # | 16 | -0.81332939 | W.           | ▉            | █            |
197 : | # | 17 | -0.57819824 | WWu          | ▉▉▍          | ██▌          |
198 : | # | 18 | -0.27941550 | WWWW-        | ▉▉▉▉▎        | ████▎        |
199 : | # | 19 | 0.050127010 | WWWWWW-      | ▉▉▉▉▉▉▎      | ██████▎      |
200 : | # | 20 |  0.37415123 | WWWWWWWW:    | ▉▉▉▉▉▉▉▉▏    | ████████▎    |
201 : | # | 21 |  0.65698660 | WWWWWWWWWH   | ▉▉▉▉▉▉▉▉▉▊   | █████████▉   |
202 : | # | 22 |  0.86749687 | WWWWWWWWWWW: | ▉▉▉▉▉▉▉▉▉▉▉▏ | ███████████▏ |
203 : | # | 23 |  0.98250779 | WWWWWWWWWWWH | ▉▉▉▉▉▉▉▉▉▉▉▊ | ███████████▉ |
204 : | # | 24 |  0.98935825 | WWWWWWWWWWWH | ▉▉▉▉▉▉▉▉▉▉▉▊ | ███████████▉ |
205 : | # | 25 |  0.88729411 | WWWWWWWWWWW- | ▉▉▉▉▉▉▉▉▉▉▉▎ | ███████████▎ |
206 : | # | 26 |  0.68755122 | WWWWWWWWWW.  | ▉▉▉▉▉▉▉▉▉▉▏  | ██████████▏  |
207 : | # | 27 |  0.41211849 | WWWWWWWW;    | ▉▉▉▉▉▉▉▉▍    | ████████▍    |
208 : | # | 28 | 0.091317236 | WWWWWWu      | ▉▉▉▉▉▉▍      | ██████▌      |
209 : | # | 29 | -0.23953677 | WWWWu        | ▉▉▉▉▍        | ████▌        |
210 : | # | 30 | -0.54402111 | WWh          | ▉▉▋          | ██▋          |
211 : | # | 31 | -0.78861628 | W:           | ▉▏           | █▎           |
212 : #+TBLFM: $3=sin($x/3);R::$5='(orgtbl-uc-draw-grid $3 -0.99895492 0.99540796 12)::$6='(orgtbl-uc-draw-cont $3 -0.99895492 0.99540796 12)::$4='(orgtbl-ascii-draw $3 -0.99895492 0.99540796 12)
214 * Related work
216 Below is a unicode ASCII art solution with Zach Holman's shell script
217 package Spark: https://github.com/holman/spark
219 : | Measurement | t0 | t1 | t2 | t3 |  t4 | t5 | t6 | t7 | Sparkline |
220 : |-------------+----+----+----+----+-----+----+----+----+-----------|
221 : |           1 |  1 | 20 | 12 | 13 | 141 | 13 | 24 | 90 | ▁▁▁▁█▁▂▅  |
222 : |           2 |  0 |  1 |  2 |  3 |   4 |  5 |  6 |  7 | ▁▂▃▄▅▆▇█  |
223 : |           3 |  0 |  2 |  4 |  6 |   8 | 10 | 12 | 14 | ▁▂▃▄▅▆▇█  |
224 : |           4 | -7 | -6 | -5 | -4 |  -3 | -2 | -1 |  0 | ▁▂▃▄▅▆▇█  |
225 : |           5 |  0 |  0 |  0 |  0 |   0 |  0 |  0 |  0 | ▅▅▅▅▅▅▅▅  |
226 : #+TBLFM: $10 = '(org-trim (shell-command-to-string "spark $2..$9")); L
228 An ongoing discussion [2015-01-08] is taking place on the Org mailing list about
229 /sparlines/ (https://en.wikipedia.org/wiki/Sparkline) by Willem Rein
230 Oudshoorn and Michael Brand which could end in yet another way of
231 plotting Org tables.
233 * License (GNU GPL)
234 Copyright (C) 2013-2014 Thierry Banel, Michael Brand
236 Authors: Thierry Banel, Michael Brand
238 Keywords: table, plot, ascii, unicode
240 orgtbl-ascii-plot.el is free software: you can redistribute it
241 and/or modify it under the terms of the GNU General Public License
242 as published by the Free Software Foundation, either version 3 of
243 the License, or (at your option) any later version.
245 orgtbl-ascii-plot.el is distributed in the hope that it will be
246 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
247 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
248 GNU General Public License for more details.
250 the GNU General Public License can be obtained here:
251 <http://www.gnu.org/licenses/>.