-
Notifications
You must be signed in to change notification settings - Fork 0
/
senec_svg.py
84 lines (70 loc) · 3.4 KB
/
senec_svg.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
76
77
78
79
80
81
82
83
84
"""Read json with data and generate a html with a svg graphics"""
import json
import datetime
import matplotlib.pyplot as plt
import mpld3
# pylint: disable=too-many-locals
def create_interactive_html():
"""Create the html with the interactive svg"""
# Load the JSON data from the file
with open("senec_statistic_data.json", "r", encoding="utf-8") as json_file:
zeitverlauf_data = json.load(json_file)
# # Extract relevant data from the JSON
# intervalle = zeitverlauf_data.get("intervalle", [])
# Initialize lists to store data for plotting
timestamps = []
netzbezug = []
netzeinspeisung = []
speicherbeladung = []
speicherentnahme = []
stromerzeugung = []
stromverbrauch = []
# Parse and extract data
for interval in zeitverlauf_data:
startzeitpunkt = datetime.datetime.strptime(interval["startzeitpunkt"], "%Y-%m-%dT%H:%M:%SZ")
timestamps.append(startzeitpunkt)
netzbezug.append(interval["netzbezug"]["wert"])
netzeinspeisung.append(interval["netzeinspeisung"]["wert"])
speicherbeladung.append(interval["speicherbeladung"]["wert"])
speicherentnahme.append(interval["speicherentnahme"]["wert"])
stromerzeugung.append(interval["stromerzeugung"]["wert"])
stromverbrauch.append(interval["stromverbrauch"]["wert"])
# Create a time series graph
plt.figure(figsize=(12, 6))
netzbezug_line, = plt.plot(timestamps, netzbezug, label="Netzbezug")
netzeinspeisung_line, = plt.plot(timestamps, netzeinspeisung, label="Netzeinspeisung")
speicherbeladung_line, = plt.plot(timestamps, speicherbeladung, label="Speicherbeladung")
speicherentnahme_line, = plt.plot(timestamps, speicherentnahme, label="Speicherentnahme")
stromerzeugung_line, = plt.plot(timestamps, stromerzeugung, label="Stromerzeugung")
stromverbrauch_line, = plt.plot(timestamps, stromverbrauch, label="Stromverbrauch")
# Create tooltips with formatted text
tooltip_netzbezug = mpld3.plugins.LineLabelTooltip(netzbezug_line, label="Netzbezug")
tooltip_netzeinspeisung = mpld3.plugins.LineLabelTooltip(netzeinspeisung_line, label="Netzeinspeisung")
tooltip_speicherbeladung = mpld3.plugins.LineLabelTooltip(speicherbeladung_line, label="Speicherbeladung")
tooltip_speicherentnahme = mpld3.plugins.LineLabelTooltip(speicherentnahme_line, label="Speicherentnahme")
tooltip_stromerzeugung = mpld3.plugins.LineLabelTooltip(stromerzeugung_line, label="Stromerzeugung")
tooltip_stromverbrauch = mpld3.plugins.LineLabelTooltip(stromverbrauch_line, label="Stromverbrauch")
# Add tooltips to the plot
mpld3.plugins.connect(
plt.gcf(),
tooltip_netzbezug,
tooltip_netzeinspeisung,
tooltip_speicherbeladung,
tooltip_speicherentnahme,
tooltip_stromerzeugung,
tooltip_stromverbrauch,
)
plt.xlabel("Timestamp")
plt.ylabel("Energy (Wh)")
plt.title("Energy Data Over Time")
plt.legend()
# Make the plot interactive as an HTML file
interactive_plot = mpld3.fig_to_html(plt.gcf())
# Save the interactive HTML plot to a file
with open("senec_graph.html", "w", encoding="utf-8") as html_file:
html_file.write(interactive_plot)
# Show the interactive plot (for Jupyter Notebook)
# mpld3.display()
# Close the Matplotlib plot
plt.close()
print("Interactive SVG graph with tooltips saved to 'interactive_graph.html'")