From 57adbc3bd63e3b0df0a9effe4dfd77237393c291 Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Tue, 9 Nov 2021 18:17:10 -0600 Subject: [PATCH] Market trend: fix bar width calc when time range exceeds data range (#41493) --- .../app/plugins/panel/market-trend/utils.ts | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/panel/market-trend/utils.ts b/public/app/plugins/panel/market-trend/utils.ts index 4693e0de38c..16ca6df16c0 100644 --- a/public/app/plugins/panel/market-trend/utils.ts +++ b/public/app/plugins/panel/market-trend/utils.ts @@ -71,7 +71,33 @@ export function drawMarkers(opts: RendererOpts) { let [idx0, idx1] = u.series[0].idxs!; - let colWidth = u.bbox.width / (idx1 - idx0); + let dataX = u.data[0]; + let dataY = oData; + + let colWidth = u.bbox.width; + + if (dataX.length > 1) { + // prior index with non-undefined y data + let prevIdx = null; + + // scan full dataset for smallest adjacent delta + // will not work properly for non-linear x scales, since does not do expensive valToPosX calcs till end + for (let i = 0, minDelta = Infinity; i < dataX.length; i++) { + if (dataY[i] !== undefined) { + if (prevIdx != null) { + let delta = Math.abs(dataX[i] - dataX[prevIdx]); + + if (delta < minDelta) { + minDelta = delta; + colWidth = Math.abs(u.valToPos(dataX[i], 'x') - u.valToPos(dataX[prevIdx], 'x')); + } + } + + prevIdx = i; + } + } + } + let barWidth = Math.round(0.6 * colWidth); let stickWidth = 2;