(
data: { x: string; y: number }[],
startDate: dayjs.ConfigType,
endDate: dayjs.ConfigType,
unit: DateUnit
)
| 26 | * replace with `@tianji/shared` |
| 27 | */ |
| 28 | export function getDateArray( |
| 29 | data: { x: string; y: number }[], |
| 30 | startDate: dayjs.ConfigType, |
| 31 | endDate: dayjs.ConfigType, |
| 32 | unit: DateUnit |
| 33 | ) { |
| 34 | const arr = []; |
| 35 | const { diff, add, normalize } = createDateUnitFn(unit); |
| 36 | const n = diff(endDate, startDate) + 1; |
| 37 | |
| 38 | function findData(date: dayjs.Dayjs) { |
| 39 | const d = data.find(({ x }) => { |
| 40 | return normalize(dayjs(x)).unix() === date.unix(); |
| 41 | }); |
| 42 | |
| 43 | return d?.y || 0; |
| 44 | } |
| 45 | |
| 46 | for (let i = 0; i < n; i++) { |
| 47 | const t = normalize(add(startDate, i)); |
| 48 | const y = findData(t); |
| 49 | |
| 50 | arr.push({ x: formatDate(t), y }); |
| 51 | } |
| 52 | |
| 53 | return arr; |
| 54 | } |
| 55 | |
| 56 | export function formatDate(val: dayjs.ConfigType) { |
| 57 | return dayjs(val).format('YYYY-MM-DD HH:mm:ss'); |
no test coverage detected