UNPKG

@rcsb/rcsb-saguaro

Version:
151 lines (150 loc) 5.8 kB
import { select } from "d3-selection"; import { RcsbD3Constants } from "../RcsbD3Constants"; export class RcsbD3BlockManager { constructor() { this.rectElements = select(RcsbD3Constants.EMPTY); this.lineElements = select(RcsbD3Constants.EMPTY); this.circleElements = select(RcsbD3Constants.EMPTY); this.STROKE_WIDTH = 1; } plot(config) { this.rectElements = config.elements.select(RcsbD3Constants.RECT); this.rectElements .attr(RcsbD3Constants.X, (d, i, e) => { var _a, _b; const begin = (_a = d.rectBegin) !== null && _a !== void 0 ? _a : d.begin; return ((_b = config.xScale(begin - config.dx)) !== null && _b !== void 0 ? _b : 0) + this.STROKE_WIDTH; }) .attr(RcsbD3Constants.Y, config.y_o) .attr(RcsbD3Constants.WIDTH, (d) => { var _a, _b; if (d.end != null) { const begin = (_a = d.rectBegin) !== null && _a !== void 0 ? _a : d.begin; const end = (_b = d.rectEnd) !== null && _b !== void 0 ? _b : d.end; return (RcsbD3BlockManager.getMinWidth(begin, end, config.xScale, config.dx) - this.STROKE_WIDTH); } else return RcsbD3BlockManager.minWidth; }) .attr(RcsbD3Constants.HEIGHT, config.dy) .attr(RcsbD3Constants.FILL, (d) => { if (d.color === undefined) { return config.color; } else { return d.color; } }) .attr(RcsbD3Constants.FILL_OPACITY, 0.5) .attr(RcsbD3Constants.STROKE, (d) => { if (d.color === undefined) { return config.color; } else { return d.color; } }) .attr(RcsbD3Constants.STROKE_OPACITY, 1) .attr(RcsbD3Constants.STROKE_WIDTH, this.STROKE_WIDTH); } plotDecorators(circles, lines) { this.plotLine(lines); this.plotCircles(circles); } move(config) { this.moveBlock(config.xScale, config.dx); this.moveLine(config.xScale, config.dx); this.moveCircle(config.xScale, config.dx); } static getMinWidth(begin, end, xScale, dx) { var _a, _b; let w = (((_a = xScale(end + dx)) !== null && _a !== void 0 ? _a : 0) - ((_b = xScale(begin - dx)) !== null && _b !== void 0 ? _b : 0)); if (w < this.minWidth) { w = this.minWidth; } return w; } plotCircles(config) { this.circleElements = config.elements.select(RcsbD3Constants.CIRCLE); this.circleElements.attr(RcsbD3Constants.CX, (d) => { var _a; return (_a = config.xScale(d.position + d.shift * config.dx)) !== null && _a !== void 0 ? _a : 0; }) .attr(RcsbD3Constants.CY, 0.5 * config.height) .attr(RcsbD3Constants.R, config.dy / 4) .attr(RcsbD3Constants.FILL, "#ffffff") .attr(RcsbD3Constants.STROKE, (d) => { if (d.color === undefined) { return config.color; } else { return d.color; } }) .attr(RcsbD3Constants.STROKE_WIDTH, this.STROKE_WIDTH); } plotLine(config) { this.lineElements = config.elements.select(RcsbD3Constants.LINE); this.lineElements .attr(RcsbD3Constants.X1, (d) => { var _a; return (_a = config.xScale(d.begin + config.dx)) !== null && _a !== void 0 ? _a : 0; }) .attr(RcsbD3Constants.Y1, (d) => { return config.height * 0.5; }) .attr(RcsbD3Constants.X2, (d) => { var _a; return (_a = config.xScale(d.end - config.dx)) !== null && _a !== void 0 ? _a : 0; }) .attr(RcsbD3Constants.Y2, (d) => { return config.height * 0.5; }) .attr(RcsbD3Constants.STROKE_WIDTH, this.STROKE_WIDTH) .attr(RcsbD3Constants.STROKE_DASH, 4) .attr(RcsbD3Constants.STROKE, (d) => { if (d.color === undefined) { return config.color; } else { return d.color; } }); } moveBlock(xScale, dx) { this.rectElements.attr(RcsbD3Constants.X, (d) => { var _a, _b; const begin = (_a = d.rectBegin) !== null && _a !== void 0 ? _a : d.begin; return ((_b = xScale(begin - dx)) !== null && _b !== void 0 ? _b : 0) + this.STROKE_WIDTH; }) .attr(RcsbD3Constants.WIDTH, (d) => { var _a, _b; if (d.end != null) { const begin = (_a = d.rectBegin) !== null && _a !== void 0 ? _a : d.begin; const end = (_b = d.rectEnd) !== null && _b !== void 0 ? _b : d.end; return (RcsbD3BlockManager.getMinWidth(begin, end, xScale, dx) - this.STROKE_WIDTH); } else { return RcsbD3BlockManager.minWidth; } }); } moveLine(xScale, dx) { this.lineElements.attr(RcsbD3Constants.X1, (d) => { var _a; return (_a = xScale(d.begin + dx)) !== null && _a !== void 0 ? _a : 0; }) .attr(RcsbD3Constants.X2, (d) => { var _a; return (_a = xScale(d.end - dx)) !== null && _a !== void 0 ? _a : 0; }); } ; moveCircle(xScale, dx) { this.circleElements.attr(RcsbD3Constants.CX, (d) => { var _a; return (_a = xScale(d.position + d.shift * dx)) !== null && _a !== void 0 ? _a : 0; }); } } RcsbD3BlockManager.minWidth = 2;