Skip to content

Commit

Permalink
kvLookup is now part of prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
nareshv committed May 17, 2013
1 parent 25f5585 commit 3271a79
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 82 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Checkout http://justgage.com for details, demos, current release and more.

###Update log

######May 17, 2013.
* make kvLookup() of prototype per @toorshia
* https://github.com/toorshia/justgage/pull/59

######April 25, 2013.
* use HTML5 'data' attributes of the DOM Element to render the gauge. (Note: data attributes override the constructor options, if present.)

Expand Down
175 changes: 93 additions & 82 deletions justgage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
* @author Bojan Djuricic (@Toorshia)
*
* LATEST UPDATES
*
* -----------------------------
* May 17, 2013.
* -----------------------------
* make kvLookup() part of prototype per @toorshia
* https://github.com/toorshia/justgage/pull/59
* -----------------------------
* April 25, 2013.
Expand Down Expand Up @@ -110,47 +116,6 @@
console.log("No element with id : %s found", config.id);
return false;
}

//
// tiny helper function to lookup value of a key from two hash tables
// if none found, return defaultvalue
//
// key: string
// tablea: object
// tableb: DOMStringMap|object
// defval: string|integer|float|null
//
var kvLookup = function(key, tablea, tableb, defval, datatype) {
var val = defval;
var canConvert = false;
if (!(key === null || key === undefined)) {
if (tableb !== null && tableb !== undefined && typeof tableb === "object" && key in tableb) {
val = tableb[key];
canConvert = true;
} else if (tablea !== null && tablea !== undefined && typeof tablea === "object" && key in tablea) {
val = tablea[key];
canConvert = true;
} else {
val = defval;
}
if (canConvert === true) {
if (datatype !== null && datatype !== undefined) {
switch(datatype) {
case 'int':
val = parseInt(val, 10);
break;
case 'float':
val = parseFloat(val);
break;
default:
break;
}
}
}
}
return val;
};

