Created
May 16, 2014 16:30
-
-
Save stormpython/a2690332f0993cc7a1a5 to your computer and use it in GitHub Desktop.
zeroInjection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function(require) { | |
'use strict'; | |
var _ = require('lib/lodash/dist/lodash'); | |
/* Returns the indexed position of a value in an array. */ | |
function isValInArr(val, arr) { | |
for (var i = 0; i < arr.length; i++) { | |
// if (!arr[i]) { arr.splice(i, 1); return 0; } | |
if (arr[i].x === val) { return i; } | |
} | |
return 0; | |
} | |
/* Injects an object with zero for its y value | |
* in an array and returns the array. */ | |
function injectZero(array, index, val) { | |
array.splice(index, 0, { x: val, y: 0 }); | |
return array; | |
} | |
/* Moves a value from its current index to a new indexed position | |
* in an array and returns the array. */ | |
function injectIndex(array, index, valObj, valIndex) { | |
// console.log(array, index, valObj, valIndex); | |
var copyObj = valObj; | |
// Moves value to current index | |
// array.splice(valIndex, 1); | |
array.splice(index, 0, copyObj); | |
return array; | |
} | |
/* Returns an array of ordered keys for a data series | |
* or several arrays of data values. | |
*/ | |
function orderKeys(data) { | |
var uniqueXObjs = {}, orderedKeys; | |
data.forEach(function(series) { | |
series.values.forEach(function(d, i) { | |
var key = d.x; | |
uniqueXObjs[key] = uniqueXObjs[key] === void 0 ? i : Math.min(i, uniqueXObjs[key]); | |
}); | |
}); | |
orderedKeys = _(uniqueXObjs).pairs().sortBy(1).pluck(0).value(); | |
return orderedKeys; | |
} | |
function zeroFill(key, val, dataArray, newArray, keyIndex) { | |
/* arr1 = orderedKeys array | |
* arr2 = data values array | |
* dataArray = array of objects, i.e. [{ x: 'html', y: 45 }, ..] | |
* index = the current index of the data value. | |
*/ | |
var valIndex = isValInArr(key, dataArray), | |
obj = dataArray[valIndex]; | |
return !val ? injectZero(newArray, keyIndex, key) : key === val.x && !!valIndex ? injectIndex(newArray, keyIndex, obj) : | |
key !== val.x && !valIndex ? injectIndex(newArray, keyIndex, obj, valIndex) : injectZero(newArray, keyIndex, key); | |
// : !!valIndex ? | |
// injectIndex(dataArray, keyIndex, obj, valIndex) : dataArray; | |
} | |
function test() { | |
var kyArr = ['MD', 'CN', 'GA']; | |
var arr = [{x: 'CN', y: 1}, {x: 'MD', y: 2}], | |
newArray = []; | |
for (var i = 0; i < 3; i++) { | |
var val = arr[i]; | |
var ky = kyArr[i]; | |
zeroFill(ky, val, arr, newArray, i); | |
} | |
// console.log(newArray); | |
return 0; | |
} | |
// console.log(test()); | |
function main(data) { | |
var orderedKeys = orderKeys(data.series), | |
key, val, newData = data, | |
finishedArray = []; | |
newData.series.forEach(function(series) { | |
var i, max = orderedKeys.length; | |
var newArray = []; | |
for (i = 0; i < max; i++) { | |
key = orderedKeys[i]; | |
val = series.values[i]; | |
zeroFill(key, val, series.values, newArray, i); | |
} | |
finishedArray.push(newArray); | |
}); | |
console.log(finishedArray); | |
return finishedArray; | |
} | |
return function(data) { | |
return main(data); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment