From ba91f8e891d263b0927434bbec4c331be0b40e4e Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 2 Oct 2015 19:16:37 +0000 Subject: [PATCH] show a graph of battery usage at the bottom of the map --- compass/app/Http/Controllers/Api.php | 5 +- compass/public/assets/map.css | 22 +- compass/public/assets/map.js | 189 ++++++++++++++++++ compass/resources/views/layouts/map.blade.php | 1 + compass/resources/views/map.blade.php | 8 +- 5 files changed, 213 insertions(+), 12 deletions(-) diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index d25fa19..b2b488a 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -50,17 +50,20 @@ class Api extends BaseController $results = $qz->queryRange($start, $end); $locations = []; + $properties = []; foreach($results as $id=>$record) { $record->date->format('U.u'); $locations[] = $record->data; + $properties[] = $record->data->properties; } if($request->input('format') == 'linestring') { $response = array( 'type' => 'LineString', - 'coordinates' => array() + 'coordinates' => array(), + 'properties' => $properties ); foreach($locations as $loc) { $response['coordinates'][] = $loc->geometry->coordinates; diff --git a/compass/public/assets/map.css b/compass/public/assets/map.css index 837a1b1..7e82244 100644 --- a/compass/public/assets/map.css +++ b/compass/public/assets/map.css @@ -1,16 +1,15 @@ -html, body, #map { +html, body { height: 100%; } -.leaflet-top .leaflet-control-zoom { - margin-top: 60px; - margin-left: 26px; +#map { + height: calc(100% - 120px); } #calendar { z-index: 100; width: 200px; - height: calc(100% - 40px); + height: calc(100% - 160px); position: absolute; top: 10px; right: 10px; @@ -27,7 +26,7 @@ html, body, #map { } #calendar .scroll { overflow: scroll; - height: calc(100%); + height: 100%; } table.calendar { @@ -50,3 +49,14 @@ table.calendar { .calendar td a.selected { background: #6699ff; } + +#battery-chart { + width: 100%; +} + + +/* Move the map zoom controls away from the Compass logo */ +.leaflet-top .leaflet-control-zoom { + margin-top: 60px; + margin-left: 26px; +} diff --git a/compass/public/assets/map.js b/compass/public/assets/map.js index 754ed19..8e0a7b6 100644 --- a/compass/public/assets/map.js +++ b/compass/public/assets/map.js @@ -1,3 +1,76 @@ +Array.prototype.clean = function(deleteValue) { + for (var i = 0; i < this.length; i++) { + if (this[i] == deleteValue) { + this.splice(i, 1); + i--; + } + } + return this; +}; + +/* + Array.findRanges + + Turns this: + + ["a","a","a","b","b","c","c","c","c","c","a","a","c"] + + into this: + + { + "a":[ + { + "from":0, + "to":2 + }, + { + "from":10, + "to":11 + } + ], + "b":[ + { + "from":3, + "to":4 + } + ], + "c":[ + { + "from":5, + "to":9 + }, + { + "from":12, + "to":12 + } + ] + } + +*/ + +Array.prototype.findRanges = function() { + var buckets = {}; + for(var i = 0; i < this.length; i++) { + if(!(this[i] in buckets)) { + buckets[this[i]] = [{ + from: i, + to: i + }] + } else { + var last = buckets[this[i]][ buckets[this[i]].length-1 ]; + if(i == last.to + 1) { + last.to = i; + } else { + buckets[this[i]].push({ + from: i, + to: i + }) + } + } + } + return buckets; +}; + var map = L.map('map', { zoomControl: false }).setView([45.516, -122.660], 14, null, null, 24); var layer = L.esri.basemapLayer("Topographic"); @@ -15,9 +88,18 @@ var geojsonLineOptions = { var visible_layers = []; var visible_data = []; +var highlightedMarker; var animatedMarker; var timers = []; +var batteryChart; + +/* +Chart.defaults.global.animation = false; +Chart.defaults.global.responsive = true; +*/ + + function resetAnimation() { if(animatedMarker) { map.removeLayer(animatedMarker); @@ -77,6 +159,107 @@ jQuery(function($){ } map.fitBounds(full_bounds); } + + var batteryStateBands = []; + var buckets = data.properties.map(function(d){return d.battery_state}).findRanges(); + + for(var i in buckets) { + for(var j=0; j{point.y}', + valueSuffix: '%' + }, + turboThreshold: 0 + }] + }); + $('#battery-chart').mousemove(function(event){ + var chart = $('#battery-chart').highcharts(); + var percent = (event.offsetX - chart.plotLeft) / chart.plotWidth; + if(percent >= 0 && percent <= 1) { + var coord = pointFromGeoJSON(visible_data[0].coordinates[Math.round(percent * visible_data[0].coordinates.length)]); + if(!highlightedMarker) { + highlightedMarker = L.marker(coord).addTo(map); + } else { + highlightedMarker.setLatLng(coord); + } + } + }); + } }); @@ -110,4 +293,10 @@ jQuery(function($){ }); + $(".calendar a[data-date='"+((new Date()).toISOString().slice(0,10))+"']").focus().click(); + + //////////////////// + + //batteryChart = new Chart(document.getElementById("battery-chart").getContext("2d")); + }); diff --git a/compass/resources/views/layouts/map.blade.php b/compass/resources/views/layouts/map.blade.php index 681dd7f..27970cc 100644 --- a/compass/resources/views/layouts/map.blade.php +++ b/compass/resources/views/layouts/map.blade.php @@ -17,6 +17,7 @@ + \ No newline at end of file diff --git a/compass/resources/views/map.blade.php b/compass/resources/views/map.blade.php index 06f01b0..e602df3 100644 --- a/compass/resources/views/map.blade.php +++ b/compass/resources/views/map.blade.php @@ -31,12 +31,10 @@
+
+
+
- @endsection