From 39655363fa401657b8ba01fd6711edf03935a88c Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Wed, 13 Nov 2024 14:25:44 +0100 Subject: [PATCH] Don't crash on no-data passed to svg charts This could cause a divide-by-zero in some cases. Instead render the text "NO DATA". Normally, the entire svg would be exluded in this case, but in case it's not this will at least work. --- postgresqleu/util/templatetags/svgcharts.py | 7 +++++++ template/util/svgbarchart.svg | 4 ++++ template/util/svgpiechart.svg | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/postgresqleu/util/templatetags/svgcharts.py b/postgresqleu/util/templatetags/svgcharts.py index 479aec02..7d73db25 100644 --- a/postgresqleu/util/templatetags/svgcharts.py +++ b/postgresqleu/util/templatetags/svgcharts.py @@ -98,6 +98,13 @@ def svgbarchart(svgdata, legend=True, wratio=2): maxval = max([d['value'] for d in svgdata]) roundedmax = math.ceil(maxval / 2) * 2 + if not roundedmax: + # Graph is empty + return t.render({ + 'height': height, + 'width': width, + }) + for i, s in enumerate(svgdata): s['leftpos'] = itemwidth * i s['height'] = int((s['value'] / roundedmax) * graphratio * height) diff --git a/template/util/svgbarchart.svg b/template/util/svgbarchart.svg index 12a24c9e..a3589c3c 100644 --- a/template/util/svgbarchart.svg +++ b/template/util/svgbarchart.svg @@ -1,5 +1,6 @@ +{% if svgdata %} {%for label, pos in gridlines.items %} @@ -13,4 +14,7 @@ {{d.label}} {%endif%} {%endfor%} +{% else %} + NO DATA +{% endif %} diff --git a/template/util/svgpiechart.svg b/template/util/svgpiechart.svg index c8af3593..eef2f84e 100644 --- a/template/util/svgpiechart.svg +++ b/template/util/svgpiechart.svg @@ -1,5 +1,6 @@ +{%if slices %} {%for s in slices%}{%if s.drawslice%} @@ -11,4 +12,7 @@ {%for s in slices%} {%if s.percent%}{{s.percent}}%{%if s.popup%}{{s.popup}}{%endif%}{%endif%} {%endfor%} +{% else %} + NO DATA +{% endif %} -- 2.39.5