@semcore/chart
Version:
Semrush Chart Component
52 lines (50 loc) • 2.07 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = normalizeCurvePoints;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _hasNull = _interopRequireDefault(require("./hasNull"));
function replaceNullValue(targetPoint, sourcePoint) {
var field;
if (targetPoint.x === null || targetPoint.value && targetPoint.value[0] === 0) field = 'x';
if (targetPoint.y === null || targetPoint.value && targetPoint.value[0] === 0) field = 'y';
return Object.assign({}, targetPoint, field ? (0, _defineProperty2["default"])({}, field, sourcePoint[field]) : {});
}
/**
* Normalizes the array of points for the plot.
* Fills points without data with points from neighboring ones.
* It is necessary to continue the dotted line in those places where there is no data on the edges.
* @param points
* @param dataKey
*/
function normalizeCurvePoints(points, dataKey) {
var curvePoints = Array.from(points);
if ((0, _hasNull["default"])(curvePoints[0], dataKey)) {
var firstDefinedIndex = points.findIndex(function (point) {
return !(0, _hasNull["default"])(point, dataKey);
});
curvePoints.forEach(function (point, index) {
if (index < firstDefinedIndex) {
curvePoints[index] = replaceNullValue(point, points[firstDefinedIndex]);
}
});
}
var length = curvePoints.length;
if ((0, _hasNull["default"])(curvePoints[length - 1], dataKey)) {
var definedIndex = curvePoints.map(function (i) {
return i;
}).reverse().findIndex(function (point) {
return !(0, _hasNull["default"])(point, dataKey);
});
var _firstDefinedIndex = length - 1 - definedIndex;
curvePoints.forEach(function (point, index) {
if (index > _firstDefinedIndex) {
curvePoints[index] = replaceNullValue(point, points[_firstDefinedIndex]);
}
});
}
return curvePoints;
}
//# sourceMappingURL=normalizeCurvePoints.js.map