Skip to content

Commit

Permalink
invertible scales
Browse files Browse the repository at this point in the history
  • Loading branch information
leeoniya committed Dec 23, 2020
1 parent d74bad7 commit 6ab82eb
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 157 deletions.
100 changes: 61 additions & 39 deletions dist/uPlot.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,10 +1315,13 @@ var xScaleOpts = {
log: 10,
min: null,
max: null,
dir: 1,
ori: 0,
};

var yScaleOpts = assign({}, xScaleOpts, {
time: false,
ori: 1,
});

var syncs = {};
Expand All @@ -1345,6 +1348,10 @@ function _sync(opts) {

var props = Array(11);

function _valToPos(u, sc) {
return sc.ori == 0 ? u.valToPosH : u.valToPosV;
}

function aliasProps(u, seriesIdx) {
var series = u.series[seriesIdx];
var scales = u.scales;
Expand All @@ -1355,8 +1362,8 @@ function aliasProps(u, seriesIdx) {
props[2] = u._data[seriesIdx]; // dataY
props[3] = scales[u.series[0].scale]; // scaleX
props[4] = scales[series.scale]; // scaleY
props[5] = u.valToPosX; // valToPosX
props[6] = u.valToPosY; // valToPosY
props[5] = _valToPos(u, props[3]); // valToPosX
props[6] = _valToPos(u, props[4]); // valToPosY
props[7] = bbox.left; // plotLft
props[8] = bbox.top; // plotTop
props[9] = bbox.width; // plotWid
Expand Down Expand Up @@ -1426,6 +1433,8 @@ function linear() {

var isGap = series.isGap;

var _dir = dir * scaleX.dir;

var _paths = dir == 1 ? {stroke: new Path2D(), fill: null, clip: null} : u.series[seriesIdx - 1]._paths;
var stroke = _paths.stroke;
var width = roundDec(series.width * pxRatio, 3);
Expand All @@ -1437,23 +1446,23 @@ function linear() {
// todo: don't build gaps on dir = -1 pass
var gaps = [];

var accX = round(valToPosX(dataX[dir == 1 ? idx0 : idx1], scaleX, plotWid, plotLft));
var accX = round(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, plotWid, plotLft));
var accGaps = false;

// data edges
var lftIdx = nonNullIdx(dataY, idx0, idx1, 1);
var rgtIdx = nonNullIdx(dataY, idx0, idx1, -1);
var lftIdx = nonNullIdx(dataY, idx0, idx1, 1 * scaleX.dir);
var rgtIdx = nonNullIdx(dataY, idx0, idx1, -1 * scaleX.dir);
var lftX = incrRound(valToPosX(dataX[lftIdx], scaleX, plotWid, plotLft), 0.5);
var rgtX = incrRound(valToPosX(dataX[rgtIdx], scaleX, plotWid, plotLft), 0.5);

if (lftX > plotLft)
{ addGap(gaps, plotLft, lftX); }

// the moves the shape edge outside the canvas so stroke doesnt bleed in
if (series.band && dir == 1)
if (series.band && _dir == 1)
{ stroke.lineTo(lftX - width * 2, round(valToPosY(dataY[idx0], scaleY, plotHgt, plotTop))); }

for (var i = dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += dir) {
for (var i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
var x = round(valToPosX(dataX[i], scaleX, plotWid, plotLft));

if (x == accX) {
Expand Down Expand Up @@ -1487,7 +1496,7 @@ function linear() {
minY = maxY = outY;

// prior pixel can have data but still start a gap if ends with null
if (x - accX > 1 && dataY[i - 1] == null && isGap(u, seriesIdx, i - 1))
if (x - accX > 1 && dataY[i - _dir] == null && isGap(u, seriesIdx, i - _dir))
{ _addGap = true; }
}
else {
Expand All @@ -1514,7 +1523,7 @@ function linear() {
var _x, _iy, _data = u._data, dataY2;

// the moves the shape edge outside the canvas so stroke doesnt bleed in
if (dir == 1) {
if (_dir == 1) {
_x = rgtX + width * 2;
_iy = rgtIdx;
dataY2 = _data[seriesIdx + 1];
Expand Down Expand Up @@ -1564,6 +1573,8 @@ function spline(opts) {
var plotWid = ref[9];
var plotHgt = ref[10];

var _dir = 1 * scaleX.dir;

idx0 = nonNullIdx(dataY, idx0, idx1, 1);
idx1 = nonNullIdx(dataY, idx0, idx1, -1);

Expand All @@ -1575,7 +1586,7 @@ function spline(opts) {
var xCoords = [];
var yCoords = [];

for (var i = idx0; i <= idx1; i++) {
for (var i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
var yVal = dataY[i];
var xVal = dataX[i];
var xPos = valToPosX(xVal, scaleX, plotWid, plotLft);
Expand Down Expand Up @@ -1760,18 +1771,20 @@ function stepped(opts) {

var stroke = new Path2D();

var _dir = 1 * scaleX.dir;

idx0 = nonNullIdx(dataY, idx0, idx1, 1);
idx1 = nonNullIdx(dataY, idx0, idx1, -1);

var gaps = [];
var inGap = false;
var prevYPos = round(valToPosY(dataY[idx0], scaleY, plotHgt, plotTop));
var firstXPos = round(valToPosX(dataX[idx0], scaleX, plotWid, plotLft));
var prevYPos = round(valToPosY(dataY[_dir == 1 ? idx0 : idx1], scaleY, plotHgt, plotTop));
var firstXPos = round(valToPosX(dataX[_dir == 1 ? idx0 : idx1], scaleX, plotWid, plotLft));
var prevXPos = firstXPos;

stroke.moveTo(firstXPos, prevYPos);

for (var i = idx0 + 1; i <= idx1; i++) {
for (var i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
var yVal1 = dataY[i];

var x1 = round(valToPosX(dataX[i], scaleX, plotWid, plotLft));
Expand Down Expand Up @@ -1866,7 +1879,9 @@ function bars(opts) {

var stroke = new Path2D();

for (var i = idx0; i <= idx1; i++) {
var _dir = 1 * scaleX.dir;

for (var i = _dir == 1 ? idx0 : idx1; i >= idx0 && i <= idx1; i += _dir) {
var yVal = dataY[i];

if (yVal == null)
Expand Down Expand Up @@ -1961,18 +1976,23 @@ function uPlot(opts, data, then) {
);
}

function getXPos(val, scale, wid, lft) {
var pctX = getValPct(val, scale);
return lft + pctX * wid;
function getHPos(val, scale, dim, off) {
var pct = getValPct(val, scale);
return off + dim * (scale.dir == -1 ? (1 - pct) : pct);
}

function getYPos(val, scale, hgt, top) {
var pctY = getValPct(val, scale);
return top + (1 - pctY) * hgt;
function getVPos(val, scale, dim, off) {
var pct = getValPct(val, scale);
return off + dim * (scale.dir == -1 ? pct : (1 - pct));
}

self.valToPosX = getXPos;
self.valToPosY = getYPos;
function getPos(val, scale, dim, off) {
return scale.ori == 0 ? getHPos(val, scale, dim, off) : getVPos(val, scale, dim, off);
}

self.valToPosH = getHPos;
self.valToPosV = getVPos;
self._valToPos = getPos;

var ready = false;
self.status = 0;
Expand Down Expand Up @@ -2798,8 +2818,8 @@ function uPlot(opts, data, then) {

for (var pi = i0; pi <= i1; pi++) {
if (data[si][pi] != null) {
var x = round(getXPos(data[0][pi], scales[xScaleKey], plotWid, plotLft));
var y = round(getYPos(data[si][pi], scales[s.scale], plotHgt, plotTop));
var x = round(getPos(data[0][pi], scales[xScaleKey], plotWid, plotLft));
var y = round(getPos(data[si][pi], scales[s.scale], plotHgt, plotTop));

path.moveTo(x + rad, y);
path.arc(x, y, rad, 0, PI * 2);
Expand Down Expand Up @@ -3076,7 +3096,6 @@ function uPlot(opts, data, then) {
var side = axis.side;
var ori = side % 2;

var getPos = ori == 0 ? getXPos : getYPos;
var plotDim = ori == 0 ? plotWid : plotHgt;
var plotOff = ori == 0 ? plotLft : plotTop;

Expand Down Expand Up @@ -3512,12 +3531,15 @@ function uPlot(opts, data, then) {
pos = dim - pos;
}

var pct = pos / dim;

var sc = scales[scale],
_min = sc.min,
_max = sc.max;

if (sc.dir == -1)
{ pos = dim - pos; }

var pct = pos / dim;

if (sc.distr == 3) {
_min = log10(_min);
_max = log10(_max);
Expand All @@ -3536,12 +3558,12 @@ function uPlot(opts, data, then) {
self.posToIdx = closestIdxFromXpos;
self.posToVal = scaleValueAtPos;
self.valToPos = function (val, scale, can) { return (
scale == xScaleKey ?
getXPos(val, scales[scale],
scale.ori == 0 ?
getHPos(val, scales[scale],
can ? plotWid : plotWidCss,
can ? plotLft : 0
) :
getYPos(val, scales[scale],
getVPos(val, scales[scale],
can ? plotHgt : plotHgtCss,
can ? plotTop : 0
)
Expand Down Expand Up @@ -3615,18 +3637,18 @@ function uPlot(opts, data, then) {

var scX = scales[xScaleKey];

var xPos = incrRoundUp(getXPos(data[0][idx], scX, plotWidCss, 0), 0.5);
var xPos = incrRoundUp(getPos(data[0][idx], scX, plotWidCss, 0), 0.5);

for (var i$1 = 0; i$1 < series.length; i$1++) {
var s = series[i$1];

var idx2 = cursor.dataIdx(self, i$1, idx, valAtPos);
var xPos2 = idx2 == idx ? xPos : incrRoundUp(getXPos(data[0][idx2], scX, plotWidCss, 0), 0.5);
var xPos2 = idx2 == idx ? xPos : incrRoundUp(getPos(data[0][idx2], scX, plotWidCss, 0), 0.5);

if (i$1 > 0 && s.show) {
var valAtIdx = data[i$1][idx2];

var yPos = valAtIdx == null ? -10 : incrRoundUp(getYPos(valAtIdx, scales[s.scale], plotHgtCss, 0), 0.5);
var yPos = valAtIdx == null ? -10 : incrRoundUp(getPos(valAtIdx, scales[s.scale], plotHgtCss, 0), 0.5);

if (yPos > 0) {
var dist = abs(yPos - mouseTop1);
Expand Down Expand Up @@ -3675,8 +3697,8 @@ function uPlot(opts, data, then) {
var srcLeft = src.posToVal(src.select.left, xKey);
var srcRight = src.posToVal(src.select.left + src.select.width, xKey);

select.left = getXPos(srcLeft, sc, plotWidCss, 0);
select.width = abs(select.left - getXPos(srcRight, sc, plotWidCss, 0));
select.left = getPos(srcLeft, sc, plotWidCss, 0);
select.width = abs(select.left - getPos(srcRight, sc, plotWidCss, 0));

setStylePx(selectDiv, LEFT, select.left);
setStylePx(selectDiv, WIDTH, select.width);
Expand All @@ -3692,8 +3714,8 @@ function uPlot(opts, data, then) {
var srcTop = src.posToVal(src.select.top, yKey);
var srcBottom = src.posToVal(src.select.top + src.select.height, yKey);

select.top = getYPos(srcTop, sc$1, plotHgtCss, 0);
select.height = abs(select.top - getYPos(srcBottom, sc$1, plotHgtCss, 0));
select.top = getPos(srcTop, sc$1, plotHgtCss, 0);
select.height = abs(select.top - getPos(srcBottom, sc$1, plotHgtCss, 0));

setStylePx(selectDiv, TOP, select.top);
setStylePx(selectDiv, HEIGHT, select.height);
Expand Down Expand Up @@ -3835,12 +3857,12 @@ function uPlot(opts, data, then) {
var yKey = ref[1];

if (xKey != null)
{ _x = getXPos(src.posToVal(_x, xKey), scales[xKey], plotWidCss, 0); }
{ _x = getPos(src.posToVal(_x, xKey), scales[xKey], plotWidCss, 0); }
else
{ _x = plotWidCss * (_x/_w); }

if (yKey != null)
{ _y = getYPos(src.posToVal(_y, yKey), scales[yKey], plotHgtCss, 0); }
{ _y = getPos(src.posToVal(_y, yKey), scales[yKey], plotHgtCss, 0); }
else
{ _y = plotHgtCss * (_y/_h); }
}
Expand Down
6 changes: 6 additions & 0 deletions dist/uPlot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ export interface Scale {

/** current max scale value */
max?: number,

/** scale direction */
dir?: 1 | -1;

/** scale orientation - 0: hz, 1: vt */
ori?: 0 | 1;
}

export namespace Series {
Expand Down

0 comments on commit 6ab82eb

Please sign in to comment.