scichart
Version:
Fast WebGL JavaScript Charting Library and Framework
147 lines (146 loc) • 6.13 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.RadianLabelProvider = void 0;
var NumericLabelProvider_1 = require("./NumericLabelProvider");
/**
* The {@link RadianLabelProvider} formats Axis Labels and Cursor / Tooltips for {@link NumericAxis} types as multiples/fractions of PI
* @note It is highly recommended to use this label provider with these axis options:
* ```ts
* const xAxis = new NumericAxis(wasmContext, {
* labelProvider: new RadianLabelProvider({
* maxDenominator: 4, // or any other non-zero integer
* errorTolerance: 0.0001,
*
* labelPrecision: 2
* // for values that cannot be expressed as fractions of PI within `maxDenominator` constraint,
* // thus default to decimal format
* }),
* autoTicks: false, // to manually set tick distance
* majorDelta: Math.PI / 4, // or any other PI fraction
* });
* ```
*/
var RadianLabelProvider = /** @class */ (function (_super) {
__extends(RadianLabelProvider, _super);
function RadianLabelProvider(options) {
if (options === void 0) { options = {}; }
var _this = this;
var _a, _b;
_this = _super.call(this, options) || this;
_this.maxDenominatorProperty = 12;
_this.errorToleranceProperty = 0.0001;
_this.maxDenominatorProperty = (_a = options.maxDenominator) !== null && _a !== void 0 ? _a : _this.maxDenominatorProperty;
_this.errorToleranceProperty = (_b = options.errorTolerance) !== null && _b !== void 0 ? _b : _this.errorToleranceProperty;
return _this;
}
Object.defineProperty(RadianLabelProvider.prototype, "maxDenominator", {
/**
* Gets or sets the maximum denominator to use when formatting values as fractions of PI
*/
get: function () {
return this.maxDenominatorProperty;
},
set: function (value) {
this.maxDenominatorProperty = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(RadianLabelProvider.prototype, "errorTolerance", {
/**
* Gets or sets the maximum error tolerance when formatting values as fractions of PI
*/
get: function () {
return this.errorToleranceProperty;
},
set: function (value) {
this.errorToleranceProperty = value;
},
enumerable: false,
configurable: true
});
/**
* Used internally.
* Returns the greatest common divisor of two numbers.
*/
RadianLabelProvider.prototype.getGCD = function (a, b) {
while (b !== 0) {
b = a % (a = b);
}
return Math.abs(a);
};
/**
* Used internally.
* Finds the best fraction for a given ratio and maximum denominator, returns decimal if no fraction found.
*/
RadianLabelProvider.prototype.findFraction = function (ratio) {
var bestNumerator = 0;
var bestDenominator = 1;
var bestError = Infinity;
for (var denominator = 1; denominator <= this.maxDenominatorProperty; denominator++) {
var numerator = Math.round(ratio * denominator);
var error = Math.abs(ratio - numerator / denominator);
if (error < bestError) {
bestError = error;
bestNumerator = numerator;
bestDenominator = denominator;
}
}
if (bestError < this.errorToleranceProperty) {
var gcd = this.getGCD(bestNumerator, bestDenominator);
return {
numerator: bestNumerator / gcd,
denominator: bestDenominator / gcd
};
}
return null;
};
Object.defineProperty(RadianLabelProvider.prototype, "formatLabel", {
/**
* @inheritdoc
*/
get: function () {
var _this = this;
return function (dataValue) {
var _a;
if (dataValue === 0)
return "0";
var ratio = dataValue / Math.PI;
var fraction = _this.findFraction(ratio);
if (fraction) {
if (!fraction) {
return dataValue.toString();
}
var numerator = fraction.numerator, denominator = fraction.denominator;
if (denominator === 1) {
return numerator === 1 ? "π" : "".concat(numerator, "\u03C0");
}
if (numerator === 1) {
return "\u03C0/".concat(denominator);
}
return "".concat(numerator, "\u03C0/").concat(denominator);
}
return dataValue.toFixed((_a = _this === null || _this === void 0 ? void 0 : _this.precision) !== null && _a !== void 0 ? _a : 1);
};
},
enumerable: false,
configurable: true
});
return RadianLabelProvider;
}(NumericLabelProvider_1.NumericLabelProvider));
exports.RadianLabelProvider = RadianLabelProvider;