uPlot icon indicating copy to clipboard operation
uPlot copied to clipboard

Log scale jumps due to slightly out-of-range data points

Open agudulin opened this issue 1 year ago • 6 comments

Hi! I noticed an issue with charts using a log scale.

Image

https://jsfiddle.net/8zvexnwd/2/

In the example above, both axes use distr: 3 and auto: true.

The list of data points includes values just slightly above 100k and 1M, which causes the scale to jump to 1M and 10M, making the chart hard to read:

const data = [
  [1704326400, 1704412800, 1704499200],
  [1000001, 1000000, 990000],
  [99000, 100000, 100000.001]
];

agudulin avatar Apr 14 '25 08:04 agudulin

what behavior do you expect?

leeoniya avatar Apr 14 '25 14:04 leeoniya

Ideally, the range boundaries would adapt - for example, with a 10% gap above the maximum and below the minimum values. Is it possible to achieve this in auto mode, or should I define a custom range with explicit min and max values instead?

agudulin avatar Apr 14 '25 14:04 agudulin

imo, it is really important to have a tick/reference above the highest data value, especially in Log scales, otherwise it is way too easy to misinterpret the magnitude of unbounded whitespace.

the built-in log ranging util uPlot.rangeLog(dataMin, dataMax, logBase, fullMags) includes the last option to snap to partial magnitudes, you should be able to pretty easily improve the above scenario by having the top go to 2M and 200k, without writing fully manual scale.range functions.

https://github.com/leeoniya/uPlot/blob/1c147324e32c0a6db65b069062632bbd330c0799/dist/uPlot.d.ts#L141-L142

this should work, but appears to be broken right now:

{
  range: (u, dataMin, dataMax) => uPlot.rangeLog(dataMin, dataMax, 10, false),
}

i'll try to get it fixed this week.

leeoniya avatar Apr 14 '25 15:04 leeoniya

just to be clear, even when fixed, it won't really address "Log scale jumps due to slightly out-of-range data points". it will still jump, just less.

if you want to eliminate any kind of jumping, you'll have to do your own scale.range logic that either allows the plotted data to be clipped by the plot limits, or pre-configure the scale min/max that accommodates values that may go slightly above or below the outermost ticks.

leeoniya avatar Apr 14 '25 16:04 leeoniya

fixed the partial magnitudes issue. added your example to bottom here:

https://leeoniya.github.io/uPlot/demos/log-scales2.html

thanks!

leeoniya avatar Apr 16 '25 16:04 leeoniya

Thank you! :)

agudulin avatar Apr 16 '25 17:04 agudulin