1 /*
  2  * Copyright (c) 2012-2014 LabKey Corporation
  3  *
  4  * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
  5  */
  6 
  7 /**
  8  * LABKEY.vis.Layer objects are used to define layers in a plot. The user should not worry about any methods on this
  9  * object, all methods are used internally by the {@link LABKEY.vis.Plot} object.
 10  *
 11  * @class LABKEY.vis.Layer class. Used to define layers used in {@link LABKEY.vis.Plot} objects.
 12  * @param config An object with the following properties.
 13  * @param {Object} [config.aes] (Optional) The aesthetic map object used to define aesthetic mappings such as x, y, color
 14  *      etc. If not present then it defaults to the aes object on the LABKEY.vis.Plot object. For an in depth description
 15  *      of the config.aes object please see the documentation for {@link LABKEY.vis.Plot}.
 16  * @param {Array} [config.data] The array of data to use when rendering this layer. If not present then it defaults to
 17  *      the data array on the LABKEY.vis.Plot object.
 18  * @param {Object} config.geom A geom object to use when rendering this layer. See {@link LABKEY.vis.Geom}.
 19  * @param {String} [config.name] (Optional) Name of the layer. You can give layers the same name or a different name. It
 20  *      Is primarily used if you have more than one layer that is using a color or shape scale and each layer has an
 21  *      overlap of values, but you want to differentiate each color or shape depending on the layer and aesthetic value.
 22  *      For example if you have a plot with two layers, each layer has a Path Geom on it, one layer could be used to
 23  *      plot weight over time, the other could be used to plot blood pressure over time for the same participant. If you
 24  *      want the lines to be different colors you could name each layer differently (i.e. Weight and Blood Pressure).
 25 
 26  */
 27 LABKEY.vis.Layer = function(config){
 28 	this.originalAes = config.aes ? config.aes : {};
 29     this.aes = LABKEY.vis.convertAes(this.originalAes);
 30 	this.data = config.data ? config.data : null; // This is the data used on the layer. If not specified it will used the data from the base plot object.
 31 	this.geom = config.geom ? config.geom : null; // This is the geom object used to render on the grid. It is currently required.
 32     this.name = config.name ? ' ' + config.name : '';
 33 
 34     for(var aesthetic in this.aes){
 35         LABKEY.vis.createGetter(this.aes[aesthetic]);
 36     }
 37     
 38     this.hasData = function(){
 39         return this.data && this.data.length > 0;
 40     };
 41 
 42 	this.render = function(renderer, grid, scales, data, parentAes, index){
 43 		// This function takes the data an renders it according the mappings, geoms, stats, and positions passed in.
 44 
 45 		if(this.geom){
 46 			this.geom.render(renderer, grid, scales, this.data ? this.data : data, this.aes, parentAes, this.name, index);
 47 		} else {
 48 			// Without a geom the render function will not do anything.
 49 			console.log("Unable to render this layer. No geom present");
 50 		}
 51 	};
 52 
 53     this.setAes = function(aes){
 54         LABKEY.vis.mergeAes(this.aes, aes);
 55         if (this.plot) {
 56             this.plot.render();
 57         }
 58     };
 59 
 60 	return this;
 61 };