在neo Emacs中制作表格和绘图

2023-10-20
3 min read

在Neo Emacs中制作表格和绘图可极大激发我们的生产力, 除了使用PlantUML、D2、dot graphviz等绘图工具外, 可以将 org table 与 gnuplot 结合形成强大的绘制统计报表的需求, 进行各类汇总报告分析.

Org Table

org table 支持进行列运算通过 #+TBLFM 标签编写表达式可对列进行多次的操作.

Table 1: 符号及意义
符号 意义 开始值
@ 指定行 1
.. @3.. 表示从第3行之后  
$ 指定列 1
:: 分隔符多个执行的表达式  
@# 行号 1

分析下面这行表达式的意义, 等号左边表示要赋值的表格的范围, @3..$1 表示赋值范围从第3行第1列至最后一行第1列.
等号右边是一个运算表达式, @2$1 表示初始值为第2行第1列 . @# 表示当前行号.
由于我们是从第3行开始赋值, 因此表达式首次执行该值为3, 最后进行了减 2, 则 @3$1=2019+3-2.
通过分析可知道,这个表达式其实是让第一列从第2行开始向后自增.

年度 平台A 平台B
2019 37 33
2020 75 25
2021 155 255
2022 268 398
2023 427 520
2024 640 740
2025 922 1022
2026 1271 1369
#+TBLFM: @3..$1=@2$1+@#-2

Gnuplot Histograms

Gnuplot comes with a large collection of demonstration plots. You can step through these interactively by typing the command below in gnuplot’s demo/ directory — it should be part of your installation, otherwise get it from the source code archive or file-by-file from the git repository.

Table 2: gnuplot 参数
command 说明 值域
data 选择数据表格 表格设置: #+TBLNAME: data-bb3
exports 导出方式 code、results、both、none
set title 设置标题 显示在图片表格上方
set boxwidth 设置柱宽 建议: 0-1之间的小数
set yrange 设置Y轴范围, 改boxwidth生效 [0:1600]
data u 每一行设置表示一个直方图 -
plot 2:xticlabel(1) 设置Y:X轴对应列 2表示Y轴高度列, 1表示用第1列作为X轴显示内容
plot title 设置图例名 “legend” with boxes
using 0:2:2 设置柱标记数 0:2:2, 最后一个2表示用哪列数据
using offset 设置柱标记数位置 0,0.5 分别表示x和y方向的偏移量
with 图表类型 histograms、points、linepoints
style 图表样式 linecolor、pointtype、pointsize
set title "2019-2026 Computing power prediction"
set grid
set autoscale
set boxwidth 0.9
set yrange [0:1800]
set style histogram gap 1
set style fill solid 1.0 border

plot data u 2:xticlabel(1) title "平台A" with histograms linecolor rgb "#3498DB", \
     '' using 0:2:2 with labels center offset -1.5,0.5 notitle ,\
     data u 3:xticlabel(1) title "平台B" with histograms linecolor rgb "#1ABC9C", \
     '' using 0:3:3 with labels center offset 1,0.5 notitle ,\
     data u 2:xticlabel(1) title "平台A" with linespoints pointtype 7 pointsize 0.6 linecolor 1, \
     data u 3:xticlabel(1) title "平台B" with linespoints pointtype 7 pointsize 0.6 linecolor 2

img

set title "2019-2026 computing power prediction line"
set grid
set autoscale
set boxwidth 0.9
set yrange [0:1600]
set style fill solid 1.0 border

plot data u 2:xticlabel(1) title "平台A" with line, \
     '' using 0:2:2 with labels center offset -1,0.5 notitle ,\
     data u 3:xticlabel(1) title "平台B" with line, \
     '' using 0:3:3 with labels center offset 1,0.5 notitle

img

饼状图案例

2024 工时详细分析

颜色 起始角度 弧度比例 百分比 标签 调百分比距离 调标签距离 错开微小弧度
1 1463 0 213.79338 59.38705 开发测试 0.9 1.1 0
1 1463 0 213.79338 59.38705 开发测试 0.9 1.1 0
2 328.5 213.79338 48.004871 13.334686 需求分析 0.9 1.1 0
3 175 261.79825 25.573371 7.1037142 文档编写 0.95 1.1 0
4 169 287.37162 24.696570 6.8601583 实施 0.95 1 0
5 125 312.06819 18.266694 5.0740817 Other 1 1 0
6 109 330.33488 15.928557 4.4245992 培训 1 1 0
7 82 346.26344 11.982951 3.3285975 项目管理 1 1 1.2
2 12 358.24639 1.75361 0.48711389 集成实施 1 1 -4
表格公式: #+TBLFM: $4=$2\*360/vsum(@1$2..@>$2)::$3=@-1$4+@-1$3::@1$3=0::@>$4=360-@>$3::$5=100\*$4/360  
计算弧度比例 $4=$2*360/vsum(@1$2..@>$2)
计算起始弧度 $3=@-1$4+@-1$3
设置初始弧度 @1$3=0
尾差最后弧度 @>$4=360-@>$3
计算百分比值 $5=100*$4/360

PS: gnuplot 对于部分中文字体渲染会失败

set title "Pie (donut) chart drawn with sectors"
reset

set terminal pngcairo transparent
set polar
set angles degree
set theta top cw
set xrange [-10:10]
set yrange [-10:10]
set size ratio -1

unset border
unset raxis
unset tics
unset key

# 起始角度:(内半径):弧度值:(外半径):颜色 ,PS:括号为值,不代括号是数据列
# (起始角度):(半径):labe值 ,PS:括号为值括号内用$符号是列引用,不代括号是数据列
plot data using 3:(3+0.9*$9):4:(5):1 with sectors lc variable lw 3 fill solid  border lc bgnd,\
     data using ($3+$4/2):(7*$7+0.9*$9):(sprintf("%.1f%",$5)) with labels font "Luminari,18" ,\
     data using ($3+$4/2):(9*$8+0.9*$9):($6) with labels font "獅尾福音黑體SC,15"
reset

img