There is no explicit support for anything like this (AKAIK), but you can 
hack a solution.  If you have a chart with both positive and negative 
values, get the range from the highest to lowest values and calculate the 
ratio of each to the range, then assign colors in the same ratios for the 
positive and negative values, adding a specific color for 0 in the middle. 
 Something like this:

/* this function takes a max and min and returns an array of colors 
 * from red (FF0000) to green (0000FF) that color the chart red for
 * negative numbers and green for positive numbers, zero is white
 * 
 * parameters:
 *   max = the maximum (positive) value
 *   min = the minimum (negative) value
 *   n = the number of color gradations (larger values of n give more 
accurate map gradation results)
 * 
 * returns the array of colors
 */
function getColors (max, min, n) {
    var a = Math.round((n-1) * Math.abs(min) / (Math.abs(min) + max));
    var b = Math.round((n-1) * max / (Math.abs(min) + max));
    var colors = [];
    var hex;
    for (i = 0; i < a; i++) {
        hex = Math.round(i * 256 / a).toString(16).toUpperCase();
        hex = (parseInt(hex, 16) < 16) ? "0" + hex : hex;
        colors.push("#FF" + hex + hex);
    }
    for (i = b; i >= 0; i--) {
        hex = Math.round(i * 255 / b).toString(16).toUpperCase();
        hex = (parseInt(hex, 16) < 16) ? "0" + hex : hex;
        colors.push("#" + hex + hex + "FF");
    }
    return colors;
}


You can change it up to adjust for returning hex values rather than strings 
or to auto-detect negative max/positive min and calculate appropriately

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-visualization-api/-/1CGzSuHWWskJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.

Reply via email to