UNPKG

@domoinc/multiline-chart

Version:

MultiLineChart - Domo Widget

111 lines (93 loc) 5.02 kB
//---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- // CatigoryIndexGenerator Generator: // This extension generates categories for data. You send in the Parameters // number of categories and the range for min and max. The function then generates // the correlating number of categories based of the range and appends a no // data catigory to the beginning. // // Detailed Explaination: This means that if you requested 4 categories you’ll // actually get 5, with the zero catigory being -2 to zero, the 1 catigory containing // your min, and so on with the 5 catigory containing your max. // // After calling this function it returns an object containing a 2d array, you can // then call the following functions on the object: // 1. GetCatigoryIndexForValue(value) : which allows you to get the catigory // for any value that you send to the object // 2. UpdateCatigoryRanges(newNumCategories, newDataValueRange): Essentially // refactors the catigory object with a new number of categories and a new // data value range. // // Example: // var cat_object = d3.DomoCatigoryIndexGenerator(4, [0,100]) // // cat_object is now initialized // cat_object.getCatigoryIndexForValue(25) // // returns 2 // cat_object.getCatigoryIndexForValue(50) // // returns 3 // cat_object.getCatigoryIndexForValue(49.99) // // returns 2 // cat_object.getCatigoryIndexForValue(5000) // // returns -1 // console.log(cat_object.rangeForEachCatigory) // //Logs [ [-2, 0], [0, 25], [25, 50], [50, 75], [75, 101] ] // // on Evaluation it's Greater than or equal to the bottom but always less than the top // // //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- d3.DomoCatigoryIndexGenerator = function(numberOfCatigories, dataRange) { //********************************************************************************** // Returns an array of ranges. One range for each 'numCategories' given the // the min and max values specified in dataValueRange. // dataValueRange == [min, max] // numCategories == 5 //********************************************************************************** function genRangesInEachColorCatigory(dataValueRange, numCategories) { var rangesInEachColorCatigory = []; rangesInEachColorCatigory.push([-2, 0]); var deltaStep = Math.floor((dataValueRange[1] - dataValueRange[0]) / numCategories); var stepVal = dataValueRange[0]; //Append Catigory labels for (var i = 0; i < numCategories - 1; i++) { rangesInEachColorCatigory.push([stepVal, stepVal + deltaStep]); stepVal += deltaStep; } //Append Top Catigory label rangesInEachColorCatigory.push([stepVal, dataValueRange[1] + 1]); return rangesInEachColorCatigory; } //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- var cateGenerator = { numCategories: numberOfCatigories, range: dataRange, rangeForEachCatigory: null, //********************************************************************************** // getCatigoryIndexForValue: This function will convert a value within a given // range into and index. //********************************************************************************** getCatigoryIndexForValue: function(value) { if (cateGenerator.rangeForEachCatigory == null) cateGenerator.rangeForEachCatigory = genRangesInEachColorCatigory(cateGenerator.range, cateGenerator.numCategories); if (value == undefined) return 0; for (var i = 0; i < cateGenerator.rangeForEachCatigory.length; i++) { if (cateGenerator.rangeForEachCatigory[i][0] <= value && value < cateGenerator.rangeForEachCatigory[i][1]) return i; } console.log("Error: returning index of catigory wasn't found for value:" + value); return -1; }, //********************************************************************************** // Updates the catIndexGenerators valueRange, numberOfCatigories, and // the array of ranges in each catigory. // IE: call on data update //********************************************************************************** updateCatigoryRanges: function(newNumCategories, newDataValueRange) { cateGenerator.range = newDataValueRange; cateGenerator.numCategories = newNumCategories; cateGenerator.rangeForEachCatigory = genRangesInEachColorCatigory(newDataValueRange, newNumCategories); }, }; return cateGenerator; };