@antv/t8
Version:
T8 is a text visualization solution for unstructured data within the AntV technology stack, and it is a declarative T8 markdown syntax that can be used to describe the content of data interpretation reports.
75 lines (72 loc) • 2.6 kB
JavaScript
;
/**
* Scaling functions for data transformation
* Maps data values to visual coordinates
*/
/**
* Creates a linear scale function that maps values from domain to range
*
* @param domain - Input range [min, max] of values to be scaled
* @param range - Output range [min, max] that domain values will be mapped to
* @returns A function that performs the linear scaling transformation
*/
var scaleLinear = function (domain, range) {
return function (n) {
var d1 = domain[0], d2 = domain[1];
var r1 = range[0], r2 = range[1];
// Avoid division by zero if the domain is a single point.
// In this case, all input values will map to the start of the range.
if (d1 === d2) {
return r1;
}
// If the range is a single point, all input values will map to that point.
if (r1 === r2) {
return r1;
}
// The core linear interpolation formula.
// It calculates the proportional position of 'n' within the domain
// and then maps that position to the corresponding value in the range.
return r1 + ((r2 - r1) * (n - d1)) / (d2 - d1);
};
};
/**
* Standalone ticks function to generate an array of uniformly spaced numbers.
* This is needed because your scaleLinear method doesn't provide it.
*/
var ticks = function (domain, count) {
var dMin = domain[0], dMax = domain[1];
// Handle edge cases
if (dMin === dMax)
return [dMin];
// Calculate the approximate step size
var roughStep = (dMax - dMin) / count;
// Find a "nice" step size based on a power of 10
var exponent = Math.floor(Math.log10(roughStep));
var powerOf10 = Math.pow(10, exponent);
var niceSteps = [1, 2, 5, 10];
var niceStep = 0;
for (var _i = 0, niceSteps_1 = niceSteps; _i < niceSteps_1.length; _i++) {
var s = niceSteps_1[_i];
if (roughStep <= s * powerOf10) {
niceStep = s * powerOf10;
break;
}
}
// Adjust for floating point inaccuracies
if (niceStep === 0) {
niceStep = niceSteps[niceSteps.length - 1] * powerOf10;
}
var result = [];
var start = Math.floor(dMin / niceStep) * niceStep;
var end = Math.ceil(dMax / niceStep) * niceStep;
// Generate the ticks
for (var current = start; current <= end; current += niceStep) {
if (current >= dMin && current <= dMax) {
result.push(current);
}
}
return result;
};
exports.scaleLinear = scaleLinear;
exports.ticks = ticks;
//# sourceMappingURL=scales.js.map