Friday, November 20, 2009

Visualize your Twitter Timeline with jQuery and SIMILE

Since my last post I wondering if there was a better way to visualize my tweets. The Word Cloud was a nice feature, but I wanted more. I thought about using the Google Annotated Time Line chart, but it didn’t quite give me what I was looking for. Well, it turns out there is a really cool JavaScript plug-in called Simile Timeline that provides the functionality I was looking for.

So, I threw the following together to display my tweets. After completing it, I realized how much I actually tweet! A lot of exciting stuff was going on after being on vacation, having PDC 2009 going on, and watching the jQuery Summit.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Visualize your Twitter Timiline with jQuery and SIMILE</title>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>  
   <script src="http://simile.mit.edu/timeline/api/timeline-api.js" type="text/javascript"></script>      
   <style type="text/css" media="screen">
   body { background-color: #000; font: 14px Helvetica, Arial; color: #fff; }
   #my-timeline, #source { margin-top: 15px; }
   #criteria, #my-timeline, #source { margin-bottom: 15px; }

   .timeline-band-layer-inner { font-size: 10px; }
   .timeline-event-bubble-title, .timeline-event-bubble-body { color: black; }
   .timeline-event-bubble-time { padding-top: 5px; color: #ccccc; font-size: 9px; }
   </style>   
   <script type="text/javascript">   
   $(function() {
      $("#my-timeline").hide();
      $('#getTwitterTimeline').click(function() {
         getTwitterTimeline($('#userName').val());
      });
   });   
      
   var timeLine;
   function getTwitterTimeline(userName) {
      var eventSource = new Timeline.DefaultEventSource();
      var url = 'http://twitter.com/status/user_timeline/' + userName + '.json?count=200&callback=?';       
      $.getJSON(url, function(data) { 
         $("#my-timeline").fadeIn('slow');
         var mostRecentTweetDate = new Date(1);
         $.each(data, function(i, item) {   
            var dateEvent = Date.parse(item.created_at); 
            mostRecentTweetDate = (dateEvent > mostRecentTweetDate) ? dateEvent : mostRecentTweetDate;
            var html = replaceUrlWithHtmlLinks(item.text);
            var evt = new Timeline.DefaultEventSource.Event(
               new Date(dateEvent), //start
               null, //end
               null, //latestStart
               null, //earliestEnd
               true, //instant
               item.text.substr(0, 47) + '...', //text
               html //description
            );
            eventSource.add(evt);            
         });
         
         var bandInfos = [
            Timeline.createBandInfo({
               trackGap: 0.2,
               width: "80%", 
               intervalUnit: Timeline.DateTime.HOUR, 
               intervalPixels: 500,
               eventSource: eventSource,
               timeZone: new Date().getTimezoneOffset() / 60,
               date: new Date(mostRecentTweetDate).toGMTString()
            }),
            Timeline.createBandInfo({
               showEventText:  false,
               trackHeight: 0.5,
               trackGap: 0.2,
               width: "20%", 
               intervalUnit: Timeline.DateTime.DAY, 
               intervalPixels: 300,
               eventSource: eventSource,
               timeZone: new Date().getTimezoneOffset() / 60,
               date: new Date(mostRecentTweetDate).toGMTString()
            })
         ];
         
         bandInfos[1].syncWith = 0;
         bandInfos[1].highlight = true;
         timeLine = Timeline.create($("#my-timeline")[0], bandInfos);                  
      });
   }

   var resizeTimerID = null;   
   $('body').resize(function() {
      if (resizeTimerID == null) {
         resizeTimerID = window.setTimeout(function() {
            resizeTimerID = null;
            timeLine.layout();
         }, 500);
      }
   });
      
   function replaceUrlWithHtmlLinks(text) {    
      var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;    
      return text.replace(exp,"<a href='$1'>$1</a>"); 
   }   
   </script>
   </head>
   <body>
      <h3>Visualize your Twitter Timeline with jQuery and SIMILE</h3>
      <div id="criteria">
         <span>Twitter Username: </span>
         <input id="userName" type="text" value="elijahmanor" /> 
         <button id="getTwitterTimeline">Twitter Timeline</button>
      </div>
      <div id="my-timeline" style="height: 200px;"></div>
      <a id="source" href="http://jsbin.com/ekimi3/edit" target="_blank">View, Run, & Edit Source Code</a>      
   </body>
</html>

cooltext439924738 cooltext439924698

 

No comments:

Post a Comment