export interface ProfitChartWidgetType { [key: string]: string number; } interface…
- User
export interface ProfitChartWidgetType { [key: string]: string | number; }
interface CumulativeProfitChartProps { data: ProfitChartWidgetType; }
const CumulativeProfitChart = ({data}: CumulativeProfitChartProps) => { const [profitData, setProfitData] = useState<ProfitChartWidgetType[]>([]); const darkTheme = useTheme().palette.mode === "dark";
useEffect(() => { if (data.length === 0) { setProfitData([]); } else { setProfitData(parseProfitData(data)); } }, [data]);
const parseProfitData = (profitData: Record<string, string | number>): ProfitChartWidgetType[] => { const result: ProfitChartWidgetType[] = [];
const dates: string[] = Object.keys(profitData).sort((a, b) => dayjs(a).isBefore(dayjs(b)) ? -1 : 1);
for (const date of dates) { if (Object.hasOwnProperty.call(profitData, date)) { result.push({ date: date, profit: typeof profitData[date] === "number" ? profitData[date] : parseFloat(profitData[date] as string), }); } }
return result; };
нужно если, есть пустые даты, создавать новые с таким же значением, как предыдущие.. например с 16 по 27 нету даты, получается должны быть все до 27 с прибылью как у 16 числа
- Assistant
Чтобы создавать новые даты с таким же значением, как у предыдущей даты, вы можете использовать следующий код:
const parseProfitData = (profitData: Record<string, string | number>): ProfitChartWidgetType[] => { const result: ProfitChartWidgetType[] = []; const dates: string[] = Object.keys(profitData).sort((a, b) => dayjs(a).isBefore(dayjs(b)) ? -1 : 1); let previousProfit: number | undefined = undefined; for (const date of dates) { if (Object.hasOwnProperty.call(profitData, date)) { const currentProfit = typeof profitData[date] === "number" ? profitData[date] : parseFloat(profitData[date] as string); // If there are missing dates, create them and set the profit value to the previous profit value if (previousProfit !== undefined) { const currentDate = dayjs(date); const previousDate = dayjs(result[result.length - 1].date); while (currentDate.diff(previousDate, "day") > 1) { const missingDate = previousDate.add(1, "day").format("YYYY-MM-DD"); result.push({ date: missingDate, profit: previousProfit, }); previousDate.add(1, "day"); } } result.push({ date: date, profit: currentProfit, }); previousProfit = currentProfit; } } return result; };