Add ability to toggle between map chart and country/session table (#153)

* Add ability to toggle between geo map and table view

* Add back haaavk's bar visualisation for countries table

* Change text/location of map/table toggle buttons

* Add some common css to reusable class

* CSS consistency

* Use button, not span for interactive elements

Co-authored-by: R. Miles McCain <oci@sendmiles.email>
This commit is contained in:
Casper Verswijvelt 2021-07-20 05:04:56 +02:00 committed by GitHub
parent 31cb616242
commit de235c02a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 17 deletions

View File

@ -11,11 +11,6 @@
max-height: 400px; max-height: 400px;
} }
.force-limited-height {
max-height: 400px;
overflow: hidden;
}
.rf { .rf {
text-align: right !important; text-align: right !important;
} }
@ -28,6 +23,22 @@
max-width: 0; max-width: 0;
} }
.min-w-48 {
min-width: 48px;
}
.geo-table {
display: none;
}
.geo-card--use-table-view .geo-map {
display: none;
}
.geo-card--use-table-view .geo-table {
display: inline-block;
}
:root { :root {
--color-neutral-000: white; --color-neutral-000: white;
--color-neutral-50: #F8FAFC; --color-neutral-50: #F8FAFC;

View File

@ -33,7 +33,7 @@
} }
// Create datamap // Create datamap
const map = new Datamap({ var geoMap = new Datamap({
element: document.getElementById('map-chart'), element: document.getElementById('map-chart'),
projection: 'mercator', projection: 'mercator',
responsive: true, responsive: true,
@ -52,8 +52,15 @@
data: countryMapData, data: countryMapData,
aspectRatio: 0.68 aspectRatio: 0.68
}); });
map.updateChoropleth(countryMapColors); geoMap.updateChoropleth(countryMapColors);
// Handle resize. TODO: debounce? let debounceTimeout
window.onresize = () => map.resize(); const debounce = (func, debounce) => {
return function(event){
if(debounceTimeout) clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(func,debounce,event);
};
}
window.addEventListener("resize",debounce(() => geoMap.resize(), 100))
</script> </script>

View File

@ -97,7 +97,7 @@
{% include 'dashboard/includes/time_chart.html' with data=stats.chart_data tooltip_format=stats.chart_tooltip_format granularity=stats.chart_granularity click_zoom=True %} {% include 'dashboard/includes/time_chart.html' with data=stats.chart_data tooltip_format=stats.chart_tooltip_format granularity=stats.chart_granularity click_zoom=True %}
</div> </div>
{% endif %} {% endif %}
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6"> <div id="card-grid" 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">
<table class="table"> <table class="table">
<thead class="text-sm"> <thead class="text-sm">
@ -118,7 +118,7 @@
<td> <td>
<div class="flex justify-end items-center"> <div class="flex justify-end items-center">
{{location.count|intcomma}} {{location.count|intcomma}}
<span class="text-xs rf" style="min-width: 48px"> <span class="text-xs rf min-w-48">
({{location.count|percent:stats.hit_count}}) ({{location.count|percent:stats.hit_count}})
</span> </span>
</div> </div>
@ -132,10 +132,54 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<div class="card ~neutral !low force-limited-height py-2 overflow-y-hidden"> <div class="geo-map card ~neutral !low py-2 overflow-y-hidden">
<p class="text-sm font-semibold mx-2 p-2 border-b mb-2">Sessions by Geography</p> <p class="text-sm font-semibold p-2 border-b mb-2" style="color: var(--color-title)">
Sessions by Geography &nbsp
<button onclick="document.getElementById('card-grid').classList.add('geo-card--use-table-view')" class="text-xs select-none p-0 button ~urge !low">
(view table)
</button>
</p>
{% include 'dashboard/includes/map_chart.html' with countries=stats.countries %} {% include 'dashboard/includes/map_chart.html' with countries=stats.countries %}
</div> </div>
<div class="geo-table card ~neutral !low limited-height py-2">
<table class="table">
<thead class="text-sm">
<tr>
<th>
Country &nbsp
<button onclick="document.getElementById('card-grid').classList.remove('geo-card--use-table-view'); geoMap.resize()" class="text-xs select-none p-0 button ~urge !low">
(view map)
</button>
</th>
<th class="rf">Sessions</th>
</tr>
</thead>
<tbody>
{% for country in stats.countries %}
<tr>
<td class="truncate w-full max-w-0 relative" title="{{country.country|country_name}}">
{% include 'dashboard/includes/bar.html' with count=country.count max=stats.countries.0.count total=stats.session_count %}
<div class="relative flex items-center">
<span class="flex-none {{country.country|flag_class}}"></span> <span class="truncate">{{country.country|country_name}}</span>
</div>
</td>
<td>
<div class="flex justify-end items-center">
{{country.count|intcomma}}
<span class="text-xs rf min-w-48">
({{country.count|percent:stats.session_count}})
</span>
</div>
</td>
</tr>
{% empty %}
<tr>
<td><span class="text-gray-600">No data yet...</span></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="card ~neutral !low limited-height py-2"> <div class="card ~neutral !low limited-height py-2">
<table class="table"> <table class="table">
<thead class="text-sm"> <thead class="text-sm">
@ -156,7 +200,7 @@
<td> <td>
<div class="flex justify-end items-center"> <div class="flex justify-end items-center">
{{referrer.count|intcomma}} {{referrer.count|intcomma}}
<span class="text-xs rf" style="min-width: 48px"> <span class="text-xs rf min-w-48">
({{referrer.count|percent:stats.session_count}}) ({{referrer.count|percent:stats.session_count}})
</span> </span>
</div> </div>
@ -190,7 +234,7 @@
<td> <td>
<div class="flex justify-end items-center"> <div class="flex justify-end items-center">
{{os.count|intcomma}} {{os.count|intcomma}}
<span class="text-xs rf" style="min-width: 48px"> <span class="text-xs rf min-w-48">
({{os.count|percent:stats.session_count}}) ({{os.count|percent:stats.session_count}})
</span> </span>
</div> </div>
@ -225,7 +269,7 @@
<td> <td>
<div class="flex justify-end items-center"> <div class="flex justify-end items-center">
{{browser.count|intcomma}} {{browser.count|intcomma}}
<span class="text-xs rf" style="min-width: 48px"> <span class="text-xs rf min-w-48">
({{browser.count|percent:stats.session_count}}) ({{browser.count|percent:stats.session_count}})
</span> </span>
</div> </div>
@ -259,7 +303,7 @@
<td> <td>
<div class="flex justify-end items-center"> <div class="flex justify-end items-center">
{{device_type.count|intcomma}} {{device_type.count|intcomma}}
<span class="text-xs rf" style="min-width: 48px"> <span class="text-xs rf min-w-48">
({{device_type.count|percent:stats.session_count}}) ({{device_type.count|percent:stats.session_count}})
</span> </span>
</div> </div>