UNPKG

ag-charts-community

Version:

Advanced Charting / Charts supporting Javascript / Typescript / React / Angular / Vue

95 lines 2.9 kB
import { calculateNiceSecondaryAxis } from "../../util/secondaryAxisTicks"; import { LinearScale } from "../../scale/linearScale"; import { extent } from "../../util/array"; import { isContinuous } from "../../util/value"; import { ChartAxis } from "../chartAxis"; // Instead of clamping the values outside of domain to the range, // return NaNs to indicate invalid input. export function clamper(domain) { let a = domain[0]; let b = domain[domain.length - 1]; if (a > b) { [a, b] = [b, a]; } return x => x >= a && x <= b ? x : NaN; } export class NumberAxis extends ChartAxis { constructor() { super(new LinearScale()); this._nice = true; this._min = NaN; this._max = NaN; this.scale.clamper = clamper; } set nice(value) { if (this._nice !== value) { this._nice = value; if (value && this.scale.nice) { this.scale.nice(this.tick.count); } } } get nice() { return this._nice; } setDomain(domain, primaryTickCount) { const { scale, min, max } = this; if (domain.length > 2) { domain = extent(domain, isContinuous, Number) || [0, 1]; } domain = [ isNaN(min) ? domain[0] : min, isNaN(max) ? domain[1] : max ]; if (primaryTickCount) { // when `primaryTickCount` is supplied the current axis is a secondary axis which needs to be aligned to // the primary by constraining the tick count to the primary axis tick count const [d, ticks] = calculateNiceSecondaryAxis(domain, primaryTickCount); scale.domain = d; this.ticks = ticks; return; } else { scale.domain = domain; this.onLabelFormatChange(this.label.format); // not sure why this is required? this.scale.clamp = true; if (this.nice && this.scale.nice) { this.scale.nice(this.tick.count); } } } set domain(domain) { this.setDomain(domain); } get domain() { return this.scale.domain; } set min(value) { if (this._min !== value) { this._min = value; if (!isNaN(value)) { this.scale.domain = [value, this.scale.domain[1]]; } } } get min() { return this._min; } set max(value) { if (this._max !== value) { this._max = value; if (!isNaN(value)) { this.scale.domain = [this.scale.domain[0], value]; } } } get max() { return this._max; } formatDatum(datum) { return datum.toFixed(2); } } NumberAxis.className = 'NumberAxis'; NumberAxis.type = 'number'; //# sourceMappingURL=numberAxis.js.map