moth/histogram.py

65 lines
1.6 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
import points
import time
import os
2009-09-01 09:34:15 -06:00
import tempfile
2009-09-01 09:34:15 -06:00
def main(s=None):
scores = {}
now = 0
2009-09-01 09:34:15 -06:00
if not s:
s = points.Storage('scores.dat')
2009-09-01 09:34:15 -06:00
plotparts = []
teams = s.teams()
teamcolors = points.colors(teams)
2009-09-01 09:34:15 -06:00
catscores = {}
for cat in s.categories():
catscores[cat] = s.cat_points(cat)
2009-09-01 09:34:15 -06:00
scoresfile = tempfile.NamedTemporaryFile('w')
fn = scoresfile.name
i = 2
for team in teams:
2009-09-01 09:34:15 -06:00
plotparts.append('"%s" using 1:%d with lines linewidth 2 linetype rgb "#%s"' % (fn, i, teamcolors[team]))
scores[team] = 0
i += 1
def write_scores(t):
scoresfile.write('%d' % t)
for team in teams:
scoresfile.write('\t%f' % (scores[team]))
scoresfile.write('\n')
for when, cat, team, score in s.log:
if when > now:
if now:
write_scores(now)
now = when
pct = score / catscores[cat]
scores[team] += pct
#print('%d [%s] [%s] %d' % (when, cat, team, points))
write_scores(when)
scoresfile.flush()
gp = os.popen('gnuplot 2> /dev/null', 'w')
gp.write('set style data lines\n')
gp.write('set xdata time\n')
gp.write('set timefmt "%s"\n')
gp.write('set format ""\n')
gp.write('set border 3\n')
gp.write('set xtics nomirror\n')
gp.write('set ytics nomirror\n')
gp.write('set nokey\n')
gp.write('set terminal png transparent size 640,200 x000000 xffffff\n')
gp.write('set output "histogram.png"\n')
gp.write('plot %s\n' % ','.join(plotparts))
gp.close()
if __name__ == '__main__':
main()