// configurable parameters
obj.config =
{
Expand All @@ -160,163 +125,163 @@

// parentNode : node object
// this is container element
parentNode : kvLookup('parentNode', config, dataset, null),
parentNode : obj.kvLookup('parentNode', config, dataset, null),

// width : int
// gauge width
width : kvLookup('width', config, dataset, null),
width : obj.kvLookup('width', config, dataset, null),

// height : int
// gauge height
height : kvLookup('height', config, dataset, null),
height : obj.kvLookup('height', config, dataset, null),

// title : string
// gauge title
title : kvLookup('title', config, dataset, ""),
title : obj.kvLookup('title', config, dataset, ""),

// titleFontColor : string
// color of gauge title
titleFontColor : kvLookup('titleFontColor', config, dataset, "#999999"),
titleFontColor : obj.kvLookup('titleFontColor', config, dataset, "#999999"),

// value : int
// value gauge is showing
value : kvLookup('value', config, dataset, 0, 'float'),
value : obj.kvLookup('value', config, dataset, 0, 'float'),

// valueFontColor : string
// color of label showing current value
valueFontColor : kvLookup('valueFontColor', config, dataset, "#010101"),
valueFontColor : obj.kvLookup('valueFontColor', config, dataset, "#010101"),

// symbol : string
// special symbol to show next to value
symbol : kvLookup('symbol', config, dataset, ''),
symbol : obj.kvLookup('symbol', config, dataset, ''),

// min : float
// min value
min : kvLookup('min', config, dataset, 0, 'float'),
min : obj.kvLookup('min', config, dataset, 0, 'float'),

// max : int
// max value
max : kvLookup('max', config, dataset, 100, 'float'),
max : obj.kvLookup('max', config, dataset, 100, 'float'),

// humanFriendlyDecimal : int
// number of decimal places for our human friendly number to contain
humanFriendlyDecimal : kvLookup('humanFriendlyDecimal', config, dataset, 0),
humanFriendlyDecimal : obj.kvLookup('humanFriendlyDecimal', config, dataset, 0),

// textRenderer: func
// function applied before rendering text
textRenderer : kvLookup('textRenderer', config, dataset, null),
textRenderer : obj.kvLookup('textRenderer', config, dataset, null),

// gaugeWidthScale : float
// width of the gauge element
gaugeWidthScale : kvLookup('gaugeWidthScale', config, dataset, 1.0),
gaugeWidthScale : obj.kvLookup('gaugeWidthScale', config, dataset, 1.0),

// gaugeColor : string
// background color of gauge element
gaugeColor : kvLookup('gaugeColor', config, dataset, "#edebeb"),
gaugeColor : obj.kvLookup('gaugeColor', config, dataset, "#edebeb"),

// label : string
// text to show below value
label : kvLookup('label', config, dataset, ''),
label : obj.kvLookup('label', config, dataset, ''),

// labelFontColor : string
// color of label showing label under value
labelFontColor : kvLookup('labelFontColor', config, dataset, "#b3b3b3"),
labelFontColor : obj.kvLookup('labelFontColor', config, dataset, "#b3b3b3"),

// shadowOpacity : int
// 0 ~ 1
shadowOpacity : kvLookup('shadowOpacity', config, dataset, 0.2),
shadowOpacity : obj.kvLookup('shadowOpacity', config, dataset, 0.2),

// shadowSize: int
// inner shadow size
shadowSize : kvLookup('shadowSize', config, dataset, 5),
shadowSize : obj.kvLookup('shadowSize', config, dataset, 5),

// shadowVerticalOffset : int
// how much shadow is offset from top
shadowVerticalOffset : kvLookup('shadowVerticalOffset', config, dataset, 3),
shadowVerticalOffset : obj.kvLookup('shadowVerticalOffset', config, dataset, 3),

// levelColors : string[]
// colors of indicator, from lower to upper, in RGB format
levelColors : kvLookup('levelColors', config, dataset, [ "#a9d70b", "#f9c802", "#ff0000" ]),
levelColors : obj.kvLookup('levelColors', config, dataset, [ "#a9d70b", "#f9c802", "#ff0000" ], 'array', ','),

// startAnimationTime : int
// length of initial animation
startAnimationTime : kvLookup('startAnimationTime', config, dataset, 700),
startAnimationTime : obj.kvLookup('startAnimationTime', config, dataset, 700),

// startAnimationType : string
// type of initial animation (linear, >, <, <>, bounce)
startAnimationType : kvLookup('startAnimationType', config, dataset, '>'),
startAnimationType : obj.kvLookup('startAnimationType', config, dataset, '>'),

// refreshAnimationTime : int
// length of refresh animation
refreshAnimationTime : kvLookup('refreshAnimationTime', config, dataset, 700),
refreshAnimationTime : obj.kvLookup('refreshAnimationTime', config, dataset, 700),

// refreshAnimationType : string
// type of refresh animation (linear, >, <, <>, bounce)
refreshAnimationType : kvLookup('refreshAnimationType', config, dataset, '>'),
refreshAnimationType : obj.kvLookup('refreshAnimationType', config, dataset, '>'),

// donutStartAngle : int
// angle to start from when in donut mode
donutStartAngle : kvLookup('donutStartAngle', config, dataset, 90),
donutStartAngle : obj.kvLookup('donutStartAngle', config, dataset, 90),

// valueMinFontSize : int
// absolute minimum font size for the value
valueMinFontSize : kvLookup('valueMinFontSize', config, dataset, 16),
valueMinFontSize : obj.kvLookup('valueMinFontSize', config, dataset, 16),

// titleMinFontSize
// absolute minimum font size for the title
titleMinFontSize : kvLookup('titleMinFontSize', config, dataset, 10),
titleMinFontSize : obj.kvLookup('titleMinFontSize', config, dataset, 10),

// labelMinFontSize
// absolute minimum font size for the label
labelMinFontSize : kvLookup('labelMinFontSize', config, dataset, 10),
labelMinFontSize : obj.kvLookup('labelMinFontSize', config, dataset, 10),

// minLabelMinFontSize
// absolute minimum font size for the minimum label
minLabelMinFontSize : kvLookup('minLabelMinFontSize', config, dataset, 10),
minLabelMinFontSize : obj.kvLookup('minLabelMinFontSize', config, dataset, 10),

// maxLabelMinFontSize
// absolute minimum font size for the maximum label
maxLabelMinFontSize : kvLookup('maxLabelMinFontSize', config, dataset, 10),
maxLabelMinFontSize : obj.kvLookup('maxLabelMinFontSize', config, dataset, 10),

// hideValue : bool
// hide value text
hideValue : kvLookup('hideValue', config, dataset, false),
hideValue : obj.kvLookup('hideValue', config, dataset, false),

// hideMinMax : bool
// hide min and max values
hideMinMax : kvLookup('hideMinMax', config, dataset, false),
hideMinMax : obj.kvLookup('hideMinMax', config, dataset, false),

// hideInnerShadow : bool
// hide inner shadow
hideInnerShadow : kvLookup('hideInnerShadow', config, dataset, false),
hideInnerShadow : obj.kvLookup('hideInnerShadow', config, dataset, false),

// humanFriendly : bool
// convert large numbers for min, max, value to human friendly (e.g. 1234567 -> 1.23M)
humanFriendly : kvLookup('humanFriendly', config, dataset, false),
humanFriendly : obj.kvLookup('humanFriendly', config, dataset, false),

// noGradient : bool
// whether to use gradual color change for value, or sector-based
noGradient : kvLookup('noGradient', config, dataset, false),
noGradient : obj.kvLookup('noGradient', config, dataset, false),

// donut : bool
// show full donut gauge
donut : kvLookup('donut', config, dataset, false),
donut : obj.kvLookup('donut', config, dataset, false),

// relativeGaugeSize : bool
// whether gauge size should follow changes in container element size
relativeGaugeSize : kvLookup('relativeGaugeSize', config, dataset, false),
relativeGaugeSize : obj.kvLookup('relativeGaugeSize', config, dataset, false),

// counter : bool
// animate level number change
counter : kvLookup('counter', config, dataset, false),
counter : obj.kvLookup('counter', config, dataset, false),

// decimals : int
// number of digits after floating point
decimals : kvLookup('decimals', config, dataset, 0),
decimals : obj.kvLookup('decimals', config, dataset, 0),

// customSectors : [] of objects
// number of digits after floating point
customSectors : kvLookup('customSectors', config, dataset, [])
customSectors : obj.kvLookup('customSectors', config, dataset, [])
};

