@rcsb/rcsb-saguaro
Version:
RCSB 1D Feature Viewer
140 lines (139 loc) • 5.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RcsbLineDisplay = void 0;
const RcsbAbstractDisplay_1 = require("./RcsbAbstractDisplay");
const RcsbD3LineManager_1 = require("../RcsbD3/RcsbD3DisplayManager/RcsbD3LineManager");
const d3_shape_1 = require("d3-shape");
const d3fc_sample_1 = require("@d3fc/d3fc-sample");
const RcsbD3ScaleFactory_1 = require("../RcsbD3/RcsbD3ScaleFactory");
class RcsbLineDisplay extends RcsbAbstractDisplay_1.RcsbAbstractDisplay {
constructor() {
super(...arguments);
this.yScale = RcsbD3ScaleFactory_1.RcsbD3ScaleFactory.getLinearScale();
this.maxPoints = 1000;
this.innerData = new Array();
this.SUFFIX_ID = "line_";
this.definedScale = false;
this.line = (0, d3_shape_1.line)().curve(d3_shape_1.curveStep);
}
setInterpolationType(type) {
if (type === "cardinal" /* InterpolationTypes.CARDINAL */)
this.line = (0, d3_shape_1.line)().curve(d3_shape_1.curveCardinal);
else if (type === "step" /* InterpolationTypes.STEP */)
this.line = (0, d3_shape_1.line)().curve(d3_shape_1.curveStep);
else if (type === "basis" /* InterpolationTypes.BASIS */)
this.line = (0, d3_shape_1.line)().curve(d3_shape_1.curveBasis);
else if (type === "linear" /* InterpolationTypes.LINEAR */)
this.line = (0, d3_shape_1.line)().curve(d3_shape_1.curveLinear);
}
yDomain(domain) {
this._yDomain = domain;
}
setScale() {
if (typeof this.height() === "number" && this._yDomain.length == 2 && typeof this._yDomain[0] === "number" && typeof this._yDomain[1] === "number") {
this.yScale
.domain(this._yDomain)
.range([this.height() - 3, 3]);
this.definedScale = true;
}
else {
throw "FATAL ERROR: d3 scale unknown format";
}
}
setLine() {
this.line
.x((d) => {
var _a;
return (_a = this.xScale(d.begin)) !== null && _a !== void 0 ? _a : 0;
})
.y((d) => {
var _a;
return (_a = this.yScale(d.value)) !== null && _a !== void 0 ? _a : 0;
});
}
updateLine() {
this.line.x((d) => {
var _a;
return (_a = this.xScale(d.begin)) !== null && _a !== void 0 ? _a : 0;
});
}
_update(where, compKey) {
this.geoPlot(this.data().filter((s, i) => {
if (s.end == null) {
return (s.begin >= where.from && s.begin <= where.to);
}
else {
return !(s.begin > where.to || s.end < where.from);
}
}));
}
geoPlot(data) {
if (!this.definedScale) {
this.setScale();
this.setLine();
}
this.linePoints = this.downSampling(data);
const config = {
points: this.linePoints,
line: this.line,
color: this._displayColor,
trackG: this.g,
id: this.SUFFIX_ID + "0"
};
RcsbD3LineManager_1.RcsbD3LineManager.plot(config);
}
move() {
this.updateLine();
const config = {
points: this.linePoints,
line: this.line,
trackG: this.g,
id: this.SUFFIX_ID + "0"
};
RcsbD3LineManager_1.RcsbD3LineManager.move(config);
this.setDataUpdated(false);
}
downSampling(points) {
let out = [];
const tmp = [];
const domain = { min: Number.MAX_SAFE_INTEGER, max: Number.MIN_SAFE_INTEGER };
points.forEach(p => {
if (p.begin < domain.min)
domain.min = p.begin - 0.5;
if (p.begin > domain.max)
domain.max = p.begin + 0.5;
});
domain.min = Math.max(domain.min, this.xScale.domain()[0]);
domain.max = Math.min(domain.max, this.xScale.domain()[1]);
const thr = this.maxPoints;
for (let n = Math.ceil(domain.min); n < domain.max; n++) {
tmp[n] = { begin: n, value: 0 };
}
points.forEach((p) => {
if (p.begin > domain.min && p.begin < domain.max) {
tmp[p.begin] = p;
}
});
const filterPoints = tmp.filter(p => (p.begin > domain.min && p.begin < domain.max));
filterPoints.forEach((p, n) => {
var _a, _b;
if (!(((_a = out[out.length - 1]) === null || _a === void 0 ? void 0 : _a.value) == p.value && p.value == ((_b = filterPoints[n + 1]) === null || _b === void 0 ? void 0 : _b.value)))
out.push(p);
this.innerData[p.begin] = p;
});
out.unshift({ begin: domain.min, value: 0 });
out.unshift({ begin: this.xScale.domain()[0], value: 0 });
out.push({ begin: domain.max, value: 0 });
out.push({ begin: this.xScale.domain()[1], value: 0 });
if (out.length > thr) {
const bucketSize = out.length / thr;
const sampler = (0, d3fc_sample_1.largestTriangleOneBucket)();
sampler.bucketSize(bucketSize);
sampler.x((d) => { return d.begin; });
sampler.y((d) => { return d.value; });
out = sampler(out);
}
return out;
}
}
exports.RcsbLineDisplay = RcsbLineDisplay;