Skip to content

Instantly share code, notes, and snippets.

@krsna1729
Forked from katef/plot.awk
Created July 24, 2020 17:43
Show Gist options
  • Save krsna1729/09db8c2511149a19fe6bd2242a376311 to your computer and use it in GitHub Desktop.
Save krsna1729/09db8c2511149a19fe6bd2242a376311 to your computer and use it in GitHub Desktop.
#!/usr/bin/awk -f
# This program is a copy of guff, a plot device. https://github.com/silentbicycle/guff
# My copy here is written in awk instead of C, has no compelling benefit.
# Public domain. @thingskatedid
# Run as awk -v x=xyz ... or env variables for stuff?
# TODO: skip title row, if alphanumeric. save the labels for a legend
# TODO: legend to the rhs
# TODO: moving average
# TODO: trend lines, or guess at complexities
# TODO: points vs. lines
# TODO: colourblind safe scheme
{
for (i = 1; i <= NF; i++) {
a[i "," NR] = $i
}
next
}
function normalise() {
for (i = 1; i <= NF; i++) {
max[i] = 0
min[i] = 0
for (j = 1; j <= NR; j++) {
if (a[i "," j] > max[i]) {
max[i] = a[i "," j]
} else
if (a[i "," j] < min[i]) {
min[i] = a[i "," j]
}
}
for (j = 1; j <= NR; j++) {
a[i "," j] -= min[i]
if (max[i] - min[i] > 0) {
a[i "," j] /= (max[i] - min[i])
}
}
}
}
# internal coordinates to svg coordinates
function point(x, y) {
return sprintf("%u,%u",
x * (width - 2 * xmargin) + xmargin,
(height - 2 * ymargin) - y * (height - 2 * ymargin) + ymargin)
}
function line(i) {
printf " <polyline stroke='%s' stroke-width='2' fill='none'", color[i]
printf " points='"
for (j = 1; j <= NR; j++) {
printf "%s ", point((j - 1) / NR, a[i "," j])
}
printf "'/>\n"
}
function circles(i) {
for (j = 1; j <= NR; j++) {
p = point((j - 1) / NR, a[i "," j])
split(p, q, ",")
printf " <circle cx='%u' cy='%u' r='1.5' fill='%s' stroke='%s'/>\n",
q[1], q[2], color[i], color[i]
}
}
function display(width, height) {
print "<?xml version='1.0'?>"
printf "<svg xmlns='%s' width='%u' height='%u' version='1.1'>\n",
"http://www.w3.org/2000/svg", width, height
for (i = 1; i <= NF; i++) {
# line(i)
circles(i)
}
print "</svg>"
}
END {
color[1] = "#377eb8"
color[2] = "#e41a1c"
color[3] = "#4daf4a"
color[4] = "#984ea3"
color[5] = "#ff7f00"
color[6] = "#ffff33"
color[7] = "#a65628"
color[8] = "#f781bf"
color[9] = "#999999"
width=320
height=120
xmargin=0
ymargin=10
# the data is scaled 0..1 for our internal coordinate space
normalise()
display(width, height)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment