Difference between revisions of "Google Charts"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
This script allows you to take an existing list of values and represent it as a chart using [http://code.google.com/apis/chart/ 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.
This script allows you to take an existing list of values and represent it as a chart using [http://code.google.com/apis/chart/ 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.


[[http://chart.apis.google.com/chart?chs=200x125&chd=s:99WPHmWPPWHetWe1mtmmemmte&cht=lc&chxt=x,y&chxl=0:|4AM|4PM|4AM|1:||8 Example]]
[http://chart.apis.google.com/chart?chs=200x125&chd=s:99WPHmWPPWHetWe1mtmmemmte&cht=lc&chxt=x,y&chxl=0:|4AM|4PM|4AM|1:||8 Example]


<lsl>
<source lang="lsl2">
 
string simpleEncode(list values, float min, float max)
string simpleEncoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
string simpleEncode(list values)  
{
{
     string text = "";
     string text = "";
     float max = llListStatistics(LIST_STAT_MAX, values);
     float range = 244 / (min - max);//4 * 61
   
     integer index = -llGetListLength(values);
     integer count = llGetListLength(values);
 
   
     do
    integer index;
   
     for(index = 0; index < count; index++)
     {
     {
         integer value = llList2Integer(values, index);
         integer type = llGetListEntryType(values, index);
         if(value < 0)
         if(type == TYPE_FLOAT || type == TYPE_INTEGER)
             text += "_";
             text = llGetSubString(llIntegerToBase64((integer)(range * (min - llList2Float(values, index)))), 4, 4) + text;
         else
         else
        {
             text = "_" + text;
             float percent = value / max;
 
            value = llFloor(percent * 61);
        ++index;
            text += llGetSubString(simpleEncoding, value, value);
        }
     }
     }
    while (index);
     return text;
     return text;
}
}
string chartUrl(list values, list captions)
 
string cleanup(float value)
{
{
     integer i;
    string str = (string)value;
     integer n = llGetListLength(captions);
    if(value >= 10.0)
     string url = "http://chart.apis.google.com/chart";
        return llGetSubString(str, 0, -8);
    url += "?chs=200x125";
 
    url += "&chd=s:" + simpleEncode(values);
     integer i = -1;
    url += "&cht=lc";
     while(llGetSubString(str, i, i) == "0")
    url += "&chxt=x,y";
        --i;
    url += "&chxl=0:";
 
     for(i = 0; i < n; i++)
    if(llGetSubString(str, i, i) == ".")
         url += "|" + llEscapeURL(llList2String(captions, i));
        --i;
     url += "|1:||" + (string)llCeil(llListStatistics(LIST_STAT_MAX, values));
 
    return llGetSubString(str, 0, i);
}
 
string chartUrl(list values, list captions, integer yDivisions)
{
    integer min = llFloor(llListStatistics(LIST_STAT_MIN, values));
    integer max = llCeil(llListStatistics(LIST_STAT_MAX, values));
 
     string url = "http://chart.apis.google.com/chart"
              + "?chs=200x125"
              + "&chd=s:" + simpleEncode(values, min, max)
              + "&cht=lc"
              + "&chxt=x,y"
              + "&chxl=0:";
 
     integer index = -llGetListLength(captions);
    do
    {
         url += "|" + llEscapeURL(llList2String(captions, index));
        ++index;
    }
    while (index);
 
     url += "|1:";
    ++yDivisions;
    float range = max - min;
 
    index = 1;
    do
    {
        url += "|" + cleanup(((index * range) / yDivisions) + min);
        ++index;
    }
    while (index <= yDivisions);
 
     return url;
     return url;
}
}
Line 51: Line 82:
     state_entry()
     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"]));
        // PUBLIC_CHANNEL has the integer value 0
         llSay(PUBLIC_CHANNEL,
            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"], 0));
     }
     }


     touch_start(integer total_number)
     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"]));
         llSay(PUBLIC_CHANNEL,
            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"], 0));
     }
     }
}
}
 
</source>
</lsl>


{{LSLC|Library|Google Charts}}  
{{LSLC|Library|Google Charts}}  
{{LSLC|Examples|Google Charts}}
{{LSLC|Examples|Google Charts}}

Latest revision as of 15:10, 24 January 2015

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

string simpleEncode(list values, float min, float max)
{
    string text = "";
    float range = 244 / (min - max);//4 * 61
    integer index = -llGetListLength(values);

    do
    {
        integer type = llGetListEntryType(values, index);
        if(type == TYPE_FLOAT || type == TYPE_INTEGER)
            text = llGetSubString(llIntegerToBase64((integer)(range * (min - llList2Float(values, index)))), 4, 4) + text;
        else
            text = "_" + text;

        ++index;
    }
    while (index);

    return text;
}

string cleanup(float value)
{
    string str = (string)value;
    if(value >= 10.0)
        return llGetSubString(str, 0, -8);

    integer i = -1;
    while(llGetSubString(str, i, i) == "0")
        --i;

    if(llGetSubString(str, i, i) == ".")
        --i;

    return llGetSubString(str, 0, i);
}

string chartUrl(list values, list captions, integer yDivisions)
{
    integer min = llFloor(llListStatistics(LIST_STAT_MIN, values));
    integer max = llCeil(llListStatistics(LIST_STAT_MAX, values));

    string url = "http://chart.apis.google.com/chart"
               + "?chs=200x125"
               + "&chd=s:" + simpleEncode(values, min, max)
               + "&cht=lc"
               + "&chxt=x,y"
               + "&chxl=0:";

    integer index = -llGetListLength(captions);
    do
    {
        url += "|" + llEscapeURL(llList2String(captions, index));
        ++index;
    }
    while (index);

    url += "|1:";
    ++yDivisions;
    float range = max - min;

    index = 1;
    do
    {
        url += "|" + cleanup(((index * range) / yDivisions) + min);
        ++index;
    }
    while (index <= yDivisions);

    return url;
}

default
{
    state_entry()
    {
        // PUBLIC_CHANNEL has the integer value 0
        llSay(PUBLIC_CHANNEL,
            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"], 0));
    }

    touch_start(integer total_number)
    {
        llSay(PUBLIC_CHANNEL,
            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"], 0));
    }
}