mapcomplete/Docs/Tools/GenPlot.py

75 lines
2 KiB
Python
Raw Normal View History

2021-08-22 15:53:05 +02:00
import json
import sys
2021-11-07 16:34:51 +01:00
from datetime import datetime
from matplotlib import pyplot
2021-08-22 15:53:05 +02:00
def pyplot_init():
pyplot.close('all')
pyplot.figure(figsize=(14, 8), dpi=200)
pyplot.xticks(rotation='vertical')
pyplot.grid()
2021-11-07 16:34:51 +01:00
2021-08-22 15:53:05 +02:00
def genKeys(data, type):
keys = map(lambda kv: kv["key"], data)
if type == "date":
2021-11-07 16:34:51 +01:00
keys = map(lambda key: datetime.strptime(key, "%Y-%m-%dT%H:%M:%S.000Z"), keys)
2021-08-22 15:53:05 +02:00
return list(keys)
2021-11-07 16:34:51 +01:00
2021-08-22 15:53:05 +02:00
def createPie(options):
data = options["plot"]["count"]
keys = genKeys(data, options["interpetKeysAs"])
values = list(map(lambda kv: kv["value"], data))
2021-11-07 16:34:51 +01:00
total = sum(map(lambda kv: kv["value"], data))
2021-08-22 15:53:05 +02:00
first_pct = data[0]["value"] / total
2021-11-07 16:34:51 +01:00
2021-08-22 15:53:05 +02:00
pyplot_init()
pyplot.pie(values, labels=keys, startangle=(90 - 360 * first_pct / 2))
2021-11-07 16:34:51 +01:00
2021-08-22 15:53:05 +02:00
def createBar(options):
data = options["plot"]["count"]
keys = genKeys(data, options["interpetKeysAs"])
values = list(map(lambda kv: kv["value"], data))
2021-11-07 16:34:51 +01:00
2021-08-22 15:53:05 +02:00
pyplot.bar(keys, values, label=options["name"])
pyplot.legend()
def createLine(options):
data = options["plot"]["count"]
keys = genKeys(data, options["interpetKeysAs"])
values = list(map(lambda kv: kv["value"], data))
pyplot.plot(keys, values, label=options["name"])
pyplot.legend()
2021-08-22 15:53:05 +02:00
pyplot_init()
title = sys.argv[1]
pyplot.title = title
names = []
2021-11-07 16:34:51 +01:00
while (True):
2021-08-22 15:53:05 +02:00
line = sys.stdin.readline()
if line == "" or line == "\n":
2021-11-07 16:34:51 +01:00
if (len(names) > 1):
pyplot.legend(loc="upper left", ncol=3)
pyplot.savefig(title + ".png", dpi=400, facecolor='w', edgecolor='w',
2021-08-22 15:53:05 +02:00
bbox_inches='tight')
break
2021-11-07 16:34:51 +01:00
2021-08-22 15:53:05 +02:00
options = json.loads(line)
2021-11-07 16:34:51 +01:00
print("Creating " + options["plot"]["type"] + " '" + options["name"] + "'")
2021-08-22 15:53:05 +02:00
names.append(options["name"])
2021-11-07 16:34:51 +01:00
if (options["plot"]["type"] == "pie"):
2021-08-22 15:53:05 +02:00
createPie(options)
2021-11-07 16:34:51 +01:00
elif (options["plot"]["type"] == "bar"):
2021-08-22 15:53:05 +02:00
createBar(options)
elif (options["plot"]["type"] == "line"):
createLine(options)
2021-08-22 15:53:05 +02:00
else:
2021-11-07 16:34:51 +01:00
print("Unkown type: " + options.type)
print("Plot generated")