News for hackers. Stuff that matters.
Re:Watch Mitt Romney's Facebook Likes Decrease in Real Time by baby on Sunday November 11, 2012 @06:13PM (#4770923)
> Is this code available on github?

It's in the source code using the google Chart api [https://developers.google.com/chart/interactive/docs/index]

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(init);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawRealtime() {

        var data = google.visualization.arrayToDataTable(headings.concat(likes));

        var options = {
          title: 'Realtime Like Count For Mitt Romney',
          hAxis : {textColor: '#ffffff'},
          colors: ['red','#004411'],
          legend: {position: 'none'}
        };

        var chart = new google.visualization.LineChart(document.getElementById('realtime'));
        chart.draw(data, options);
      }

      function drawHistoric() {

        var data = google.visualization.arrayToDataTable(headings.concat([['1:59pm',12049076],['2:16pm',12048948],['2:30pm',12048820],['2:45pm',12048681],['3:00pm',12048559],['3:15pm',12048421],['3:30pm',12048297],['3:45pm',12048182],['4:00pm',12048077],['4:15pm',12047946],['4:30pm',12047807],['4:45pm',12047660],['5:00pm',12047509],['5:15pm',12047380],['5:30pm',12047247],['5:45pm',12047105],['6:00pm',12046942]]));

        var options = {
          title: 'Recent Like Count For Mitt Romney',
          legend: {position: 'none'}
        };

        var chart = new google.visualization.LineChart(document.getElementById('historic'));
        chart.draw(data, options);
      }

      var headings = [['Time','Likes']];
      var likes = [];

      console.log(headings.concat(likes));

      window.setInterval(fadeAway,1000);

      function init() {
      	drawHistoric();
      	window.setInterval(fetchLikes,1000);
      }

      var start = 0;

      function fetchLikes() {
      	$.getJSON('https://graph.facebook.com/mittromney?fields=likes',function(data){
			console.log(data.likes);
			if(likes.length==0) { data.likes+=10; start=data.likes; } else {
				$("#count").text("-"+(start-data.likes));
			}
			likes.push([new Date().getTime(),data.likes]);
			drawRealtime();
		});
      }

      function fadeAway() {
      	var opa = $("#romney").css('opacity');
      	$("#romney").css('opacity',(opa==0 ? 0 : opa-0.02));
      }

    </script>
It's all in the fetchLikes() that use the JSON api of the facebook graph. It's launched through this : window.setInterval(fetchLikes,1000);

After the graph API has been loaded.

There are no comments on this comment yet.