Skip to content

Commit 6d22631

Browse files
authored
[FEATURE] updates nearby-series to support logarithmic scaled data (#11)
* [FEATURE] updates nearby-series to support logarithmic scaled data This change adds a scaling to the log-base data to convert it to a linear space for nearby series finding. If this is not present and you use a log-based chart, you end up having millions of data points within just a few pixels. --------- Signed-off-by: Simon Olander <simon.olander@sap.com> * Update nearby-series.ts Resolved linting by removing redundant mathjs dep (replaced with ** operator) Signed-off-by: Simon Olander <simon.olander@sap.com> --------- Signed-off-by: Simon Olander <simon.olander@sap.com>
1 parent 29f8802 commit 6d22631

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

components/src/TimeSeriesTooltip/nearby-series.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export function checkforNearbyTimeSeries(
9696

9797
const xValue = nearbyTimeSeries[0];
9898
const yValue = nearbyTimeSeries[1];
99-
10099
// TODO: ensure null values not displayed in tooltip
101100
if (yValue !== undefined && yValue !== null) {
102101
if (closestTimestamp === xValue) {
@@ -336,7 +335,20 @@ export function getNearbySeriesData({
336335
const pointInGrid = getPointInGrid(mousePos.plotCanvas.x, mousePos.plotCanvas.y, chart);
337336
if (pointInGrid !== null) {
338337
const chartModel = chart['_model'];
339-
const yInterval = chartModel.getComponent('yAxis').axis.scale._interval;
338+
const yAxisScale = chartModel.getComponent('yAxis').axis.scale;
339+
const isLogScale = yAxisScale.type === 'log';
340+
let yInterval = yAxisScale._interval;
341+
// For logarithmic scales, convert the log interval to actual data range
342+
if (isLogScale && yAxisScale.base) {
343+
const logBase = yAxisScale.base;
344+
const extent = yAxisScale._extent;
345+
// Calculate actual data range from log extent
346+
// extent is in log space (e.g., [0, 2] for 10^0 to 10^2)
347+
const actualMin = logBase ** extent[0];
348+
const actualMax = logBase ** extent[1];
349+
// Use a fraction of the actual range as the interval
350+
yInterval = (actualMax - actualMin) / 100;
351+
}
340352
const totalSeries = data.length;
341353
const yBuffer = getYBuffer({ yInterval, totalSeries, showAllSeries });
342354
return checkforNearbyTimeSeries(data, seriesMapping, pointInGrid, yBuffer, chart, format);
@@ -439,7 +451,6 @@ export function getYBuffer({
439451
if (showAllSeries) {
440452
return yInterval * 10; // roughly correlates with grid so entire canvas is searched
441453
}
442-
443454
// never let nearby series range be less than roughly the size of a single tick
444455
const yBufferMin = yInterval * 0.3;
445456

@@ -448,7 +459,6 @@ export function getYBuffer({
448459
const adjustedBuffer = (yInterval * DYNAMIC_NEARBY_SERIES_MULTIPLIER) / totalSeries;
449460
return Math.max(yBufferMin, adjustedBuffer);
450461
}
451-
452462
// increase multiplier to expand nearby series range
453463
return Math.max(yBufferMin, yInterval * INCREASE_NEARBY_SERIES_MULTIPLIER);
454464
}

0 commit comments

Comments
 (0)