settingCursor from external function
Hi, first thx for the cool plot routine.
I am using it at the moment and had to add 2 functionalities to achieve additional functions:
- get the cursor set at a specific X value from outside
/** * added sets a marker at the given xValue. If the point is outside of the viewable area the view jumps with the same zoom to the next area which has this point included. **/ function goToXPoint(xValue) {
if (self.scales != null)
{
//it is assumed that axes zero is the x axis
let xScale = self.scales[xScaleKey];
let xSpan = xScale._max - xScale._min;
let min = xValue - xSpan / 2;
let max = xValue + xSpan / 2;
if(xValue > scales.x.max)
{
min = xScale.max;
max = xScale.max + xSpan;
_setScale(xScaleKey, min, max);
}
else if(xValue < xScale.min)
{
min = xScale.min - xSpan;
max = xScale.min ;
_setScale(xScaleKey, min, max);
}
else
{
setScale('x', scales.x.min, scales.x.max);
}
setCursorAtPoint(xValue);
}
}
function setCursorAtPoint(xValue)
{
let xDim = scaleX.ori == 0 ? plotWidCss : plotHgtCss;
let yDim = scaleX.ori == 1 ? plotWidCss : plotHgtCss;
let idx = closestIdx(xValue, data[0], i0, i1);
let xPos = valToPosX(xValue, scaleX, xDim, 0);
for (let i = mode == 2 ? 1 : 0; i < series.length; i++) {
let s = series[i];
let idx1 = activeIdxs[i];
let yVal1 = mode == 1 ? data[i][idx] : data[i][1][idx1];
let idx2 = cursor.dataIdx(self, i, idx, xValue);
let yVal2 = mode == 1 ? data[i][idx] : data[i][1][idx2];
shouldSetLegend = shouldSetLegend || yVal2 != yVal1 || idx2 != idx1;
activeIdxs[i] = idx2;
let xPos2 = idx2 == idx ? xPos : incrRoundUp(valToPosX(mode == 1 ? data[0][idx2] : data[i][0][idx2], scaleX, xDim, 0), 0.5);
if (i > 0 && s.show) {
let yPos = yVal2 == null ? -10 : incrRoundUp(valToPosY(yVal2, mode == 1 ? scales[s.scale] : scales[s.facets[1].scale], yDim, 0), 0.5);
if (yPos > 0 && mode == 1) {
let dist = abs(yPos - mouseTop1);
if (dist <= closestDist) {
closestDist = dist;
closestSeries = i;
}
}
let hPos, vPos;
if (scaleX.ori == 0) {
hPos = xPos2;
vPos = yPos;
} else {
hPos = yPos;
vPos = xPos2;
}
if (shouldSetLegend && cursorPts.length > 1) {
elColor(cursorPts[i], cursor.points.fill(self, i), cursor.points.stroke(self, i));
let ptWid, ptHgt, ptLft, ptTop,
centered = true,
getBBox = cursor.points.bbox;
if (getBBox != null) {
centered = false;
let bbox = getBBox(self, i);
ptLft = bbox.left;
ptTop = bbox.top;
ptWid = bbox.width;
ptHgt = bbox.height;
} else {
ptLft = hPos;
ptTop = vPos;
ptWid = ptHgt = cursor.points.size(self, i);
}
elSize(cursorPts[i], ptWid, ptHgt, centered);
elTrans(cursorPts[i], ptLft, ptTop, plotWidCss, plotHgtCss);
}
}
if (legend.live) {
if (!shouldSetLegend || i == 0 && multiValLegend)
continue;
setLegendValues(i, idx2);
}
}
}
self.goToXPoint = goToXPoint;
"
and
- to call an external function when a value in the plot is clicked
ADDED-code in function "function uPlot(opts, data, then) {"
self.pointselect = null;
if (opts.pointselect)
{
self.pointselect = opts.pointselect;
}
ADDED-code in "function mouseUp(e, src, _l, _t, _w, _h, _i) {" function mouseUp(e, src, _l, _t, _w, _h, _i) { dragging = drag._x = drag._y = false;
cacheMouse(e, src, _l, _t, _w, _h, _i, false, true);
let {left, top, width, height} = select;
let hasSelect = width > 0 || height > 0;
hasSelect && setSelect(select);
if (drag.setScale && hasSelect) {
// if (syncKey != null) {
// dragX = drag.x;
// dragY = drag.y;
// }
let xOff = left,
xDim = width,
yOff = top,
yDim = height;
if (scaleX.ori == 1) {
xOff = top,
xDim = height,
yOff = left,
yDim = width;
}
if (dragX) {
_setScale(xScaleKey,
posToVal(xOff, xScaleKey),
posToVal(xOff + xDim, xScaleKey)
);
}
if (dragY) {
for (let k in scales) {
let sc = scales[k];
if (k != xScaleKey && sc.from == null && sc.min != inf) {
_setScale(k,
posToVal(yOff + yDim, k),
posToVal(yOff, k)
);
}
}
}
hideSelect();
} else if (cursor.lock) {
cursor._lock = !cursor._lock;
if (!cursor._lock)
updateCursor(null, true, false);
}
//-------------ADDDED-START------
else
{
if (self.pointselect != null)
{
//cacheMouse(e, src, _l, _t, _w, _h, _i, false, true);
if (e !== null)
{
let valAtPosX = posToVal(e.offsetX, xScaleKey);
let idx = closestIdx(valAtPosX, data[0], i0, i1);
let value = data[0][idx];
self.pointselect(value);
}
}
}
//------------ADDED-END----------------------
if (e != null) {
offMouse(mouseup, doc);
pubSync(mouseup, self, mouseLeft1, mouseTop1, plotWidCss, plotHgtCss, null);
}
}
Is this already possible and did i add this without reason? When this is not the case, would you be so kind to add this in your way for a next release. Otherwise i have to add it always with a new release :)
Best Regards Josef
it's difficult for me to understand the exact desired effect from reading non-runnable code.
can you attach a screen recording that shows the new/needed functionality?
Hi thx for the quick response,
Yes i added a picture. You See here on the left side (spatial plot) a normal plot with position info [meter] on x and a value on y axis. If i click now on a point in this plot on the right side (temporal plot) this single point is displayed over time, how it changes. Over the right plot you also see two buttons in (red encircled in picture). They are responsible to move the cursor/marker in the spatial plot a single step forward or backward to see the new selected point in the temporal plot.
For this i would need the possibility to set the cursor position via a function call in the uplot. I hop i could give you enough info.
Cheers Josef
https://github.com/leeoniya/uPlot/assets/10980847/287fe495-310c-43b2-906d-8de5da3c3de6
a07c72)
uPlot has a u.setCursor() API that accepts top and left CSS coordinates:
https://github.com/leeoniya/uPlot/blob/08dd61d74267156e1361b3523541508cc1a125f5/dist/uPlot.d.ts#L76-L77
to get the coordinates of a point in the data, you should get the values from the needed index and do something like:
// data
let data = [
[0,1,2,3,4,5], // x values
[5,9,5,9,5,9], // y values
];
let idx = 5;
u.setCursor({
left: u.valToPos(u.data[0][idx], 'x'),
top: u.valToPos(u.data[1][idx], 'y'),
});
hey any idea like how to achieve this in React.js