TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-14-2007, 05:08 PM   #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default Google chart extended data encoding help

Hey, I need some help with an algorithm for the GoogleChart "wrappers" that I am working with. I'm in the process of writing the extended data encoding algorithm, I've come up with something that works, but I now there is a better solution to it using math. Here is the code that I came up with, it all works and should prove to be a good reference.

php Code:
public static function convert($nValue)
    {
        // This code will be slow, and should be replaced with a mathematical algorithm
       
        assert($nValue >= 0);
                           
        $aMask = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                        'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                        'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
                        '8', '9', '-', '.');
       
        $iLength = count($aMask);                     
        $iCount = 0;       
           
        for ($i = 0; $i < $iLength; $i++)
        {         
            for ($j = 0; $j < $iLength; $j++)
            {
                if ($nValue == $iCount)
                {
                    return $aMask[$i] . $aMask[$j];
                }

                $iCount++;
            }
        }
                           
        return '__';
    }

Any help with this would be greatly appreciated.

Here's the link to Google's documentation on the extended encoding:
http://code.google.com/apis/chart/#extended

One last note, I'm not sure if I was on the right track, but I was thinking that the math algorithm would need to use a base64 style numbering system, replacing each of the 64 possible values with the values provided by Google and then somehow do the math (it just all got too much for me).
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-14-2007, 05:31 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

at the moment my brain is returning a fatal error, i think i need to reboot.

i can see what you mean, but its just how to do it, if i get my brain working ill have a go.

its a tough one xD
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-14-2007, 05:54 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I think that I've come up with a function which will do this conversion properly. I've done a bunch of tests against answers which I know are correct (from the Google Charts docs) and everything appears ok. If there is something broken, at least you'll have a starting point to push forwards from.

So, below is the function and the tests used to see if it works or not.

php Code:
<?php


// This function converts an integer (0 < i <= 4095)
// into the 'extended' format for Google Charts data.
function convert($iValue)
{
    static $szBase, $iBaseLen;
   
    // Initialise static variables only once
    isset($szBase)   or $szBase   = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
    isset($iBaseLen) or $iBaseLen = strlen($szBase);
   
    // If an invalid value is provided, return 'missing value'
    if (!is_int($iValue) or $iValue < 0 or $iValue > 4095)
    {
        return '__';
    }
   
    // Process the integer value into the new 'base' and return
    return $szBase[(int) ($iValue / $iBaseLen)] . $szBase[$iValue % $iBaseLen];
}


// Set up some test cases to which we know the expected results
$aExpected = array('AA', 'AZ', 'Aa', 'Az', 'A0', 'A9', 'A-', 'A.',
                   'BA', 'BZ', 'Ba', 'Bz', 'B0', 'B9', 'B-', 'B.',
                   '.A', '.Z', '.a', '.z', '.0', '.9', '.-', '..');
                   
$aTests    = array(0,    25,   26,   51,   52,   61,   62,   63,
                   64,   89,   90,   115116125126127,
                   4032, 4057, 4058, 4083, 4084, 4093, 4094, 4095);

// Run the test cases
$aResults = array_map('convert', $aTests);

// Output the result for each of the tests
header('Content-Type: text/plain');
foreach ($aExpected as $iKey => $szExpected)
{
    $szResult = ($aResults[$iKey] === $szExpected ? 'passed' : '***** FAILED, should be: ' . $szExpected);
    printf("%4d -> %s %s\n", $aTests[$iKey], $aResults[$iKey], $szResult);
}

For me, that test page returns the following output:
Code:
   0 -> AA passed
  25 -> AZ passed
  26 -> Aa passed
  51 -> Az passed
  52 -> A0 passed
  61 -> A9 passed
  62 -> A- passed
  63 -> A. passed
  64 -> BA passed
  89 -> BZ passed
  90 -> Ba passed
 115 -> Bz passed
 116 -> B0 passed
 125 -> B9 passed
 126 -> B- passed
 127 -> B. passed
4032 -> .A passed
4057 -> .Z passed
4058 -> .a passed
4083 -> .z passed
4084 -> .0 passed
4093 -> .9 passed
4094 -> .- passed
4095 -> .. passed
Salathe is offline  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
Karl (12-14-2007), sketchMedia (12-15-2007)
Old 12-14-2007, 06:39 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

perfect, cheers m8
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design