UNPKG

wykrestest

Version:

Candlestick Chart made with Konva, React and Jotai

98 lines (97 loc) 3.59 kB
import { parseISO } from 'date-fns'; export const initialDates = { year: 0, day: 0, month: 0, hour: 0, min: 0 }; const staticObj = { toggle: false, count: 0, data: [], bucket: '', }; const groups = { even: { ...staticObj, filterNum: 2, operator: '==', text: 'even' }, odd: { ...staticObj, filterNum: 2, operator: '!=', text: 'odd' }, quarter: { ...staticObj, filterNum: 3, operator: '==', text: 'quarter' }, weekly: { ...staticObj, filterNum: 5, operator: '==', text: 'weekly' }, fourHour: { ...staticObj, filterNum: 4, operator: '==', text: 'fourHour' }, fifteenMin: { ...staticObj, filterNum: 15, operator: '==', text: 'fifteenMin' }, rest: { ...staticObj, filterNum: 1, operator: '==', text: 'rest' }, }; export const initialMap = new Map([ ['years', [ { ...groups.even }, { ...groups.odd } ]], ['months', [ { ...groups.even }, { ...groups.odd } ]], ['days', [ { ...groups.fifteenMin }, { ...groups.fourHour }, ]], ['hours', [ { ...groups.fourHour }, { ...groups.odd } ]], ['mins', [ { ...groups.fifteenMin }, ]] ]); export const filterFunc = { '==': (data, num) => { return data.filter(el => el.value % num == 0); }, '!=': (data, num) => { return data.filter(el => el.value % num != 0); }, }; export const convertDateFunc = (apiDate) => { const date = parseISO(apiDate); return { year: date.getFullYear(), month: date.getMonth(), day: date.getDate(), hour: date.getHours(), min: date.getMinutes() }; }; //-------------------------_DateBucket const initDateBuckets = { year: [], months: [], days: [], hours: [], mins: [] }; const monthConverter = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; //updateFunc export const updateDateBuckets = (data) => { let baseDate = { ...initialDates }; let years = [], months = [], days = [], hours = [], mins = []; if (data.length > 1) { data.forEach((item, i) => { const currDate = convertDateFunc(item.api_date); if (baseDate.year != currDate.year) { years.push({ ...currDate, value: currDate.year, i: i, text: currDate.year.toString() }); } else if (baseDate.month != currDate.month) { months.push({ ...currDate, value: currDate.month, i: i, text: monthConverter[currDate.month] }); //monthConverter } else if (baseDate.day != currDate.day) { days.push({ ...currDate, value: currDate.day, i: i, text: currDate.day.toString() }); } else if (baseDate.hour != currDate.hour) { hours.push({ ...currDate, value: currDate.hour, i: i, text: currDate.hour.toString() }); } else if (baseDate.min != currDate.min) { mins.push({ ...currDate, value: currDate.min, i: i, text: currDate.min.toString() }); } baseDate = currDate; }); } return { years: years, months: months, days: days, hours: hours, mins: mins }; }; //DATE ARRAY-------------------------------------------------- export const initDateArray = () => { const dateMap = new Map(initialMap); const dateArray = []; dateMap.forEach((dateObj, bucket) => { dateObj.forEach(item => { dateArray.push({ ...item, bucket: bucket }); }); }); return dateArray; };