UNPKG

@jbrowse/plugin-wiggle

Version:

JBrowse 2 wiggle adapters, tracks, etc.

106 lines (105 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YSCALEBAR_LABEL_OFFSET = void 0; exports.getScale = getScale; exports.getOrigin = getOrigin; exports.getNiceDomain = getNiceDomain; exports.toP = toP; exports.round = round; exports.fillRectCtx = fillRectCtx; const d3_scale_1 = require("@mui/x-charts-vendor/d3-scale"); exports.YSCALEBAR_LABEL_OFFSET = 5; function getScale({ domain = [], range = [], scaleType, pivotValue, inverted, }) { let scale; const [min, max] = domain; if (min === undefined || max === undefined) { throw new Error('invalid domain'); } if (scaleType === 'linear') { scale = (0, d3_scale_1.scaleLinear)(); } else if (scaleType === 'log') { scale = (0, d3_scale_1.scaleLog)().base(2); } else if (scaleType === 'quantize') { scale = (0, d3_scale_1.scaleQuantize)(); } else { throw new Error('undefined scaleType'); } scale.domain(pivotValue !== undefined ? [min, pivotValue, max] : [min, max]); scale.nice(); const [rangeMin, rangeMax] = range; if (rangeMin === undefined || rangeMax === undefined) { throw new Error('invalid range'); } scale.range(inverted ? range.slice().reverse() : range); return scale; } function getOrigin(scaleType) { if (scaleType === 'log') { return 1; } return 0; } function getNiceDomain({ scaleType, domain, bounds, }) { const [minScore, maxScore] = bounds; let [min, max] = domain; if (scaleType === 'linear') { if (max < 0) { max = 0; } if (min > 0) { min = 0; } } if (scaleType === 'log') { if (min >= 0 && max > 1) { min = 1; } } if (minScore !== undefined && minScore !== Number.MIN_VALUE) { min = minScore; } if (maxScore !== undefined && maxScore !== Number.MAX_VALUE) { max = maxScore; } const getScaleType = (type) => { if (type === 'linear') { return (0, d3_scale_1.scaleLinear)(); } if (type === 'log') { const scale = (0, d3_scale_1.scaleLog)(); scale.base(2); return scale; } if (type === 'quantize') { return (0, d3_scale_1.scaleQuantize)(); } throw new Error(`undefined scaleType ${type}`); }; const scale = getScaleType(scaleType); scale.domain([min, max]); scale.nice(); return scale.domain(); } function toP(s = 0) { return +s.toPrecision(6); } function round(value) { return Math.round(value * 1e5) / 1e5; } function fillRectCtx(x, y, width, height, ctx, color) { if (width < 0) { x += width; width = -width; } if (height < 0) { y += height; height = -height; } if (color) { ctx.fillStyle = color; } ctx.fillRect(x, y, width, height); }