Browse Source

Added commond handling for settings with internal prefix 'default'

codeClimate
Wim Rijnders 8 years ago
parent
commit
66ac9ddd3a
1 changed files with 97 additions and 53 deletions
  1. +97
    -53
      lib/graph3d/Graph3d.js

+ 97
- 53
lib/graph3d/Graph3d.js View File

@ -61,7 +61,30 @@ var OPTIONKEYS = [
/**
* Default values for certain option fields.
* Field names in the options hash which are of relevance to the user.
*
* Same as OPTIONKEYS, but internally these fields are stored with
* prefix 'default' in the name.
*/
var PREFIXEDOPTIONKEYS = [
'xBarWidth',
'yBarWidth',
'valueMin',
'valueMax',
'xMin',
'xMax',
'xStep',
'yMin',
'yMax',
'yStep',
'zMin',
'zMax',
'zStep'
];
/**
* Default values for option settings.
*
* These are the values used when a Graph3d instance is initialized
* without custom settings.
@ -103,8 +126,7 @@ var DEFAULTS = {
xCenter : '55%',
yCenter : '50%',
// Following not in OPTIONKEYS because they require special handling,
// Following require special handling, therefore not mentioned in the OPTIONKEYS tables.
style : Graph3d.STYLE.DOT,
tooltip : false,
@ -121,13 +143,55 @@ var DEFAULTS = {
horizontal: 1.0,
vertical : 0.5,
distance : 1.7
}
},
// Following stored internally with field prefix 'default'
xBarWidth : 1,
yBarWidth : 1,
valueMin : 0,
valueMax : 1,
yBarWidth : 1,
xMin : 0,
xMax : 1,
xStep : undefined, // auto by default
yMin : 0,
yMax : 1,
yStep : undefined, // auto by default
zMin : 0,
zMax : 1,
zStep : undefined // auto by default
};
/**
* Make first letter of parameter upper case.
*
* Source: http://stackoverflow.com/a/1026087
*/
function capitalize(str) {
if (str === undefined || str === "") {
return str;
}
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* forcibly copy fields from src to dst in a controlled manner.
* Add a prefix to a field name, taking style guide into account
*/
function prefixFieldName(prefix, fieldName) {
if (prefix === undefined || prefix === "") {
return fieldName;
}
return prefix + capitalize(fieldName);
}
/**
* Forcibly copy fields from src to dst in a controlled manner.
*
* A given field in dst will always be overwitten. If this field
* is undefined or not present in src, the field in dst will
@ -137,12 +201,18 @@ var DEFAULTS = {
*
* Only the fields mentioned in array 'fields' will be handled.
*
* @param fields array with names of fields to copy
* @param fields array with names of fields to copy
* @param prefix optional; prefix to use for the target fields.
*/
function forceCopy(src, dst, fields) {
function forceCopy(src, dst, fields, prefix) {
var srcKey;
var dstKey;
for (var i in fields) {
var field = fields[i];
dst[field] = src[field];
srcKey = fields[i];
dstKey = prefixFieldName(prefix, srcKey);
dst[dstKey] = src[srcKey];
}
}
@ -153,15 +223,20 @@ function forceCopy(src, dst, fields) {
* Only the fields mentioned in array 'fields' will be copied over,
* and only if these are actually defined.
*
* @param fields array with names of fields to copy
* @param fields array with names of fields to copy
* @param prefix optional; prefix to use for the target fields.
*/
function safeCopy(src, dst, fields) {
function safeCopy(src, dst, fields, prefix) {
var srcKey;
var dstKey;
for (var i in fields) {
var field = fields[i];
srcKey = fields[i];
if (src[srcKey] === undefined) continue;
if (src[field] !== undefined) {
dst[field] = src[field];
}
dstKey = prefixFieldName(prefix, srcKey);
dst[dstKey] = src[srcKey];
}
}
@ -198,14 +273,15 @@ function Graph3d(container, data, options) {
this.create();
//
// Start Settings
// Set Defaults
//
// Handle the defaults which can be simply copied over
forceCopy(DEFAULTS, this, OPTIONKEYS);
forceCopy(DEFAULTS, this, PREFIXEDOPTIONKEYS, 'default');
// Following are internal fields, not part of the user settings
this.margin = 10; // px
this.margin = 10; // px
this.showGrayBottom = false; // TODO: this does not work correctly
this.showTooltip = false;
this.dotSizeRatio = 0.02; // size of the dots as a fraction of the graph width
@ -214,10 +290,9 @@ function Graph3d(container, data, options) {
// Handle the more complex ('special') fields
this._setSpecialSettings(DEFAULTS, this);
// The rest of the fields.
// These require special attention in some way
// TODO: handle these
//
// End Set Defaults
//
// the column indexes
this.colX = undefined;
@ -226,25 +301,8 @@ function Graph3d(container, data, options) {
this.colValue = undefined;
this.colFilter = undefined;
this.xMin = 0;
this.xStep = undefined; // auto by default
this.xMax = 1;
this.yMin = 0;
this.yStep = undefined; // auto by default
this.yMax = 1;
this.zMin = 0;
this.zStep = undefined; // auto by default
this.zMax = 1;
this.valueMin = 0;
this.valueMax = 1;
this.xBarWidth = 1;
this.yBarWidth = 1;
// TODO: customize axis range
//
// End Settings
//
// apply options (also when undefined)
this.setOptions(options);
@ -1150,24 +1208,10 @@ Graph3d.prototype.setOptions = function (options) {
// Handle the parameters which can be simply copied over
safeCopy(options, this, OPTIONKEYS);
safeCopy(options, this, PREFIXEDOPTIONKEYS, 'default');
// Handle the more complex ('special') fields
this._setSpecialSettings(options, this);
// Handle the rest of the parameters
if (options.xBarWidth !== undefined) this.defaultXBarWidth = options.xBarWidth;
if (options.yBarWidth !== undefined) this.defaultYBarWidth = options.yBarWidth;
if (options.xMin !== undefined) this.defaultXMin = options.xMin;
if (options.xStep !== undefined) this.defaultXStep = options.xStep;
if (options.xMax !== undefined) this.defaultXMax = options.xMax;
if (options.yMin !== undefined) this.defaultYMin = options.yMin;
if (options.yStep !== undefined) this.defaultYStep = options.yStep;
if (options.yMax !== undefined) this.defaultYMax = options.yMax;
if (options.zMin !== undefined) this.defaultZMin = options.zMin;
if (options.zStep !== undefined) this.defaultZStep = options.zStep;
if (options.zMax !== undefined) this.defaultZMax = options.zMax;
if (options.valueMin !== undefined) this.defaultValueMin = options.valueMin;
if (options.valueMax !== undefined) this.defaultValueMax = options.valueMax;
}
this.setSize(this.width, this.height);

Loading…
Cancel
Save