// variables
Expand Down Expand Up @@ -347,7 +312,7 @@
// overflow values
if (obj.config.value > obj.config.max) obj.config.value = obj.config.max;
if (obj.config.value < obj.config.min) obj.config.value = obj.config.min;
obj.originalValue = kvLookup('value', config, dataset, -1, 'int');
obj.originalValue = obj.kvLookup('value', config, dataset, -1, 'int');

// create canvas
if (obj.config.id !== null && (document.getElementById(obj.config.id)) !== null) {
Expand Down Expand Up @@ -745,6 +710,52 @@
obj.txtLabel.animate({"fill-opacity":"1"}, obj.config.startAnimationTime, obj.config.startAnimationType);
};

//
// tiny helper function to lookup value of a key from two hash tables
// if none found, return defaultvalue
//
// key: string
// tablea: object
// tableb: DOMStringMap|object
// defval: string|integer|float|null
// datatype: return datatype
// delimiter: delimiter to be used in conjunction with datatype formatting
//
JustGage.prototype.kvLookup = function(key, tablea, tableb, defval, datatype, delimiter) {
var val = defval;
var canConvert = false;
if (!(key === null || key === undefined)) {
if (tableb !== null && tableb !== undefined && typeof tableb === "object" && key in tableb) {
val = tableb[key];
canConvert = true;
} else if (tablea !== null && tablea !== undefined && typeof tablea === "object" && key in tablea) {
val = tablea[key];
canConvert = true;
} else {
val = defval;
}
if (canConvert === true) {
if (datatype !== null && datatype !== undefined) {
switch(datatype) {
case 'int':
val = parseInt(val, 10);
break;
case 'float':
val = parseFloat(val);
break;
case 'array':
delimiter = delimiter ? delimiter : ',';
val = val.split(delimiter);
break;
default:
break;
}
}
}
}
return val;
};

/** Refresh gauge level */
JustGage.prototype.refresh = function(val, max) {

Expand Down

0 comments on commit 3271a79

Please sign in to comment.