js/units.js
/**
 * Finds the desired scale from data.
 * Scale is one of SCALE_TEXT, SCALE_TIME or SCALE_UNITS
 * Returns an index to the scale.
 **/
Finds the desired scale from data.
Scale is one of SCALE_TEXT, SCALE_TIME or SCALE_UNITS
Returns an index to the scale.
Finds the desired scale from data . Scale is one of SCALE_TEXT SCALE_TIME or SCALE_UNITS Returns an index to the scale .
function rescaleFactor(data, attribute, scale, existingScale) {
	if(typeof existingScale == 'undefined') existingScale = 1.0;
	let scaleFactor = 1.0;
	let max = Math.abs(data[0][attribute]);
	for(let i = 1; i < data.length; i++) {
		let v = Math.abs(data[i][attribute]);
		if(v > max) max = v;
	}
	if(isNaN(max) || max == 0)
		return -1;
	if(max >= 1.0 && max < 1000) {
		scaleFactor = existingScale;
	}
	else {
		max *= existingScale;
		scaleFactor = max/9.999;
	}
	let bestDistance = 0.0;
	let bestIndex = -1;
	for(let i = 0; i < scale.length; i++) {
		let d = Math.abs(Math.log(scale[i][0]) - Math.log(scaleFactor));
		if(bestIndex == -1 || d < bestDistance) {
			bestDistance = d;
			bestIndex = i;
		}
	}
	console.log("best at "+bestIndex+" with distance "+bestDistance+" = "+scale[bestIndex][0]+" from "+scaleFactor);
	return bestIndex;
}
function rescaleFactor ( data , attribute , scale , existingScale ) { if ( typeof existingScale == 'undefined' ) existingScale = 1.0 ; let scaleFactor = 1.0 ; let max = Math . abs ( data [ 0 ] [ attribute ] ) ; for ( let i = 1 ; i < data . length ; i ++ ) { let v = Math . abs ( data [ i ] [ attribute ] ) ; if ( v > max ) max = v ; } if ( isNaN ( max ) || max == 0 ) return - 1 ; if ( max >= 1.0 && max < 1000 ) { scaleFactor = existingScale ; } else { max *= existingScale ; scaleFactor = max / 9.999 ; } let bestDistance = 0.0 ; let bestIndex = - 1 ; for ( let i = 0 ; i < scale . length ; i ++ ) { let d = Math . abs ( Math . log ( scale [ i ] [ 0 ] ) - Math . log ( scaleFactor ) ) ; if ( bestIndex == - 1 || d < bestDistance ) { bestDistance = d ; bestIndex = i ; } } console . log ( "best at " + bestIndex + " with distance " + bestDistance + " = " + scale [ bestIndex ] [ 0 ] + " from " + scaleFactor ) ; return bestIndex ; }
Finds the desired scale from data.
Finds the desired scale from data .
//we want a scale factor based on the range //that minimises the number of digits to reach 3 s.f. //let min = max; //if(v != 0 && (v < min || min == 0)) min = v; //the current scale is reasonable //TODO: pick a scale that will either raise or reduce max so its digit length is minimised // ideally in range 0-9 if there is nothing past the decimal. How to check this? // otherwise... 0-99 then 0-999 then 0-9.9  then 0-99.9 or 0-9.99 (how to pick?) //now find the best index