tmMapLegend

tmMapLegend is a PHP 4 class that interpolates colors to assist in generating a legend for the Google Charts API map chart type

Note—The interpolated colors on the map and the legend are not an exact match. I'm not sure if this is because of the map PNG's color profile or Google using a different algorithm.

Usage

  1. Download the latest version of tmMapLegend and upload to your webserver.
  2. Include the class in your script: include_once('tmMapLegend.php');
  3. Create a new tmMapLegend object with the simple data encoding: $legend = new tmMapLegend('simple");
  4. Retrieve a hexadecimal color value by using the getColor method of the tmMapLegend object: $legend->getColor('e', 'FFFFFF', 'FF0000')

For more about tmMapLegend, see the examples, class reference, and demo.

Download

Download tmMapLegend 1.0 (2008-04-02).

Examples

Create a tmMapLegend object

$legend = new tmMapLegend();

Note the encoding for this object is text.

Change the encoding to simple

$legend->setEncType('simple');

Retrieve an interpolated color value

$color = '#' . $legend->getColor('e', 'fff', 'ff0000');

Note e is the value 30 in Google' simple encoding. This is a two color ramp starting with white and ending with red.

Class Reference

obj tmMapLegend([ string $data_encoding ])

Constructor for the tmMapLegend class. Accepts one optional parameter to specify the data encoding. This parameter can take the value of either text or simple to specify Google's text encoding or simple encoding. By default, the class is set to use text encoding.

string getColor(string $value, string $gradient_start, string $gradient_end [, string $gradient_end])

getColor() is the primary method of tmMapLegend. It requires three parameters with an optional fourth. The first parameter, $value, can be either a string, float, or integer. It is the value that you want to generate the interpolated color for and can be in either Google's text encoding or simple encoding. The second parameter is the hexadecimal color that starts the color ramp. The third parameter is the top of the color ramp. Note the hex values can be either upper- or lowercase and can be three or six characters long (fff vs. fffff).

The optional fourth parameter specifies a third hexadecimal color for a three color ramp. In this case, the second parameter becomes the middle of the color ramp and the third parameter is the top. View the demo to see a three color ramp in action.

getColor() returns a string, which is the interpolated color in hexadecimal format.

array getRamp(array $values, string $gradient_start, string $gradient_end [, string $gradient_end])

getRamp() is very similar to getColor, but instead of taking a single string as the initial parameter, it takes an array of strings. getRamp() returns an array of interpolated color values in hexadecimal format.

setEncType(string $data_encoding)

setEncType() sets the data encoding after the tmMapLegend object has already been created. It takes one parameter with a value of either text or simple to specify Google's text encoding or simple encoding

Last update—2 April 2008

tmMapLegend | Demo »

Want to work with us yet? We’re ready when you are.