Source: util/ColorManager.js

/**
 * @namespace Oracle.BDD.Portlets.Visualization.Util
 * 
 */
Ext.ns('Oracle.BDD.Portlets.Visualization.Util');

/**
 * 
 * Typically, only a single ColorManager is required by the visualization and that
 * is best acquired via {@link Oracle.BDD.Portlets.Visualization.Renderers.BaseRenderer#getColorManager}.
 * However, multiple instances of a ColorManager may be created.
 * 
 * <pre>
 * var dayOfWeekColorMgr = new Oracle.BDD.Portlets.Visualization.Util.ColorManager({
 *    library: ["#6B486B", "#718BAE", "#ABBE23"],
 *    scaleDomain: ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
 * });
 * </pre>
 * @class Oracle.BDD.Portlets.Visualization.Util.ColorManager
 * 
 */
Oracle.BDD.Portlets.Visualization.Util.ColorManager = function(config) {

	config = config || {};
	
	if (config.library) this.setLibrary(config.library);
	this.setScaleDomain(config.scaleDomain || d3.range(this.library.length))
  
	return this;
};

Oracle.BDD.Portlets.Visualization.Util.ColorManager.prototype = {
	
	library: ["#6B486B", "#718BAE", "#ABBE23", "#CAAA2B", "#D0743C", "#B93E3E", "#8242BB", 
                   "#325ECF", "#77C034", "#E2D23F", "#EE6911", "#DA3F3F"],
    
	scaleDomain: undefined,   
    colorFn: undefined,
    
    /** 
     * Returns the hex value from the color library corresponding to the domain key.  
     * See {@link https://github.com/mbostock/d3/wiki/Ordinal-Scales|D3 Ordinal Scales}
     * for more information on color scales and scale domains.
     * @param {string|Numeric} domainKey The domain key
     * @return {string} Hex value of color
     */
    getColor: function(domainKey) {   	
        return this.colorFn(domainKey);
    },
    
    /** 
     * Returns the scale's input domain values.  
     * See {@link https://github.com/mbostock/d3/wiki/Ordinal-Scales|D3 Ordinal Scales}
     * for more information on color scales and scale domains.
     * @return {string[]} The array of domain values
     */
    getScaleDomain: function() {
    	return this.scaleDomain;
    },
    
    /** 
     * Sets the color library for the color scale of the visualization.  Value passed may be an array
     * of hex strings or a string value of '10', '20' or '20b' to set the color library to that of 
     * the D3 category10, category20 or category20b  libraries respectively.  
     * <p>
     * See {@link https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors|Categorical 
     * Colors} for information on D3 categorical color libraries.
     * <p>
     * See 
     * {@link https://github.com/mbostock/d3/wiki/Ordinal-Scales|D3 Ordinal Scales}
     * for more information on color scales and scale domains.
     * @param {Object[]|string} colors An array of hex values to serve as the color library for the color scale.  The
     * string values of '10', '20' or '20b' may be passed to set the color library to those of the D3 category10, category20
     * or category20b  libraries 
     */		
    setLibrary: function(colors) {
	    
        if (colors === '10' || colors == '20' || colors == '20b') {
            this.library = [];
            switch (colors) {
                case '10':
                    this.colorFn = d3.scale.category10();
                    break;
                case '20':
                    this.colorFn = d3.scale.category20();
                    break;
                case '20':
                    this.colorFn = d3.scale.category20();
                    break;
            }
        } else {
            if (!Ext.isArray(colors))  colors = [colors];
            this.library = colors;
            this.colorFn = d3.scale.ordinal().domain(this.scaleDomain).range(this.library);
        }
	    
    },
    
	/** 
	* Sets the color scale's input domain values.  This is the means to ensure that any instance
	* of the ColorManager configured with the same library will return the same color for the given
	* key value (getColor(domainKey)).  Setting a scale domain is optional; one instance of the ColorManager
	* will always return the same value for the same input key.  Setting a scale domain ensures consistency
	* across multiple instances of the ColorManager (i.e. from page request to page request).
	* <p>
	* See {@link https://github.com/mbostock/d3/wiki/Ordinal-Scales|D3 Ordinal Scales}
	* for more information on color scales and scale domains.
	* @param {Object[]} domain An array of values to serve as the color scale's domain input values
	*/
	setScaleDomain: function(domain) {
			    	
	    if (!Ext.isEmpty(domain)) {
	    	
	    	if (Ext.isNumber(domain)) {
	    		domain = d3.range(domain);
	    	}
	        	        
	        if (Ext.isArray(domain)) {
	        	this.scaleDomain = domain;
	        	this.colorFn = d3.scale.ordinal().domain(this.scaleDomain).range(this.library);
	        }
	    }	   	
	}
}
//@ sourceURL=ColorManager.js