UNPKG

vislite

Version:

灵活、快速、简单的数据可视化交互式跨端前端库

99 lines (96 loc) 3.73 kB
/*! * Cardinal of VISLite JavaScript Library v1.3.0 * git+https://github.com/oi-contrib/VISLite.git */ var Hermite = (function () { function Hermite(u) { if (u === void 0) { u = 0.5; } this.name = 'Hermite'; this.__u = u; } Hermite.prototype.setP = function (x1, y1, x2, y2, s1, s2) { if (x1 < x2) { this.__a = x1; this.__b = x2; var p3 = this.__u * s1, p4 = this.__u * s2; y1 /= (x2 - x1); y2 /= (x2 - x1); this.__MR = [ 2 * y1 - 2 * y2 + p3 + p4, 3 * y2 - 3 * y1 - 2 * p3 - p4, p3, y1 ]; } else throw new Error('The point x-position should be increamented!'); return this; }; Hermite.prototype.use = function (x) { if (this.__MR) { var sx = (x - this.__a) / (this.__b - this.__a), sx2 = sx * sx, sx3 = sx * sx2; var sResult = sx3 * this.__MR[0] + sx2 * this.__MR[1] + sx * this.__MR[2] + this.__MR[3]; return sResult * (this.__b - this.__a); } else throw new Error('You shoud first set the position!'); }; return Hermite; }()); var Cardinal = (function () { function Cardinal(t) { if (t === void 0) { t = 0; } this.name = 'Cardinal'; this.__t = t; } Cardinal.prototype.setP = function (points) { this.__HS = { "x": [], "h": [] }; var flag, slope = (points[1][1] - points[0][1]) / (points[1][0] - points[0][0]), temp; this.__HS.x[0] = points[0][0]; for (flag = 1; flag < points.length; flag++) { if (points[flag][0] <= points[flag - 1][0]) throw new Error('The point position should be increamented!'); this.__HS.x[flag] = points[flag][0]; if (flag < points.length - 1) { if ((points[flag + 1][1] > points[flag][1] && points[flag - 1][1] > points[flag][1]) || (points[flag + 1][1] < points[flag][1] && points[flag - 1][1] < points[flag][1]) || points[flag + 1][1] == points[flag][1] || points[flag - 1][1] == points[flag][1]) { temp = 0; } else { temp = (points[flag + 1][1] - points[flag - 1][1]) / (points[flag + 1][0] - points[flag - 1][0]); } } else { temp = (points[flag][1] - points[flag - 1][1]) / (points[flag][0] - points[flag - 1][0]); } this.__HS.h[flag - 1] = new Hermite((1 - this.__t) * 0.5).setP(points[flag - 1][0], points[flag - 1][1], points[flag][0], points[flag][1], slope, temp); slope = temp; } return this; }; Cardinal.prototype.use = function (x) { if (this.__HS) { this.__i = -1; while (this.__i + 1 < this.__HS.x.length && (x > this.__HS.x[this.__i + 1] || (this.__i == -1 && x >= this.__HS.x[this.__i + 1]))) { this.__i += 1; } if (this.__i < 0) { return this.__HS.h[0].use(this.__HS.x[0]); } else if (this.__i >= this.__HS.h.length) { return this.__HS.h[this.__HS.h.length - 1].use(this.__HS.x[this.__HS.x.length - 1]); } return this.__HS.h[this.__i].use(x); } else { throw new Error('You shoud first set the position!'); } }; return Cardinal; }()); export { Cardinal as default };