Google Charts

From Second Life Wiki
Revision as of 17:43, 31 December 2007 by Dedric Mauriac (talk | contribs)
Jump to navigation Jump to search

This script allows you to take an existing list of values and represent it as a chart using Google's Chart API. The example here takes a list of visitors per hour and represents it as a bar chart. The simpleEncoding method provided by Google has been converted to LSL script.

Example

<lsl>

string simpleEncoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; string simpleEncode(list values) {

   string text = "";
   float max = llListStatistics(LIST_STAT_MAX, values);
   
   integer count = llGetListLength(values);
   
   integer index;
   
   for(index = 0; index < count; index++)
   {
       integer value = llList2Integer(values, index);
       if(value < 0)
           text += "_";
       else
       {
           float percent = value / max;
           value = llFloor(percent * 61);
           text += llGetSubString(simpleEncoding, value, value);
       }
   }
   return text;

} string chartUrl(list values, list captions) {

   integer i;
   integer n = llGetListLength(captions);
   string url = "http://chart.apis.google.com/chart";
   url += "?chs=200x125";
   url += "&chd=s:" + simpleEncode(values);
   url += "&cht=lc";
   url += "&chxt=x,y";
   url += "&chxl=0:";
   for(i = 0; i < n; i++)
       url += "|" + llEscapeURL(llList2String(captions, i));
   url += "|1:||" + (string)llCeil(llListStatistics(LIST_STAT_MAX, values));
   return url;

}

default {

   state_entry()
   {
       llSay(0, chartUrl([8,8,3,2,1,5,3,2,2,3,1,4,6,3,4,7,5,6,5,5,4,5,5,6,4], ["4AM", "4PM", "4AM"]));
   }
   touch_start(integer total_number)
   {
       llSay(0, chartUrl([8,8,3,2,1,5,3,2,2,3,1,4,6,3,4,7,5,6,5,5,4,5,5,6,4], ["4AM", "4PM", "4AM"]));
   }

}

</lsl>