From: Philipp Spitzer Date: Mon, 28 Jan 2019 22:32:04 +0000 (+0100) Subject: Add table to report. X-Git-Url: https://git.toastfreeware.priv.at/chrisu/seepark.git/commitdiff_plain/d6fe9f73ee8f656d85795574ef2debe7a72d9706 Add table to report. --- diff --git a/web/seepark_web.py b/web/seepark_web.py index 131489b..fb522ba 100644 --- a/web/seepark_web.py +++ b/web/seepark_web.py @@ -30,6 +30,15 @@ MONTH_DE = [ 'November', 'Dezember'] +DAY_OF_WEEK_DE = [ + 'Montag', + 'Dienstag', + 'Mittwoch', + 'Donnerstag', + 'Freitag', + 'Samstag', + 'Sonntag'] + # https://stackoverflow.com/a/37350445 def sqlalchemy_model_to_dict(model): @@ -305,9 +314,9 @@ def report(year, month): begin = datetime.datetime(year, month, 1) end = add_month(begin) - data = list(select_sensordata_grouped(mainsensor, 'Wassertemperatur', begin, end)) - x = [d.timestamp for d in data] - y = [d.value for d in data] + data = list(select_sensordata(mainsensor, 'Wassertemperatur', begin, end)) + x = np.array([d.timestamp for d in data]) + y = np.array([d.value for d in data]) days_datetime = [] d = begin @@ -319,6 +328,36 @@ def report(year, month): binary_pdf = io.BytesIO() with PdfPages(binary_pdf) as pdf: a4 = (21./2.54, 29.7/2.54) + title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year) + report_times = [datetime.time(10), datetime.time(15)] + + # table + plt.figure(figsize=a4) + columns = ['Datum'] + for t in report_times: + columns.append('Wassertemperatur {} Uhr'.format(t.hour)) + cells = [] + for d in days_datetime: + cell = ['{}, {}. {}'.format(DAY_OF_WEEK_DE[d.weekday()], d.day, MONTH_DE[d.month-1])] + for t in report_times: + report_datetime = datetime.datetime.combine(d.date(), t) + closed_index = np.argmin(np.abs(x - report_datetime)) + if abs(x[closed_index] - report_datetime) > datetime.timedelta(hours=1): + cell.append('N/A') + else: + value = y[closed_index] + cell.append('{:.1f}° C'.format(value)) + cells.append(cell) + + ax = plt.gca() + ax.table(cellText=cells, colLabels=columns, + loc='upper left') + ax.axis('off') + plt.title(title) + plt.subplots_adjust(left=0.1, right=0.9) # do not cut row labels + pdf.savefig() + + # graphic plt.figure(figsize=a4) plt.plot(x, y) plt.xticks(days_datetime, days_str, rotation='vertical') @@ -326,7 +365,6 @@ def report(year, month): plt.ylabel('Temparatur in °C') plt.axis(xmin=begin, xmax=end) plt.grid() - title = 'Seepark Wassertemperatur {} {}'.format(MONTH_DE[begin.month-1], begin.year) plt.title(title) pdf.savefig()