From 912b6505f1335a1f9eabf55045b67e38738bdaf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20W=C3=BChrer?= Date: Sun, 12 Apr 2026 16:51:43 +0200 Subject: piechart --- bin/piechart | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 bin/piechart (limited to 'bin/piechart') diff --git a/bin/piechart b/bin/piechart new file mode 100755 index 0000000..c1a57ee --- /dev/null +++ b/bin/piechart @@ -0,0 +1,105 @@ +#!/usr/bin/awk -f +## piechart +# +# BUGS: +# Adjacient colours could have higher contrast. +# Negative angles. +# + +function hex(i) { + if (i < 16) { + if (i < 1) { + return "" + } else if (i < 10) { + return "" int(i) + } else switch (int(i)) { + case 10: return "a" + case 11: return "b" + case 12: return "c" + case 13: return "d" + case 14: return "e" + case 15: return "f" + } } + return hex(i/16) hex(i%16) +} +function phex(i) { + if (i==0) { return "00" } + if (i<16) { return "0" hex(i) } + return hex(i) +} + +BEGIN { + print "" + print "" + print "" + sum = 0 + values[0]=0 + labels[0]="" +} +END { + tau = 2*atan2(0,-1) + r = 100 + + progress = 0 + overlap = 0 + for (i = 1;i < length(values);i++) { + label = labels[i] + number = values[i] + angle = tau*number/sum + fangle = angle < 0 ? -angle : (angle-overlap) + + x1 = r*cos(progress); + y1 = r*sin(progress); + x2 = r*cos(progress+fangle); + y2 = r*sin(progress+fangle); + divider = progress+fangle/2; + xo = 250+(3*r/2*cos(divider)); + yo = 250+(3*r/2*sin(divider)); + + # rr = phex(int(0xff*((divider)/(tau)))); + # gg = phex(int(0xff*((divider-tau/3)/(tau)))); + # bb = phex(int(0xff*((divider-2*tau/3)/(tau)))); + + rr = int(0xff*((divider)/(tau)))%0x100; + gg = int(0xff*((divider+2*tau/3)/(tau)))%0x100; + bb = int(0xff*((divider+tau/3)/(tau)))%0x100; + + # Mx,y : move to x,y + # Lx,y : draw line to x,y + # Arx,ry rot 0,0 x,y : arc with radii rx,ry rotated by rot large-arch or not,clockwise or not to point x,y + # z : closes the path + printf "\n", rr,gg,bb,250+x1,250+y1,r,r,(fangle>tau/2),1,250+x2,250+y2 + if (label != "") { + print "" + print "" label "" + } + + progress += fangle; + overlap -= angle; # multiple overlaps accumulate + if (overlap<0) { overlap=0 } + } + print "" +} +# At the very least, we need a true value. +# Everything following it can be label or whatever. +/^[0-9]/ { + values[length(values)] = $1 + sum += $1 + labels[length(labels)] = substr($0, length($1)+2) +} +# If the value is negative, it is meant to overlap with the previous record. Or records. +# The value would have to be ignored for computing the sum total. +/^-[0-9]/ { + values[length(values)] = $1 + labels[length(labels)] = substr($0, length($1)+2) +} +# If there is no number, the line is obviously a comment and should be ignored. +!/^-?[0-9]/{ + print "" +} -- cgit v1.2.3