 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-14-2007, 04:08 PM
|
#1 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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.
|
|
|
|
12-14-2007, 04:31 PM
|
#2 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
12-14-2007, 04:54 PM
|
#3 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
|
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, 115, 116, 125, 126, 127, 4032, 4057, 4058, 4083, 4084, 4093, 4094, 4095); // Run the test cases$aResults = array_map('convert', $aTests); // Output the result for each of the testsheader('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
|
|
|
|
|
The Following 2 Users Say Thank You to Salathe For This Useful Post:
|
|
12-14-2007, 05:39 PM
|
#4 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
perfect, cheers m8 
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|