forked from pietervdvn/MapComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenPlot.py
75 lines (58 loc) · 2.03 KB
/
GenPlot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import sys
from datetime import datetime
from matplotlib import pyplot
def pyplot_init():
pyplot.close('all')
pyplot.figure(figsize=(14, 8), dpi=200)
pyplot.xticks(rotation='vertical')
pyplot.grid()
def genKeys(data, type):
keys = map(lambda kv: kv["key"], data)
if type == "date":
keys = map(lambda key: datetime.strptime(key, "%Y-%m-%dT%H:%M:%S.000Z"), keys)
return list(keys)
def createPie(options):
data = options["plot"]["count"]
keys = genKeys(data, options["interpetKeysAs"])
values = list(map(lambda kv: kv["value"], data))
total = sum(map(lambda kv: kv["value"], data))
first_pct = data[0]["value"] / total
pyplot_init()
pyplot.pie(values, labels=keys, startangle=(90 - 360 * first_pct / 2))
def createBar(options):
data = options["plot"]["count"]
keys = genKeys(data, options["interpetKeysAs"])
values = list(map(lambda kv: kv["value"], data))
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()
pyplot_init()
title = sys.argv[1]
pyplot.title = title
names = []
while (True):
line = sys.stdin.readline()
if line == "" or line == "\n":
if (len(names) > 1):
pyplot.legend(loc="upper left", ncol=3)
pyplot.savefig(title + ".png", dpi=400, facecolor='w', edgecolor='w',
bbox_inches='tight')
break
options = json.loads(line)
print("Creating " + options["plot"]["type"] + " '" + options["name"] + "'")
names.append(options["name"])
if (options["plot"]["type"] == "pie"):
createPie(options)
elif (options["plot"]["type"] == "bar"):
createBar(options)
elif (options["plot"]["type"] == "line"):
createLine(options)
else:
print("Unkown type: " + options.type)
print("Plot generated")