leeoniya / uPlot

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

bar cut-off #857

Closed hz2018tv closed 10 months ago

hz2018tv commented 10 months ago

0430 0721

same everything, only replaced the uPlot from 230430 to 230721, got the bottom of the bar cut-off. what could it be? looks like sth in scaling

leeoniya commented 10 months ago

looks like sth in scaling

maybe :shrug:. i'll need runnable code to tell you what's going on.

you can try running different commits in between to see which commit changed this. you can use git bisect to help you quickly find when this happened: https://stackoverflow.com/questions/4713088/how-to-use-git-bisect

hz2018tv commented 10 months ago

interesting, 230705 does not have this problem 0705

leeoniya commented 10 months ago

maybe this? https://github.com/leeoniya/uPlot/commit/c057b8e4d0b24f19bae8d023f2eb9440fa55cc89

what is your data like?

hz2018tv commented 10 months ago

yes, I replaced the getMinMax with old code in iife.js, it seems back :D 0721-2

leeoniya commented 10 months ago

can you paste your data arrays here?

hz2018tv commented 10 months ago

here you go 230623,1,2 230626,-1,-1 230627,1,1 230628,-1,-1 230629,-1,-2 230630,-1,-3 230703,-1,-4 230705,-1,-5 230706,1,1 230707,-1,-1 230710,-1,-2 230711,-1,-3 230712,-1,-4 230713,-1,-5 230714,1,1 230717,1,2 230718,-1,-1 230719,-1,-2 230720,-1,-3

leeoniya commented 10 months ago

okay. it's because your data arrays are probably parsed as strings instead of numbers. uPlot does not support string values in the data, so you need to fix this on your end.

https://jsfiddle.net/93crox0d/2/

const inf = Infinity;
const min = Math.min;
const max = Math.max;

function getMinMaxNew(data, _i0, _i1, sorted) {
  let _min = inf;
  let _max = -inf;

  if (sorted == 1) {
    _min = data[_i0];
    _max = data[_i1];
  }
  else if (sorted == -1) {
    _min = data[_i1];
    _max = data[_i0];
  }
  else {
    for (let i = _i0; i <= _i1; i++) {
      let v = data[i];

      if (v != null) {
        if (v < _min)
          _min = v;
        if (v > _max)
          _max = v;
      }
    }
  }

  return [_min, _max];
}

function getMinMaxOld(data, _i0, _i1, sorted) {
  let _min = inf;
  let _max = -inf;

  if (sorted == 1) {
    _min = data[_i0];
    _max = data[_i1];
  }
  else if (sorted == -1) {
    _min = data[_i1];
    _max = data[_i0];
  }
  else {
    for (let i = _i0; i <= _i1; i++) {
      if (data[i] != null) {
        _min = min(_min, data[i]);
        _max = max(_max, data[i]);
      }
    }
  }

  return [_min, _max];
}

let x = [];
let y1 = [];
let y2 = [];

const data = `
230623,1,2
230626,-1,-1
230627,1,1
230628,-1,-1
230629,-1,-2
230630,-1,-3
230703,-1,-4
230705,-1,-5
230706,1,1
230707,-1,-1
230710,-1,-2
230711,-1,-3
230712,-1,-4
230713,-1,-5
230714,1,1
230717,1,2
230718,-1,-1
230719,-1,-2
230720,-1,-3
`.trim().split("\n").forEach(r => {
  let vals = r.split(',');
  x.push(+vals[0]);
  y1.push(+vals[1]);
  y2.push(+vals[2]);
});

let idx0 = 0;
let idx1 = x.length - 1;

console.log(x, y1, y2);
console.log(getMinMaxOld(x, idx0, idx1), getMinMaxNew(x, idx0, idx1));
console.log(getMinMaxOld(y1, idx0, idx1), getMinMaxNew(y1, idx0, idx1));
console.log(getMinMaxOld(y2, idx0, idx1), getMinMaxNew(y2, idx0, idx1));

// maybe strings?
let _x = [];
let _y1 = [];
let _y2 = [];

const data2 = `
230623,1,2
230626,-1,-1
230627,1,1
230628,-1,-1
230629,-1,-2
230630,-1,-3
230703,-1,-4
230705,-1,-5
230706,1,1
230707,-1,-1
230710,-1,-2
230711,-1,-3
230712,-1,-4
230713,-1,-5
230714,1,1
230717,1,2
230718,-1,-1
230719,-1,-2
230720,-1,-3
`.trim().split("\n").forEach(r => {
  let vals = r.split(',');
  _x.push(vals[0]);
  _y1.push(vals[1]);
  _y2.push(vals[2]);
});

console.log(_x, _y1, _y2);
console.log(getMinMaxOld(_x, idx0, idx1), getMinMaxNew(_x, idx0, idx1));
console.log(getMinMaxOld(_y1, idx0, idx1), getMinMaxNew(_y1, idx0, idx1));
console.log(getMinMaxOld(_y2, idx0, idx1), getMinMaxNew(_y2, idx0, idx1));
hz2018tv commented 10 months ago

cool. I forced a number() on the data, with your new getMinMax(), seems ok. thanks!