leeoniya / uPlot

📈 A small, fast chart for time series, lines, areas, ohlc & bars
MIT License
8.51k stars 371 forks source link

Axes grid lines with 0.04 incrs #805

Closed aurbano closed 1 year ago

aurbano commented 1 year ago

I must be missing something but I can't get it to render grid lines at 0.04 incrs, 0.05 seems to be the smallest that is rendered.

See this https://jsfiddle.net/pnoh14aq/ - it should show red grid lines at 0.05 (shown), and blue grid lines at 0.04 (not shown)

I've trying playing around with the space property but couldn't get this to work, any ideas?

Sample axes configuration:

axes: [
    {
        "scale": "x",
        "show": true,
        "side": 2,
        "space": 0,
        "incrs": [
            0.05
        ],
        "grid": {
            "stroke": "red",
            "width": 1
        },
    },
    {
        "scale": "x",
        "show": true,
        "side": 2,
        "space": 0,
        "incrs": [
            0.04
        ],
        "grid": {
            "stroke": "blue",
            "width": 1
        },
    }
],
leeoniya commented 1 year ago

hmm, it might just be that 0.04 is not part of the internally generated increments. 0.02 works okay: https://jsfiddle.net/ayeu0hsm/

probably something in here:

https://github.com/leeoniya/uPlot/blob/4d9bb330ca0a987564ec15dfc9bbb3e282db64e2/src/opts.js#L47-L60

https://github.com/leeoniya/uPlot/blob/4d9bb330ca0a987564ec15dfc9bbb3e282db64e2/src/uPlot.js#L257-L273

aurbano commented 1 year ago

Thank you for the quick reply!

Sorry I may have missed it but is there anything we can do to render at 0.04 then, or are you saying that this increment is not supported?

leeoniya commented 1 year ago

i'll look into the cause/fix in the next few days.

you can always override axis.splits and do your own math to return exactly the tick values that you need.

aurbano commented 1 year ago

Thank you so much! I had been looking through all the options but I hadn't realized that I had to remove incrs if using custom splits - it's working now cheers!

leeoniya commented 1 year ago

linked commit should fix this.

at some point i might expose control over the internal const allMults = [1,2,2.5,5]; array so the default increments can be better controlled without unnecessary headaches. as you can tell, the need for this doesn't come up very often :)

leeoniya commented 1 year ago

but I hadn't realized that I had to remove incrs if using custom splits

hmm, that's not intentional.

internally, splits is always converted to a callback, and increment + minSpace finding runs ahead of splits (which takes those as args). but this bails if the found space is 0, so never gets to running splits:

https://github.com/leeoniya/uPlot/blob/eb1199288a0209306e104410be85b58ec030e513/src/uPlot.js#L1711-L1719

i would have to store a flag that the axis has an explicit splits array before it is converted to callback, and then skip the getIncrSpace finding.

it's a bug, but also really hard to hit. e.g. you wouldn't have hit it if incrs: [0.04] worked for you originally, as it now does.

aurbano commented 1 year ago

Thank you for the quick resolution! Any idea when this should be released?

So once this fix is released I would be able to just use 0.04 as an incr, or would I still need to define a custom splits method?

A downside I've just found with the custom splits is that now the axis doesn't "follow" the plot when panning, in case anyone has a similar issue this is my current splits method to ensure that the grid stays consistent:

// incr = 0.04 in my case
splits: (_, __, scaleMin, scaleMax) => {
      // Temporary workaround to support small increments
      const startingPoint = Math.floor(scaleMin);
      const totalSplits = Math.ceil((scaleMax - startingPoint) / incr);
      const splits = [];
      for (let i = 0; i < totalSplits; i++) {
          splits.push(startingPoint + i * incr);
      }
      return splits;
},
leeoniya commented 1 year ago

Thank you for the quick resolution! Any idea when this should be released?

i'll aim for this week.

So once this fix is released I would be able to just use 0.04 as an incr

yep

aurbano commented 1 year ago

@leeoniya any updates on releasing this? Unless I'm missing something it looks like there haven't been any releases since 1.6.24?

leeoniya commented 1 year ago

i've been on vacation for a couple weeks. but will be back in next few days. will need to dig through new issue reports but should be a release soon after that.

aurbano commented 1 year ago

Thank you for the quick reply! Hope you had a great time off 😄

I'll keep an eye out, thanks again