@visactor/vgrammar-core
Version:
VGrammar is a visual grammar library
26 lines (22 loc) • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.transform = void 0;
const vutils_1 = require("@visactor/vutils"), transform = (options, upstreamData) => {
var _a, _b;
const {field: field, asTransformRatio: asTransformRatio, asReachRatio: asReachRatio, asHeightRatio: asHeightRatio, asValueRatio: asValueRatio, asNextValueRatio: asNextValueRatio, asLastValueRatio: asLastValueRatio, asLastValue: asLastValue, asCurrentValue: asCurrentValue, asNextValue: asNextValue, heightVisual: heightVisual = !1, isCone: isCone = !0, range: range} = options, max = upstreamData.reduce(((m, d) => Math.max(m, Number.parseFloat(d[field]) || -1 / 0)), -1 / 0), min = upstreamData.reduce(((m, d) => Math.min(m, Number.parseFloat(d[field]) || 1 / 0)), 1 / 0), rangeArr = [ null !== (_a = null == range ? void 0 : range.min) && void 0 !== _a ? _a : min, null !== (_b = null == range ? void 0 : range.max) && void 0 !== _b ? _b : max ], data = upstreamData.map(((originDatum, index) => {
var _a, _b;
const datum = Object.assign({}, originDatum), currentValue = Number.parseFloat(datum[field]), lastValue = Number.parseFloat(null === (_a = upstreamData[index - 1]) || void 0 === _a ? void 0 : _a[field]), nextValue = Number.parseFloat(null === (_b = upstreamData[index + 1]) || void 0 === _b ? void 0 : _b[field]), transformRatio = (0,
vutils_1.isValidNumber)(nextValue * currentValue) ? nextValue / currentValue : null, reachRatio = (0,
vutils_1.isValidNumber)(currentValue * currentValue) ? currentValue / lastValue : null;
return asLastValue && (datum[asLastValue] = lastValue), asNextValue && (datum[asNextValue] = nextValue),
asTransformRatio && (datum[asTransformRatio] = transformRatio), asReachRatio && (datum[asReachRatio] = 0 === index ? 1 : reachRatio),
asHeightRatio && (datum[asHeightRatio] = !0 === heightVisual ? transformRatio : 1 / upstreamData.length),
asValueRatio && (datum[asValueRatio] = currentValue / rangeArr[1]), asNextValueRatio && (datum[asNextValueRatio] = index === upstreamData.length - 1 ? isCone ? 0 : datum[asValueRatio] : nextValue / rangeArr[1]),
asLastValueRatio && (datum[asLastValueRatio] = 0 === index ? 1 : lastValue / rangeArr[1]),
asCurrentValue && (datum[asCurrentValue] = currentValue), datum;
}));
return data;
};
exports.transform = transform;
//# sourceMappingURL=funnel.js.map