Advertisement
vgillestad

Untitled

Feb 26th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //On the comsuption site (/Meter/Consumption) you should be able to paste in this
  2. //script in the chrome-console and the four largest values should be flagged.
  3. (function () {
  4.     //Helper function for finding largest values in array        
  5.     var findLargestValues = function (arr, howManyToFind) {
  6.         if (!arr || arr.length === 0) {
  7.             return [];
  8.         }
  9.         howManyToFind = arr.length < howManyToFind ? arr.length : howManyToFind;
  10.         var clone = arr.slice(); //clone to not temper with the original
  11.         var largest = []; //{index,value}
  12.         for (var i = 0; i < howManyToFind; i++) {
  13.             var max = Math.max.apply(null, clone); // get the max value
  14.             if (max) {
  15.                 var maxi = clone.indexOf(max); //get the max index
  16.                 clone[maxi] = -Infinity; // replace max in the array with -infinity (for performance)
  17.                 largest.push({
  18.                     index: maxi,
  19.                     value: max
  20.                 });
  21.             }
  22.  
  23.         }
  24.         return largest;
  25.     }
  26.  
  27.     var $chart = $('#chart-container');
  28.     var chart = $chart.highcharts();
  29.     var hourValues = eval($chart.attr('data-serie-data'));
  30.     var firstDate = eval($chart.attr('data-serie-pointstart'));
  31.  
  32.     var largestFour = findLargestValues(hourValues, 4);
  33.     var flagData = largestFour.sort(function (a, b) {
  34.         return a.index - b.index;
  35.     }).map(function (element) {
  36.         return {
  37.             x: firstDate + element.index * 60 * 60 * 1000,
  38.             title: element.value
  39.         }
  40.     });
  41.  
  42.     var data = {
  43.         type: 'flags',
  44.         name: 'Flags on series',
  45.         data: flagData,
  46.         onSeries: 'thisyear', //Draw on consumption graph
  47.         shape: 'flag',
  48.     }
  49.  
  50.     chart.addSeries(data);
  51. } ());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement