generate chart.css based charts

This commit is contained in:
olli 2023-10-20 17:42:31 +02:00
parent 6cddba7cec
commit 54078dc7ad

105
dabo/functions/genchart.sh Normal file
View File

@ -0,0 +1,105 @@
function genchart {
# generate css chart (line diagram) from csv file or simple file with number per line - needed charts.css included in webppage
local mark
local lastmark
local file=$1
local lastlines=$2
[ -z "${lastlines}" ] && lastlines=50
local fields=$3
[ -z "${fields}" ] && fields=1
local colors=$4
[ -z "${colors}" ] && colors="White,Gold,Silver,Blue,DarkMagenta,DarkViolet,Indigo,MediumBlue,DarkOrchid,MidnightBlue,CornflowerBlue,CadetBlue,DarkCyan,DarkSlateBlue,DeepSkyBlue,DodgerBlue,Teal"
local awkfields=$(echo "${fields}" | sed 's/,/ \",\" \$/g; s/^/\$/')
#tail -n ${lastlines} "${file}" | cut -d, -f${fields} | egrep "^[-0-9]" >${g_tmp}/g_genchart_data
tail -n ${lastlines} "${file}" | awk -F',' "{ print $awkfields }" | perl -pe 's/,,+//g' | egrep "^[-0-9]" >${g_tmp}/g_genchart_data
lines=$(cat ${g_tmp}/g_genchart_data | wc -l)
#head -n1 "${file}" | cut -d, -f${fields} >${g_tmp}/g_genchart_headline
head -n1 "${file}" | awk -F',' "{ print $awkfields }" >${g_tmp}/g_genchart_headline
local time_from=$(tail -n ${lastlines} "${file}" | head -n1 | cut -d, -f1)
local time_to=$(tail -n1 "${file}" | cut -d, -f1)
local highest=$(cat ${g_tmp}/g_genchart_data | sed 's/,/\n/g' | sort -n | egrep "^[-0-9]" | tail -n1 | sed 's/^-//')
local lowest=$(cat ${g_tmp}/g_genchart_data | sed 's/,/\n/g' | sort -n | egrep "^[-0-9]" | head -n1)
if echo ${lowest} | grep -q '^-'
then
lowest=$(echo ${lowest} | sed 's/^-//')
local calc="+ ${lowest}) / (${highest} + ${lowest}"
local calcnull="(0 $calc)"
else
local calc="- ${lowest}) / (${highest} - ${lowest}"
local calcnull="0"
fi
#local divideby=$(echo "$highest+$lowest" | bc -l | sed 's/^\./0./; s/^-\./-0./')
local fieldsnum=$(cat ${g_tmp}/g_genchart_headline | sed 's/,/\n/g' | wc -l)
local color="green"
tail -n1 ${g_tmp}/g_genchart_data | cut -d, -f1 | grep -q "^-" && color="red"
mkdir ${g_tmp}/g_genchart
local RND=$RANDOM
echo "<table id='noborder' width='100%'><tr><td id='noborder' width='100%'>"
echo "<div id='$RND'>"
echo "<table class='charts-css line show-data-on-hover'><caption> $RND </caption>"
local line
for fieldnum in $(seq ${fieldsnum} | tac)
do
linecolor=$(echo "$colors" | cut -d, -f${fieldnum})
linename=$(cat ${g_tmp}/g_genchart_headline | cut -d, -f${fieldnum} | tr [:lower:] [:upper:])
if [ ${fieldnum} -eq 1 ]
then
echo "<b><font color='${color}'>${linename}</font></b><br>"
else
echo "<b><font color='${linecolor}'>${linename}</font></b><br>"
fi >>${g_tmp}/g_genchart/legend
local linenum=1
for line in $(cat ${g_tmp}/g_genchart_data)
do
for mark in $(echo ${line} | cut -d, -f${fieldnum})
do
[ -z "${lastmark}" ] && lastmark=${mark}
local calcstart="(${lastmark} ${calc})"
local calcend="(${mark} ${calc})"
if [ ${fieldnum} -eq 1 ]
then
echo "<td style='--color: grey; --start: calc( ${calcnull} ); --end: calc( ${calcnull} );'> </td>"
echo "<td style='--color: ${color}; --start: calc( ${calcstart} ); --end: calc( ${calcend} );'> <span class='tooltip'> ${mark} </span> </td>"
else
echo "<td style='--color: ${linecolor}; --start: calc( ${calcstart} ); --end: calc( ${calcend} );'> </td>"
fi >>${g_tmp}/g_genchart/${linenum}
((linenum=linenum+1))
lastmark=${mark}
done
done
done
# put all lines together
for linenum in $(seq 2 ${lines})
do
echo "<tr>"
cat ${g_tmp}/g_genchart/${linenum}
echo "</tr>"
done
echo "</table></div>"
# legend
if grep -q ',' ${g_tmp}/g_genchart_headline
then
echo "<td id='noborder'>"
tac ${g_tmp}/g_genchart/legend
echo "</td>"
fi
if echo ${time_from} | egrep -q '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]' && echo ${time_to} | egrep -q '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'
then
echo "</tr><td id='noborder'><table width='100%' id='noborder'><tr><td id='noborder'><p style='text-align:left;'>${time_from}</p></td><td id='noborder' width='100%'><hr></td><td id='noborder'><p style='text-align:right;'>${time_to}</p></td></tr></table></td><td id='noborder'></td>"
fi
echo "</tr></table>"
rm -r ${g_tmp}/g_genchart
}