File: ia\data\Associate.js
/**
* Contains information about an associate.
*
* @author J Clare
* @class ia.Associate
* @extends ia.BaseData
* @constructor
* @param {ia.Geography} geography The geography the associate belongs to.
* @param {ia.Indicator} indicator The indicator the associate belongs to.
* @param {JSON} data The json data describing the object.
*/
ia.Associate = function(geography, indicator, data)
{
ia.Associate.baseConstructor.call(this, data, indicator);
this.indicator = indicator;
this.geography = geography;
this.parseData();
};
ia.extend(ia.BaseData, ia.Associate);
/**
* The parent indicator.
*
* @property indicator
* @type ia.Indicator
*/
ia.Associate.prototype.indicator;
/**
* The minimum value.
*
* @property minValue
* @type Number
*/
ia.Associate.prototype.minValue;
/**
* The maximum value.
*
* @property maxValue
* @type Number
*/
ia.Associate.prototype.maxValue;
/**
* Parses the JSON data.
*
* @method parseData
* @param {JSON} data The json data describing the object.
*/
ia.Associate.prototype.parseData = function(data)
{
// Parse the JSON data.
this.id = this.data.name; // Override id setting for associate because it uses "name" rather than "id".
// Values
this._indexHash = {};
this._valueArray = this.data.values;
this._fValueArray = [];
if (this._valueArray)
{
// Features
var features = this.geography.getFeatures();
var fLength = features.length;
this.minValue = Infinity;
this.maxValue = -Infinity;
var v;
var fv;
var featureId;
for (var i = 0; i < fLength; i++)
{
featureId = features[i].id;
v = this._valueArray[i];
fv = this.geography.reportData.formatter.format(v, this.precision);
this._indexHash[featureId] = i;
this.minValue = (v < this.minValue) ? v : this.minValue;
this.maxValue = (v > this.maxValue) ? v : this.maxValue;
this._fValueArray.push(fv);
}
}
// Comparison Values
this._comparisonIndexHash = {};
this._comparisonArray = this.data.comparisonValues;
this._fComparisonArray = [];
if (this._comparisonArray)
{
// Comparison Features
var comparisonFeatures = this.geography.getComparisonFeatures();
var cLength = comparisonFeatures.length;
for (var i = 0; i < cLength; i++)
{
featureId = comparisonFeatures[i].id;
v = this._comparisonArray[i];
this._comparisonIndexHash[featureId] = i;
fv = this.geography.reportData.formatter.format(v, this.precision);
this._fComparisonArray.push(fv);
}
}
};
/**
* Returns the value that corresponds to the feature id.
* Will return a comparison value if the id starts with "#".
*
* @method getValue
* @param {String} id The feature id.
* @return {Number|String} The value for the given id.
*/
ia.Associate.prototype.getValue = function(id)
{
if (this._indexHash[id] !== undefined) return this._valueArray[this._indexHash[id]]
else return this.getComparisonValue(id);
};
/**
* Returns the list of values.
*
* @method getValues
* @return {Number|String[]} An array of values.
*/
ia.Associate.prototype.getValues = function() {return this._valueArray;};
/**
* Returns the formatted value that corresponds to the feature id.
* Will return a comparison value if the id starts with "#".
*
* @method getFormattedValue
* @param {String} id The feature id.
* @return {String} The formatted value for the given id.
*/
ia.Associate.prototype.getFormattedValue = function(id)
{
if (this._indexHash[id] !== undefined) return this._fValueArray[this._indexHash[id]]
else return this.getFormattedComparisonValue(id);
};
/**
* Returns the list of formatted values.
*
* @method getFormattedValues
* @return {String[]} An array of formatted values.
*/
ia.Associate.prototype.getFormattedValues = function() {return this._fValueArray;};
/**
* Returns the comparison value that corresponds to the feature id.
*
* @method getComparisonValue
* @param {String} id The feature id.
* @return {Number|String} The value for the given id.
*/
ia.Associate.prototype.getComparisonValue = function(id)
{
if (this._comparisonIndexHash[id] !== undefined)
return this._comparisonArray[this._comparisonIndexHash[id]];
return undefined;
};
/**
* Returns the list of comparison values.
*
* @method getComparisonValues
* @return {Number|String[]} An array of values.
*/
ia.Associate.prototype.getComparisonValues = function() {return this._comparisonArray;};
/**
* Returns the formatted comparison value that corresponds to the feature id.
*
* @method getFormattedComparisonValue
* @param {String} id The feature id.
* @return {String} The formatted value for the given id.
*/
ia.Associate.prototype.getFormattedComparisonValue = function(id)
{
if (this._comparisonIndexHash[id] !== undefined)
return this._fComparisonArray[this._comparisonIndexHash[id]];
return undefined;
};
/**
* Returns the list of formatted comparison values.
*
* @method getFormattedComparisonValues
* @return {String[]} An array of formatted values.
*/
ia.Associate.prototype.getFormattedComparisonValues = function() {return this._fComparisonArray;};
/**
* Returns a hashtable of the associate data.
*
* <p>The returned data has the following structure:</p>
*
* <p>["eh11"]{id:"eh11", name:"polwarth", value:2345, value_formatted:2345, associate1:25, associate1_formatted:25}
* <br/>["eh12"]{id:"eh12", name:"morningside", value:4347, value_formatted:4347, associate1:45, associate1_formatted:45}
* <br/>["eh13"]{id:"eh13", name:"merchiston", value:2496, value_formatted:2496, associate1:25, associate1_formatted:25}</p>
*
* @method getData
* @param {String[]} featureIds An optional list of feature ids to get data for.
* @return {Associative Array} As described above.
*/
ia.Associate.prototype.getData = function(featureIds)
{
var dataHash = {};
var fLength;
var featureArray;
if (featureIds != null)
fLength = featureIds.length;
else
{
featureArray = this.geography.getFeatures();
fLength = featureArray.length;
}
// Each loop is a new row in the data.
for (var i = 0; i < fLength; i++)
{
// Feature.
var feature;
if (featureIds != null)
feature = this.geography.getFeature(featureIds[i]);
else
feature = featureArray[i];
// Indicator.
var obj = {};
obj.id = feature.id;
obj.name = feature.name;
obj.value = this.getValue(feature.id);
obj.value_formatted = this.getFormattedValue(feature.id);
dataHash[feature.id] = obj;
}
return dataHash;
};
/**
* Returns a hashtable of the associate comparison data.
*
* <p>The returned data has the following structure:</p>
*
* <p>["eh11"]{id:"eh11", name:"polwarth", value:2345, value_formatted:2345, associate1:25, associate1_formatted:25}
* <br/>["eh12"]{id:"eh12", name:"morningside", value:4347, value_formatted:4347, associate1:45, associate1_formatted:45}
* <br/>["eh13"]{id:"eh13", name:"merchiston", value:2496, value_formatted:2496, associate1:25, associate1_formatted:25}</p>
*
* @method getData
* @param {String[]} featureIds An optional list of feature ids to get data for.
* @return {Associative Array} As described above.
*/
ia.Associate.prototype.getComparisonData = function(featureIds)
{
var dataHash = {};
var fLength;
var featureArray;
if (featureIds != null)
fLength = featureIds.length;
else
{
featureArray = this.geography.getComparisonFeatures();
fLength = featureArray.length;
}
// Each loop is a new row in the data.
for (var i = 0; i < fLength; i++)
{
// Feature.
var feature;
if (featureIds != null)
feature = this.geography.getComparisonFeature(featureIds[i]);
else
feature = featureArray[i];
// Indicator.
var obj = {};
obj.id = feature.id;
obj.name = feature.name;
obj.value = this.getComparisonValue(feature.id);
obj.value_formatted = this.getFormattedComparisonValue(feature.id);
dataHash[feature.id] = obj;
}
return dataHash;
};