@domoinc/multiline-chart
Version:
MultiLineChart - Domo Widget
77 lines (63 loc) • 2.28 kB
JavaScript
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// Unique ID Generator:
// Example:
// d3.DomoUniqueIdFromPrefix("DomoClipPath") -> "DomoClipPath_0"
// ...append("rect").attr("id", "DomoClipPath_0");
// d3.DomoUniqueIdFromPrefix("DomoClipPath") -> "DomoClipPath_1"
// ...append("rect").attr("id", "DomoClipPath_1");
// d3.DomoUniqueIdFromPrefix("DomoClipPath") -> "DomoClipPath_2"
// ...append("rect").attr("id", "DomoClipPath_2");
// ...
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
da.DomoUniqueIdFromPrefix = function (prefix) {
/**
* Return a unique identifier with the given `len`.
*
* utils.uid(10);
* // => "FDaS435D2z"
*
* @param {Number} len
* @return {String}
* @api private
*/
function uid(len) {
var buf = []
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charlen = chars.length;
for (var i = 0; i < len; ++i) {
buf.push(chars[getRandomInt(0, charlen - 1)]);
}
return buf.join('');
}
/**
* Return a random int, used by `utils.uid()`
*
* @param {Number} min
* @param {Number} max
* @return {Number}
* @api private
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Returns the number of milliseconds since midnight Jan 1, 1970
*/
function getDateTimeMilliseconds()
{
return (new Date()).getTime();
}
//**********************************************************************************
// Searches Dom for an Id with the given prefix.
// Return the prefix + (number of id's found with the given prefix)
// Thus generating a unique id.
//**********************************************************************************
function generateNewUniqueDomoClipPathId(aPrefix) {
return aPrefix + "_" + getDateTimeMilliseconds() + uid(100) + "_";
}
return generateNewUniqueDomoClipPathId(prefix);
};
//Backwards Compatibility
d3.DomoUniqueIdFromPrefix = da.DomoUniqueIdFromPrefix;