Implement log scale for y axis

This commit is contained in:
Peter J. Holzer 2022-12-31 16:58:38 +01:00 committed by Peter J. Holzer
parent d6bdaa4128
commit fedc4c66ac
1 changed files with 191 additions and 124 deletions

View File

@ -63,8 +63,10 @@ class Dashboard:
class Widget:
def __init__(self, d):
log.debug("")
self.type = d["type"]
self.stops = d["stops"]
self.yscale = d.get("yscale", "linear")
log.debug("data = %s", d["data"])
self.lts = LTS(id=d["data"][0]) # by default we handle only one data source
pass
@ -140,6 +142,7 @@ class Widget:
class TimeSeries(Widget):
def __init__(self, d):
log.debug("")
super().__init__(d)
pass
@ -149,31 +152,18 @@ class TimeSeries(Widget):
x = n - math.log((t_last - t) / (n*dt) + 1) * n / k
return x
data = self.lts.data
n = len(data)
t_last = data[-1][0]
if len(data) < 5:
return "(not enough data)"
dt = (t_last - data[-5][0]) / 4
k = math.log((t_last - data[0][0]) / dt / n + 1)
max_value = max([d[1] for d in self.lts.data])
max_value = max(max_value, 0.001) # ensure positive
v_data = []
for i in range(n):
t = data[i][0]
x = t2x(t)
t_h = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))
#print(t, t_h, x)
v_data.append(
{
"t": t,
"v": data[i][1],
"x": x,
"y": (1 - data[i][1]/max_value) * 200,
"color": self.criticalcolor(data[i][1]),
})
def v2y(v):
if self.yscale == "log":
return (1 - math.log(v / min_value)
/ math.log(max_value / min_value)
) * 200
elif self.yscale == "linear":
return (1 - v/max_value) * 200,
else:
raise ValueError(f"Unknown yscale {self.yscale}")
def set_x_tickmarks():
log.debug("")
tickmarks = []
t = v_data[-1]["t"]
x = v_data[-1]["x"]
@ -310,14 +300,89 @@ class TimeSeries(Widget):
d = datetime.datetime.fromtimestamp(t)
tickmarks.append({"t": t, "t_h": d.strftime("%Y-%m-%d %H:%M:%S"), "x": x})
self.x_tickmarks = tickmarks
log.debug("")
def set_y_tickmarks():
log.debug("")
self.y_tickmarks = []
if self.yscale == "linear":
log.debug("")
step = 10 ** math.floor(math.log10(max_value))
v = 0
while v < max_value:
self.y_tickmarks.append({"y": (1 - v/max_value) * 200, "v_h": str(v)})
self.y_tickmarks.append({"y": v2y(v), "v_h": str(v)})
v += step
log.debug("")
elif self.yscale == "log":
log.debug("")
v = 10 ** math.ceil(math.log10(min_value))
log.debug("v = %s", v)
if v > max_value:
# Implement that when it happens
log.warning("No tickmark between %s and %s", min_value, max_value)
return
while v <= max_value:
y = v2y(v)
log.debug("v = %s, y = %s", v, y)
self.y_tickmarks.append({"y": y, "v_h": str(v)})
v *= 10
log.debug("")
else:
log.debug("")
raise ValueError(f"Unknown yscale {self.yscale}")
log.debug("")
log.debug("in graph")
data = self.lts.data
n = len(data)
t_last = data[-1][0]
if len(data) < 5:
return "(not enough data)"
dt = (t_last - data[-5][0]) / 4
k = math.log((t_last - data[0][0]) / dt / n + 1)
max_value = max([d[1] for d in self.lts.data])
max_value = max(max_value, 0.001) # ensure positive
if self.yscale == "log":
try:
min_value = min(d[1] for d in self.lts.data if d[1] > 0)
except ValueError:
# no non-negative values
min_value = max_value / 2
if any(True for d in self.lts.data if d[1] < min_value):
# if there are values smaller than the minimum (i.e.
# 0 or negative unless we have a configured minimum)
# reduce the minimum again so that we have a distinct
# value to clamp to.
min_value /= 2
if min_value == max_value:
# Make sure min_value is less than max_value
min_value /= 2
log.debug("min_value = %s, max_value = %s", min_value, max_value)
log.debug("collecting data")
v_data = []
for i in range(n):
t = data[i][0]
v = data[i][1]
x = t2x(t)
t_h = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t))
y = v2y(v)
#print(t, t_h, x)
v_data.append(
{
"t": t,
"v": v,
"x": x,
"y": y,
"color": self.criticalcolor(v),
})
log.debug("setting tickmarks")
set_x_tickmarks()
set_y_tickmarks()
log.debug("assembling svg")
html = ""
html += "<svg width=1150 height=300>"
for tm in self.x_tickmarks:
@ -329,9 +394,11 @@ class TimeSeries(Widget):
for v in v_data:
html += f"<circle cx={v['x']} cy={v['y']} r=3 fill='{v['color']}' />"
html += "</svg>"
log.debug("len(html) = %s", len(html))
return Markup(html)
def as_html(self):
log.debug("in as_html")
return Markup(render_template("timeseries.html", widget=self))
class Gauge(Widget):