wykrestest
Version:
Candlestick Chart made with Konva, React and Jotai
56 lines (55 loc) • 2.1 kB
JavaScript
import { atom } from 'jotai';
import { dataHLAtom, dimsAtom } from '../store';
export const priceArrayAtom = atom([], (get, set) => {
const dataHL = get(dataHLAtom);
const dims = get(dimsAtom);
const space = stepSpace(dataHL.diff);
const ikd = someFunc(dataHL, space, dims.heightSub, dataHL);
set(priceArrayAtom, ikd);
});
export const stepSpace = (diff) => {
const tempStep = diff / 2;
const mag = Math.floor(Math.log10(tempStep));
const magPow = Math.pow(10, mag);
return magPow;
};
export const someFunc = (highLow, space, height, dataHL) => {
const base = (Math.floor(highLow.low / space) * space);
const top = (Math.floor(highLow.high / space) * space);
let currSpace = space;
let mainArr = [];
if (highLow.diff != 0) {
for (let i = 0; i < 2; i++) { //i<2 is where we can adjust how many loops we want to go through
let currCount = base;
let tempEven = [];
let tempOdd = [];
let fx = currSpace.toString().split('.')[1].length;
let even = true;
while (currCount < top) {
if (even) {
tempEven.push({
price: currCount,
text: currCount.toFixed(fx),
pixel: ((dataHL.high - currCount) / dataHL.diff) * height
});
// tempEven.push(currCount.toFixed(fx))
even = false;
}
else {
tempOdd.push({
price: currCount,
text: currCount.toFixed(fx),
pixel: ((dataHL.high - currCount) / dataHL.diff) * height
});
// tempOdd.push(currCount.toFixed(fx))
even = true;
}
currCount += currSpace;
}
mainArr.push({ data: tempEven, toggle: false });
mainArr.push({ data: tempOdd, toggle: false });
currSpace /= 10;
}
}
return mainArr;
};