ich habe das Problem das ich eine Datenbank habe wo mehrere Messstationen unterschiedliche Messwerte Reinschreiben z.B.: Messstation 1: Luftemperatur, Bodenfeuchtigkeit, Lux | Messstation 2: Luftemperatur, Wassertemperatur, Lux. Wenn ich jetzt die Datenbank auslese um die Messwerte in Graphen anzuzeigen, wird mir bei Messstation 1 ein leerer Graph mit der Wassertemperatur erstellt.
Wie kann ich bevor die Graphen generiert werden abfragen welche Messwerte es in der Station gibt und dann nur für diese Werte Plots erstellen?
Code: Alles auswählen
PLOTS = {
'luftfeuchtigkeit': {
'label1': 'Luftfeuchtigkeit innen',
'label2': 'Luftfeuchtigkeit ausen',
'ylabel': 'Luftfeuchtigkeit in %',
'werttypone': 1,
'werttyptwo': 3
},
'temperatur': {
'label1': 'Temperatur innen',
'label2': 'Temperatur ausen',
'ylabel': 'Temperatur in °C',
'werttypone': 2,
'werttyptwo': 4
},
'erdfeuchtigkeit': {
'label1': 'Erdfeuchtigkeit',
'ylabel': 'Erdfeuchtigkeit',
'werttypone': 5,
'werttyptwo': None
},
'lux': {
'label1': 'Lichtitensität(lux)',
'ylabel': 'Lichtitensität in Lux',
'werttypone': 6,
'werttyptwo': None
},
'wassertemperatur': {
'label1': 'Wassertemperatur',
'ylabel': 'Wassertemperatur in °C',
'werttypone': 7,
'werttyptwo': None
},
}
p_list = [
make_plot(mess_id, quantity, ask_length, None, None)
for quantity in PLOTS
]
def make_plot(mess_id, datatype, ask_length, start, end):
time = []
wertone = []
werttwo = []
plot_data = PLOTS[datatype]
conn = sqlite3.connect('data.db')
print ("Opened database " + datatype + " successfully")
cur = conn.cursor()
if plot_data['werttyptwo'] != None:
length = ask_length * 2
else:
length = ask_length
if start == None or end == None :
cur.execute("SELECT id, messstation_id, werttyp_id, timestamp, wert from messung WHERE messstation_id = ? AND werttyp_id = ? OR werttyp_id = ?ORDER BY timestamp DESC, werttyp_id ASC LIMIT ?;", [mess_id, plot_data['werttypone'], plot_data['werttyptwo'], length])
else:
cur.execute("SELECT id, messstation_id, werttyp_id, timestamp, wert from messung WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp DESC, werttyp_id ASC;", (start, end))
rows = cur.fetchall()
for row in rows:
if plot_data['werttypone'] == row[2] in row:
time.append(row[3])
wertone.append(row[4])
if plot_data['werttyptwo'] == row[2] in row:
werttwo.append(row[4])
conn.close()
time.reverse()
wertone.reverse()
if 'werttyptwo' in plot_data:
werttwo.reverse()
datetimes = [datetime.datetime.strptime(d, '%Y-%m-%d %H:%M') for d in time]
df = pd.DataFrame(time, columns=['date'])
df.loc[:, 'date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M')
plot = figure(plot_height=300, x_axis_type='datetime', sizing_mode='scale_width', tools="save", toolbar_location=None)
source = ColumnDataSource(data=dict( date=df['date'], wertone=wertone))
plot1 = plot.line('date', 'wertone', line_width=2, source=source, legend=plot_data['label1'])
if datatype == 'luftfeuchtigkeit' or datatype == 'temperatur':
source = ColumnDataSource(data=dict( date=df['date'], wertone=wertone, werttwo=werttwo))
plot2 = plot.line('date', 'werttwo', line_width=2, source=source, legend=plot_data['label2'], color="firebrick")
hover = HoverTool(
tooltips=[('Uhrzeit:', '@date{%d.%m.%Y %H:%M}'), (plot_data['label1'], '@wertone{0.00}') ],
formatters={'date' : 'datetime',},
mode='vline'
)
if datatype == 'luftfeuchtigkeit' or datatype == 'temperatur':
hover.renderers=[plot2]
hover.tooltips=[('Uhrzeit:', '@date{%d.%m.%Y %H:%M}'), (plot_data['label1'], '@wertone{0.00}'), (plot_data['label2'], '@werttwo{0.00}') ]
else:
hover.renderers=[plot1]
plot.add_tools(hover)
plot.xaxis.formatter=DatetimeTickFormatter( hours=["%H:%M"], days=["%d.%m.%Y %H:%M"], months=["%d.%m.%Y %H:%M"], years=["%d.%m.%Y %H:%M"])
plot.xaxis.major_label_orientation = pi/5
plot.yaxis.axis_label = plot_data['ylabel']
plot.legend.location ="top_left"
plot.legend.click_policy="hide"
return plot