@domoinc/multiline-chart
Version:
MultiLineChart - Domo Widget
117 lines (103 loc) • 4.87 kB
JavaScript
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// Number Cleanup:
// This function will take a number and format it to something with up to 3 digits
// and a decimal point, as a result of this process something needs an abbreviation,
// so 10000 becomes 10K, 10100 become 10.1K, and 10010 becomes 10.01K.
// Thif function works regardless of if the number is a string or
// if the param is an actual integer value
//
// Example:
// var formattedNum = d3.DomoNumberCleanup(10000)
// // returns “10K”
// formattedNum = d3.DomoNumberCleanup(“10000”)
// // returns “10K”
// formattedNum = d3.DomoNumberCleanup(“1100000”)
// // returns “1.1M"
//
// Test Cases:
// Tested on PI
// Tested on random numbers using a function like this: function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min;}
// Tested on 10000, 1100000, 10010, 10100
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
d3.DomoNumberCleanup = function (number, numOfDigits){
if (numOfDigits === undefined){
numOfDigits === 3
}
var cleanedNum = d3.DomoFormatNum((+number), numOfDigits)
return cleanedNum[0] + cleanedNum [1];
};
//**********************************************************************************
// Formatting Function: This function (combined with d3.DomoShortenNum function below)
// originally came from James. I've heavily modified it to take a number and format it to
// something with up to 3 digits and a decimal point, as a result of this process
// something needs an abbreviation, so 10000 becomes 10K 10100 become 10.1K
// and 10010 becomes 10.0K.
//
// There is NO rounding.
// This function operates on the idea that truncation doesn't falsely represent data
//
//
// formatNum finds the value of the number and as long as it is lower than a Quintillion
// it will call the function shortenNum on the value divided by highest possible whole
// value either 10 to the 6th 10 to the 9th etc. up to ten to the 15th
// returns array with number at index 0 and magnitude at index 1
//**********************************************************************************
d3.DomoFormatNum = function(num, numToShow){
if (numToShow === undefined){
numToShow = 3
}
if( num >= 1000000000000000){
return shortenNum((+num/1000000000000000), "Q", numToShow);
}else if(num >= 1000000000000){
return shortenNum((+num/1000000000000), "T", numToShow);
}else if(num >= 1000000000){
return shortenNum((+num/1000000000), "B", numToShow);
}else if(num >= 1000000){
return shortenNum((+num/1000000), "M", numToShow);
}else if(num >= 1000){
return shortenNum((+num/1000), "K", numToShow);
}else{
return shortenNum(+num, '', numToShow);
}
//**********************************************************************************
// ShortenNum function:
// 1. Converts number to a string and stores the original decimal value
// 2. If there is no decimal value then the number is just returned (for example) 1000 is 1K
// 3. Uses the decimal to break the string into two parts
// 4. combines the two parts without the decimal
// 5. loops through two parts to truncate it down to 3 characters or less (decimal is not included)
// 5a. loop is prevented from going to infinity by capping the amount of loops possible at 5000
// 6. Period is added back into it's original position
// 7. If the last character is a period it removes the period
// 8. Add the abbreviation sent in from the format number function
//**********************************************************************************
function shortenNum (number, abbrev, numberToShow){
if (numberToShow === undefined || numberToShow < 3){
numberToShow = 3
}
var numberString = number + ''
var indexOfPeriod = numberString.indexOf('.')
if (indexOfPeriod === -1){
return [numberString, abbrev]
}
var everythingAfterDecimal = numberString.substring(indexOfPeriod + 1, numberString.length)
if (everythingAfterDecimal.length > 2){
everythingAfterDecimal = everythingAfterDecimal.substring(0, 2)
}
var everythingBeforeDecimal = numberString.substring(0, indexOfPeriod);
var result = everythingBeforeDecimal + everythingAfterDecimal
var infinitePreventer = 0
while (result.length > numberToShow && infinitePreventer < 5000){
var length = result.length -1
result = result.substring(0, length)
infinitePreventer ++;
}
result = result.substring(0, indexOfPeriod) + '.' + result.substring(indexOfPeriod, result.length)
if ((result.length - 1) === result.indexOf('.')){
result = result.substring(0, result.length - 1)
}
return [result, abbrev]
}
};