@rcsb/rcsb-saguaro
Version:
RCSB 1D Feature Viewer
83 lines (82 loc) • 3.39 kB
JavaScript
import { select } from "d3-selection";
import { RcsbD3Constants } from "../RcsbD3Constants";
import { asyncScheduler, Subscription } from "rxjs";
export class RcsbD3SequenceManager {
constructor() {
this.textElements = select(RcsbD3Constants.EMPTY);
this.plotTask = new Subscription();
}
plot(config) {
this.plotTask.unsubscribe();
this.plotTask = asyncScheduler.schedule(() => {
var _a;
const xScale = config.xScale;
const yScale = config.yScale;
this.textElements = config.elements.select(RcsbD3Constants.TEXT);
this.textElements
.attr(RcsbD3Constants.X, (d) => {
var _a;
return (_a = xScale(d.begin)) !== null && _a !== void 0 ? _a : 0;
})
.attr(RcsbD3Constants.Y, (_a = yScale(Math.floor(config.height * 0.5) + 4)) !== null && _a !== void 0 ? _a : 0)
.transition()
.duration(500)
.attr(RcsbD3Constants.FONT_SIZE, "10")
.attr(RcsbD3Constants.FONT_FAMILY, "Arial")
.attr(RcsbD3Constants.TEXT_ANCHOR, "middle")
.attr(RcsbD3Constants.FILL, (d) => {
if (typeof d.color === "string") {
return d.color;
}
else if (typeof config.color === "string") {
return config.color;
}
else {
console.warn("Config color noy found");
return "#CCCCCC";
}
}).text((d) => {
return d.label || "";
}).attr(RcsbD3Constants.FILL_OPACITY, () => {
return RcsbD3SequenceManager.opacity(xScale, config.intervalRatio);
});
});
}
static plotSequenceLine(config) {
var _a, _b;
config.g.select(RcsbD3Constants.LINE).remove();
config.g.append(RcsbD3Constants.LINE)
.style(RcsbD3Constants.STROKE_WIDTH, 2)
.style(RcsbD3Constants.STROKE, "#DDDDDD")
.attr(RcsbD3Constants.STROKE_DASH, "2")
.attr(RcsbD3Constants.X1, config.xScale.range()[0])
.attr(RcsbD3Constants.Y1, (_a = config.yScale(config.height * 0.5)) !== null && _a !== void 0 ? _a : 0)
.attr(RcsbD3Constants.X2, config.xScale.range()[1])
.attr(RcsbD3Constants.Y2, (_b = config.yScale(config.height * 0.5)) !== null && _b !== void 0 ? _b : 0);
}
move(config) {
asyncScheduler.schedule(() => {
const xScale = config.xScale;
this.textElements
.attr(RcsbD3Constants.X, (d) => {
var _a;
return (_a = xScale(d.begin)) !== null && _a !== void 0 ? _a : 0;
})
.attr(RcsbD3Constants.FILL_OPACITY, () => {
return RcsbD3SequenceManager.opacity(xScale, config.intervalRatio);
});
});
}
static opacity(xScale, intervalRatio) {
const r = (xScale.range()[1] - xScale.range()[0]) / (xScale.domain()[1] - xScale.domain()[0]);
const o_min = 0.2;
const a = intervalRatio[0];
const b = intervalRatio[1];
if (r < b) {
return (1 - o_min) / (b - a) * (r - a) + o_min;
}
else {
return 1;
}
}
}