2021-05-11 12:24:23 -05:00
|
|
|
import uPlot, { Axis, Series } from 'uplot';
|
2021-01-29 14:52:52 -06:00
|
|
|
import { Quadtree, Rect, pointWithin } from './quadtree';
|
|
|
|
import { distribute, SPACE_BETWEEN } from './distribute';
|
2021-05-11 13:57:52 -05:00
|
|
|
import { TooltipInterpolator } from '@grafana/ui/src/components/uPlot/types';
|
|
|
|
import { ScaleDirection, ScaleOrientation } from '@grafana/ui/src/components/uPlot/config';
|
2021-01-29 14:52:52 -06:00
|
|
|
|
|
|
|
const groupDistr = SPACE_BETWEEN;
|
|
|
|
const barDistr = SPACE_BETWEEN;
|
2021-05-11 12:24:23 -05:00
|
|
|
const font = Math.round(10 * devicePixelRatio) + 'px Arial';
|
2021-01-29 14:52:52 -06:00
|
|
|
|
|
|
|
type WalkTwoCb = null | ((idx: number, offPx: number, dimPx: number) => void);
|
|
|
|
|
|
|
|
function walkTwo(
|
|
|
|
groupWidth: number,
|
|
|
|
barWidth: number,
|
|
|
|
yIdx: number,
|
|
|
|
xCount: number,
|
|
|
|
yCount: number,
|
|
|
|
xDim: number,
|
|
|
|
xDraw?: WalkTwoCb,
|
|
|
|
yDraw?: WalkTwoCb
|
|
|
|
) {
|
|
|
|
distribute(xCount, groupWidth, groupDistr, null, (ix, offPct, dimPct) => {
|
|
|
|
let groupOffPx = xDim * offPct;
|
|
|
|
let groupWidPx = xDim * dimPct;
|
|
|
|
|
|
|
|
xDraw && xDraw(ix, groupOffPx, groupWidPx);
|
|
|
|
|
|
|
|
yDraw &&
|
|
|
|
distribute(yCount, barWidth, barDistr, yIdx, (iy, offPct, dimPct) => {
|
|
|
|
let barOffPx = groupWidPx * offPct;
|
|
|
|
let barWidPx = groupWidPx * dimPct;
|
|
|
|
|
|
|
|
yDraw(ix, groupOffPx + barOffPx, barWidPx);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export interface BarsOptions {
|
2021-05-11 12:24:23 -05:00
|
|
|
xOri: ScaleOrientation;
|
|
|
|
xDir: ScaleDirection;
|
2021-01-29 14:52:52 -06:00
|
|
|
groupWidth: number;
|
|
|
|
barWidth: number;
|
|
|
|
formatValue?: (seriesIdx: number, value: any) => string;
|
2021-04-06 18:06:46 -05:00
|
|
|
onHover?: (seriesIdx: number, valueIdx: number) => void;
|
|
|
|
onLeave?: (seriesIdx: number, valueIdx: number) => void;
|
2021-01-29 14:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function getConfig(opts: BarsOptions) {
|
2021-05-11 12:24:23 -05:00
|
|
|
const { xOri: ori, xDir: dir, groupWidth, barWidth, formatValue } = opts;
|
2021-01-29 14:52:52 -06:00
|
|
|
|
|
|
|
let qt: Quadtree;
|
|
|
|
|
2021-05-11 12:24:23 -05:00
|
|
|
const drawBars: Series.PathBuilder = (u, sidx) => {
|
2021-01-29 14:52:52 -06:00
|
|
|
return uPlot.orient(
|
|
|
|
u,
|
|
|
|
sidx,
|
|
|
|
(series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim, moveTo, lineTo, rect) => {
|
|
|
|
const fill = new Path2D();
|
2021-02-02 11:49:45 -06:00
|
|
|
const stroke = new Path2D();
|
2021-01-29 14:52:52 -06:00
|
|
|
|
|
|
|
let numGroups = dataX.length;
|
|
|
|
let barsPerGroup = u.series.length - 1;
|
|
|
|
|
|
|
|
let y0Pos = valToPosY(0, scaleY, yDim, yOff);
|
|
|
|
|
|
|
|
const _dir = dir * (ori === 0 ? 1 : -1);
|
|
|
|
|
|
|
|
walkTwo(groupWidth, barWidth, sidx - 1, numGroups, barsPerGroup, xDim, null, (ix, x0, wid) => {
|
|
|
|
let lft = Math.round(xOff + (_dir === 1 ? x0 : xDim - x0 - wid));
|
|
|
|
let barWid = Math.round(wid);
|
|
|
|
|
|
|
|
if (dataY[ix] != null) {
|
|
|
|
let yPos = valToPosY(dataY[ix]!, scaleY, yDim, yOff);
|
|
|
|
|
|
|
|
let btm = Math.round(Math.max(yPos, y0Pos));
|
|
|
|
let top = Math.round(Math.min(yPos, y0Pos));
|
|
|
|
let barHgt = btm - top;
|
|
|
|
|
2021-02-02 11:49:45 -06:00
|
|
|
let strokeWidth = series.width || 0;
|
|
|
|
|
|
|
|
if (strokeWidth) {
|
|
|
|
rect(stroke, lft + strokeWidth / 2, top + strokeWidth / 2, barWid - strokeWidth, barHgt - strokeWidth);
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:52:52 -06:00
|
|
|
rect(fill, lft, top, barWid, barHgt);
|
|
|
|
|
|
|
|
let x = ori === 0 ? Math.round(lft - xOff) : Math.round(top - yOff);
|
|
|
|
let y = ori === 0 ? Math.round(top - yOff) : Math.round(lft - xOff);
|
|
|
|
let w = ori === 0 ? barWid : barHgt;
|
|
|
|
let h = ori === 0 ? barHgt : barWid;
|
|
|
|
|
|
|
|
qt.add({ x, y, w, h, sidx: sidx, didx: ix });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2021-02-02 11:49:45 -06:00
|
|
|
stroke,
|
2021-01-29 14:52:52 -06:00
|
|
|
fill,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const drawPoints: Series.Points.Show =
|
|
|
|
formatValue == null
|
|
|
|
? false
|
2021-05-11 12:24:23 -05:00
|
|
|
: (u, sidx) => {
|
2021-01-29 14:52:52 -06:00
|
|
|
u.ctx.font = font;
|
|
|
|
u.ctx.fillStyle = 'white';
|
|
|
|
|
|
|
|
uPlot.orient(
|
|
|
|
u,
|
|
|
|
sidx,
|
2021-05-11 12:24:23 -05:00
|
|
|
(series, dataX, dataY, scaleX, scaleY, valToPosX, valToPosY, xOff, yOff, xDim, yDim) => {
|
2021-01-29 14:52:52 -06:00
|
|
|
let numGroups = dataX.length;
|
|
|
|
let barsPerGroup = u.series.length - 1;
|
|
|
|
|
|
|
|
const _dir = dir * (ori === 0 ? 1 : -1);
|
|
|
|
|
|
|
|
walkTwo(groupWidth, barWidth, sidx - 1, numGroups, barsPerGroup, xDim, null, (ix, x0, wid) => {
|
|
|
|
let lft = Math.round(xOff + (_dir === 1 ? x0 : xDim - x0 - wid));
|
|
|
|
let barWid = Math.round(wid);
|
|
|
|
|
|
|
|
// prettier-ignore
|
|
|
|
if (dataY[ix] != null) {
|
|
|
|
let yPos = valToPosY(dataY[ix]!, scaleY, yDim, yOff);
|
|
|
|
|
|
|
|
let x = ori === 0 ? Math.round(lft + barWid / 2) : Math.round(yPos);
|
|
|
|
let y = ori === 0 ? Math.round(yPos) : Math.round(lft + barWid / 2);
|
|
|
|
|
|
|
|
u.ctx.textAlign = ori === 0 ? 'center' : dataY[ix]! >= 0 ? 'left' : 'right';
|
|
|
|
u.ctx.textBaseline = ori === 1 ? 'middle' : dataY[ix]! >= 0 ? 'bottom' : 'top';
|
|
|
|
|
|
|
|
u.ctx.fillText(formatValue(sidx, dataY[ix]), x, y);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-05-11 12:24:23 -05:00
|
|
|
const xSplits: Axis.Splits = (u: uPlot) => {
|
2021-01-29 14:52:52 -06:00
|
|
|
const dim = ori === 0 ? u.bbox.width : u.bbox.height;
|
|
|
|
const _dir = dir * (ori === 0 ? 1 : -1);
|
|
|
|
|
|
|
|
let splits: number[] = [];
|
|
|
|
|
|
|
|
distribute(u.data[0].length, groupWidth, groupDistr, null, (di, lftPct, widPct) => {
|
2021-05-11 12:24:23 -05:00
|
|
|
let groupLftPx = (dim * lftPct) / devicePixelRatio;
|
|
|
|
let groupWidPx = (dim * widPct) / devicePixelRatio;
|
2021-01-29 14:52:52 -06:00
|
|
|
|
|
|
|
let groupCenterPx = groupLftPx + groupWidPx / 2;
|
|
|
|
|
|
|
|
splits.push(u.posToVal(groupCenterPx, 'x'));
|
|
|
|
});
|
|
|
|
|
|
|
|
return _dir === 1 ? splits : splits.reverse();
|
|
|
|
};
|
|
|
|
|
|
|
|
const xValues: Axis.Values = (u) => u.data[0];
|
|
|
|
|
|
|
|
let hovered: Rect | null = null;
|
|
|
|
|
|
|
|
let barMark = document.createElement('div');
|
|
|
|
barMark.classList.add('bar-mark');
|
|
|
|
barMark.style.position = 'absolute';
|
|
|
|
barMark.style.background = 'rgba(255,255,255,0.4)';
|
|
|
|
|
|
|
|
const init = (u: uPlot) => {
|
|
|
|
let over = u.root.querySelector('.u-over')! as HTMLElement;
|
|
|
|
over.style.overflow = 'hidden';
|
|
|
|
over.appendChild(barMark);
|
|
|
|
};
|
|
|
|
|
|
|
|
const drawClear = (u: uPlot) => {
|
|
|
|
qt = qt || new Quadtree(0, 0, u.bbox.width, u.bbox.height);
|
|
|
|
|
|
|
|
qt.clear();
|
|
|
|
|
|
|
|
// clear the path cache to force drawBars() to rebuild new quadtree
|
|
|
|
u.series.forEach((s) => {
|
|
|
|
// @ts-ignore
|
|
|
|
s._paths = null;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// handle hover interaction with quadtree probing
|
2021-05-11 12:24:23 -05:00
|
|
|
const interpolateBarChartTooltip: TooltipInterpolator = (
|
|
|
|
updateActiveSeriesIdx,
|
|
|
|
updateActiveDatapointIdx,
|
|
|
|
updateTooltipPosition
|
|
|
|
) => {
|
|
|
|
return (u: uPlot) => {
|
|
|
|
let found: Rect | null = null;
|
|
|
|
let cx = u.cursor.left! * devicePixelRatio;
|
|
|
|
let cy = u.cursor.top! * devicePixelRatio;
|
|
|
|
|
|
|
|
qt.get(cx, cy, 1, 1, (o) => {
|
|
|
|
if (pointWithin(cx, cy, o.x, o.y, o.x + o.w, o.y + o.h)) {
|
|
|
|
found = o;
|
|
|
|
}
|
|
|
|
});
|
2021-01-29 14:52:52 -06:00
|
|
|
|
2021-05-11 12:24:23 -05:00
|
|
|
if (found) {
|
|
|
|
// prettier-ignore
|
|
|
|
if (found !== hovered) {
|
|
|
|
barMark.style.display = '';
|
|
|
|
barMark.style.left = found!.x / devicePixelRatio + 'px';
|
|
|
|
barMark.style.top = found!.y / devicePixelRatio + 'px';
|
|
|
|
barMark.style.width = found!.w / devicePixelRatio + 'px';
|
|
|
|
barMark.style.height = found!.h / devicePixelRatio + 'px';
|
|
|
|
hovered = found;
|
|
|
|
updateActiveSeriesIdx(hovered!.sidx);
|
|
|
|
updateActiveDatapointIdx(hovered!.didx);
|
|
|
|
updateTooltipPosition();
|
2021-01-29 14:52:52 -06:00
|
|
|
}
|
2021-05-11 12:24:23 -05:00
|
|
|
} else if (hovered != null) {
|
|
|
|
updateActiveSeriesIdx(hovered!.sidx);
|
|
|
|
updateActiveDatapointIdx(hovered!.didx);
|
|
|
|
updateTooltipPosition();
|
|
|
|
hovered = null;
|
|
|
|
barMark.style.display = 'none';
|
|
|
|
} else {
|
|
|
|
updateTooltipPosition(true);
|
2021-01-29 14:52:52 -06:00
|
|
|
}
|
2021-05-11 12:24:23 -05:00
|
|
|
};
|
2021-01-29 14:52:52 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
// scale & axis opts
|
|
|
|
xValues,
|
|
|
|
xSplits,
|
|
|
|
|
|
|
|
// pathbuilders
|
|
|
|
drawBars,
|
|
|
|
drawPoints,
|
|
|
|
|
|
|
|
// hooks
|
|
|
|
init,
|
|
|
|
drawClear,
|
2021-05-11 12:24:23 -05:00
|
|
|
interpolateBarChartTooltip,
|
2021-01-29 14:52:52 -06:00
|
|
|
};
|
|
|
|
}
|