import * as d3 from "d3"; export function calculateBoxStatistics(data) { let extent = d3.extent(data), q1Val = d3.quantile(data, .25), median = d3.quantile(data, .5), q3Val = d3.quantile(data, .75); return { iqr: (q3Val - q1Val), max: extent[1], median: median, min: extent[0], q1Val: q1Val, q3Val: q3Val }; } export function calculateBinExtent(min: number, max: number, binWidth: number): number[] { let expanse: number = 2 * binWidth; if (min > 0) { if (binWidth > min) { min = binWidth; } else { min = min - (min % binWidth); } } else if (min < 0) { if (-binWidth > min) { min = binWidth; } else { min = min + (min % binWidth); } } min = min - expanse; if (max > 0) { max = max + (binWidth - (max % binWidth)); } else if (max < 0) { max = max - (binWidth - (max % binWidth)); } max = max + expanse; return [min, max]; } export function scrapeTable(tableSelector: string){ //retrieve attributes data from let theadThObjects = d3.selectAll(tableSelector + " thead th").nodes(), theadThAttributes = [], capAttr, tempObject, theadTh, dataset, attr; for (let i = 0; i Id ->categoryId as attribute name capAttr = attr.charAt(0).toUpperCase() + attr.slice(1); tempObject["category" + capAttr] = dataset[attr]; } theadThAttributes.push(tempObject); } //retrieve attributes data from let positiveValueRegex = /^\$?(\d,?)+\.?(\d+)?%?/, negativeValueRegex = /^\(\$?(\d,?)+\.?(\d+)?%?\)/, tbodyTrObjects = d3.selectAll(tableSelector + " tbody tr").nodes(), tbodyTdAttributes = [], trIndex, theadOffset, trTh, trChildrens, trChild, tdIndex = 0, tdMatch, tdValue, tempAttr; for (trIndex = 0; trIndex Id ->categoryId1 as attribute name capAttr = attr.charAt(0).toUpperCase() + attr.slice(1); trTh["category1" + capAttr] = dataset[attr]; } trTh["category1Label"] = trChild.innerText; tdIndex = 1; } for (; tdIndex < trChildrens.length; tdIndex++) { tempObject = {}; trChild = trChildrens[tdIndex]; tdMatch = trChild.innerText.match(negativeValueRegex); tdValue = ''; if (tdMatch !== null) { //retrieve value from , example: (123%), ($123), ($0) tdValue = tdMatch.input.replace(/[^\d.-]/g, ''); tdValue = '-'+tdValue; if (tdValue === '-0'){ tdValue ='0'; } } else { //retrieve value from , example: $123, 123%, 123; tdMatch = trChild.innerText.match(positiveValueRegex); if (tdMatch !== null){ tdValue = tdMatch.input.replace(/[^\d.-]/g, ''); } } tempObject['value'] = parseFloat(tdValue); //merge the attributes from , the attributes from , and the attributes from into the same object //for ES6 //Object.assign(tempObject, trTh, theadThAttributes[tdIndex-1]); //for ES5 and earlier for (tempAttr in trTh){ tempObject[tempAttr] = trTh[tempAttr] } for (tempAttr in theadThAttributes[tdIndex]){ tempObject[tempAttr] = theadThAttributes[tdIndex][tempAttr] } //generate the final object containing all the table data tbodyTdAttributes.push(tempObject); } } return tbodyTdAttributes; } export function getUniqueValues(valueArray1, valueArray2){ let v; const uniqueValueArray = []; for (v of valueArray1){ if (uniqueValueArray.indexOf(v) < 0){ uniqueValueArray.push(v); } } for (v of valueArray2){ if (uniqueValueArray.indexOf(v) < 0){ uniqueValueArray.push(v); } } return uniqueValueArray; } /** * Checks across all objects in the dataArray if each attribute value is the same. * @param dataArray */ export function distinctDataCheck(dataArray){ const dataValues = {}, dataDistinct = {} ; let attr, data ; for (data of dataArray) { for (attr in data) { if (dataValues.hasOwnProperty(attr)){ dataDistinct[attr] = dataDistinct[attr] && (dataValues[attr] === data[attr]); } else { dataValues[attr] = data[attr]; dataDistinct[attr] = true; } } } return dataDistinct; }