2 messages in com.googlegroups.google-chart-apiRe: Simple encoding functions
FromSent OnAttachments
Felipe Barone24 Jan 2008 08:27 
Felipe Barone24 Jan 2008 08:28 
Subject:Re: Simple encoding functions
From:Felipe Barone (fgba@gmail.com)
Date:01/24/2008 08:28:42 AM
List:com.googlegroups.google-chart-api

And the same function above, written in PHP:

<?php function simpleEncode($values, $max = 61, $min = 0){ $simple_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $chardata = 's:'; $delta = $max - $min; $size = (strlen($simple_table)-1);

foreach($values as $k => $v){ if($v >= $min && $v <= $max){ $chardata .= $simple_table[round($size * ($v - $min) / $delta)]; }else{ $chardata .= '_'; } } return($chardata); } // Usage: $test = array(-100, 50, -50, 0, 80, 100); $max = 100; $min = -100; echo simpleEncode($test, $max, $min); ?>

I was porting the javascript
functionhttp://code.google.com/apis/chart/#encoding_data to PHP (i know it was done already, but just for kicks) when i realized this function can NOT handle negative values....

For example if i want to plot the values: 10,-20,40,72,-30 the -20 and the -30 wont show.... so, heres and extended version of this function where you can setup minimum and maximum value: <code> var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function simpleEncode(valueArray,maxValue,minValue) { var chartData = ['s:']; var size = simpleEncoding.length-1; var delta = maxValue - minValue; for (var i = 0; i < valueArray.length; i++) { var currentValue = valueArray[i]; if (!isNaN(currentValue) && currentValue >= minValue) { chartData.push(simpleEncoding.charAt(Math.round(size * (currentValue - minValue)/ delta))); } else { chartData.push('_'); } } return chartData.join('');}

var valueArray = new Array(-100, 50, -50, 0, 80, 100); var maxValue = 100; var minValue = -100; simpleEncode(valueArray,maxValue, minValue);