Add click handler for going from daily to hourly chart view by clicking chart at specific date

This commit is contained in:
CasperVerswijvelt 2021-05-01 22:00:12 +02:00
parent 7c69b0bd81
commit 4cd0c4735d
4 changed files with 13 additions and 2 deletions

View File

@ -207,6 +207,7 @@ class Service(models.Model):
# Show hourly chart for date ranges of 3 days or less, otherwise daily chart # Show hourly chart for date ranges of 3 days or less, otherwise daily chart
if (end_time - start_time).days < 3: if (end_time - start_time).days < 3:
session_chart_tooltip_format = "MM/dd HH:mm" session_chart_tooltip_format = "MM/dd HH:mm"
session_chart_granularity = "hourly"
session_chart_data = { session_chart_data = {
k["hour"]: k["count"] k["hour"]: k["count"]
for k in sessions.annotate(hour=TruncHour("start_time")) for k in sessions.annotate(hour=TruncHour("start_time"))
@ -220,6 +221,7 @@ class Service(models.Model):
session_chart_data[hour] = 0 if hour <= tz_now else None session_chart_data[hour] = 0 if hour <= tz_now else None
else: else:
session_chart_tooltip_format = "MMM d" session_chart_tooltip_format = "MMM d"
session_chart_granularity = "daily"
session_chart_data = { session_chart_data = {
k["date"]: k["count"] k["date"]: k["count"]
for k in sessions.annotate(date=TruncDate("start_time")) for k in sessions.annotate(date=TruncDate("start_time"))
@ -259,6 +261,7 @@ class Service(models.Model):
] ]
), ),
"session_chart_tooltip_format": session_chart_tooltip_format, "session_chart_tooltip_format": session_chart_tooltip_format,
"session_chart_granularity": session_chart_granularity,
"online": True, "online": True,
} }

View File

@ -51,7 +51,7 @@
</div> </div>
<hr class="sep h-4"> <hr class="sep h-4">
<div style="bottom: -1px;"> <div style="bottom: -1px;">
{% include 'dashboard/includes/time_chart.html' with data=stats.session_chart_data sparkline=True height=100 name=object.uuid tooltip_format=stats.session_chart_tooltip_format %} {% include 'dashboard/includes/time_chart.html' with data=stats.session_chart_data sparkline=True height=100 name=object.uuid tooltip_format=stats.session_chart_tooltip_format granularity=stats.session_chart_granularity %}
</div> </div>
{% endwith %} {% endwith %}
</a> </a>

View File

@ -37,6 +37,14 @@
stops: [0, 75, 100] stops: [0, 75, 100]
}, },
}, },
{% if granularity == "daily" and click_zoom %}
events: {
markerClick: function(event, chartContext, { seriesIndex, dataPointIndex, w: {config}}) {
const day = config.series[seriesIndex].data[dataPointIndex].x
window.location.href = `?startDate=${day}&endDate=${day}`
},
},
{% endif %}
}, },
grid: { grid: {
padding: { padding: {

View File

@ -86,7 +86,7 @@
{% endwith %} {% endwith %}
</div> </div>
<div class="card ~neutral !low py-0 mb-6"> <div class="card ~neutral !low py-0 mb-6">
{% include 'dashboard/includes/time_chart.html' with data=stats.session_chart_data tooltip_format=stats.session_chart_tooltip_format %} {% include 'dashboard/includes/time_chart.html' with data=stats.session_chart_data tooltip_format=stats.session_chart_tooltip_format granularity=stats.session_chart_granularity click_zoom=True %}
</div> </div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6"> <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
<div class="card ~neutral !low limited-height py-2"> <div class="card ~neutral !low limited-height py-2">