Guest User

The Button stat tracker 9001

a guest
Apr 1st, 2015
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Reddit: The button: The stat chart
  3. // @version      0.1
  4. // @description  button stat tracker 9001
  5. // @author       /u/tcghyit
  6. // @match        http://www.reddit.com/r/thebutton*
  7. // @grant        none
  8. // @require      http://canvasjs.com/assets/script/canvasjs.min.js
  9. // ==/UserScript==
  10.  
  11. console.log('Button chart')
  12.  
  13. var dps = []; // dataPoints
  14.  
  15. var el = document.createElement("div");
  16. el.id = "chartContainer";
  17. el.style.width="100%";
  18. el.style.height= "350px";
  19. el.style.clear= "both";
  20.  
  21. var theButton = document.getElementsByClassName('thebutton-form')[0];
  22. theButton.appendChild(el);
  23.  
  24. var chart = new CanvasJS.Chart("chartContainer",{
  25.     title :{
  26.         text: "Button Presses"
  27.     },
  28.     legend:{
  29.         verticalAlign: "top",
  30.         horizontalAlign: "centre",
  31.         fontSize: 18,
  32.     },
  33.     axisY: {
  34.         title: "# of clicks"
  35.     },
  36.     data: [{
  37.         type: "line",
  38.         xValueType: "dateTime",
  39.         dataPoints: dps,
  40.         showInLegend: true,
  41.         legendMarkerType: "square",
  42.         legendMarkerBorderThickness: 5,
  43.         legendMarkerBorderColor: "white",
  44.         legendMarkerColor: "white",
  45.         legendText: "Lowest recorded time left: 60"
  46.     }]
  47. });
  48.  
  49. var updateInterval = 1000;
  50. var dataLength = 300; // number of dataPoints visible at any point
  51. var participants;
  52. var partEl = document.getElementsByClassName('thebutton-participants')[0];
  53. var oldParticipants = 0;
  54. var lowestMS = 60000;
  55.  
  56. var updateChart = function (count) {
  57.  
  58.     participants = parseInt(partEl.innerHTML.replace(/,/, ''));
  59.  
  60.     dps.push({
  61.         x: new Date().getTime(),
  62.         y: (participants-(oldParticipants||participants)),
  63.         toolTipContent: "<span style='\"'color: blue;'\"'>{x}</span>: {y} people clicked"
  64.     });
  65.     oldParticipants = participants;
  66.  
  67.     //delete excess data
  68.     if (dps.length > dataLength)
  69.     {
  70.         dps.shift();               
  71.     }
  72.  
  73.     var msLeft = r.thebutton._msLeft;
  74.     if(lowestMS>msLeft && msLeft!=0) {
  75.         lowestMS=msLeft;
  76.         chart.options.data[0].legendText = "Lowest recorded time left: "+(lowestMS/1000);
  77.     }
  78.  
  79.     chart.render();    
  80.  
  81. };
  82.  
  83. // update chart after specified time.
  84. $(document).ready(function(){
  85.     setInterval(function(){updateChart()}, updateInterval);
  86. });
Add Comment
Please, Sign In to add comment