@jbrowse/plugin-wiggle
Version:
JBrowse 2 wiggle adapters, tracks, etc.
97 lines (96 loc) • 2.62 kB
JavaScript
import { scaleLinear, scaleLog, scaleQuantize, } from '@mui/x-charts-vendor/d3-scale';
export const YSCALEBAR_LABEL_OFFSET = 5;
export 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 = scaleLinear();
}
else if (scaleType === 'log') {
scale = scaleLog().base(2);
}
else if (scaleType === 'quantize') {
scale = 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;
}
export function getOrigin(scaleType) {
if (scaleType === 'log') {
return 1;
}
return 0;
}
export 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 scaleLinear();
}
if (type === 'log') {
const scale = scaleLog();
scale.base(2);
return scale;
}
if (type === 'quantize') {
return scaleQuantize();
}
throw new Error(`undefined scaleType ${type}`);
};
const scale = getScaleType(scaleType);
scale.domain([min, max]);
scale.nice();
return scale.domain();
}
export function toP(s = 0) {
return +s.toPrecision(6);
}
export function round(value) {
return Math.round(value * 1e5) / 1e5;
}
export 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);
}