materialStatisticsRangeUtils.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. export const STAT_BLUE = '#5470c6';
  2. export const STAT_GREEN = '#91cc75';
  3. export const STAT_ORANGE = '#fac858';
  4. export function getDefaultDateRange(): [Date, Date] {
  5. const today = new Date();
  6. const oneMonthAgo = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
  7. return [oneMonthAgo, today];
  8. }
  9. function toDate(v: Date | { toDate?: () => Date }): Date {
  10. if (v && typeof (v as { toDate?: () => Date }).toDate === 'function') {
  11. return (v as { toDate: () => Date }).toDate();
  12. }
  13. return v as Date;
  14. }
  15. export function formatDateTimeRangeFromRangePicker(
  16. start: Date | { toDate?: () => Date },
  17. end: Date | { toDate?: () => Date }
  18. ): { startTime: string; endTime: string } {
  19. const s = toDate(start);
  20. const e = toDate(end);
  21. const startDate = new Date(s);
  22. startDate.setHours(0, 0, 0, 0);
  23. const endDate = new Date(e);
  24. endDate.setHours(23, 59, 59, 999);
  25. return { startTime: formatDateTime(startDate), endTime: formatDateTime(endDate) };
  26. }
  27. export function formatDateTime(date: Date): string {
  28. const y = date.getFullYear();
  29. const m = String(date.getMonth() + 1).padStart(2, '0');
  30. const d = String(date.getDate()).padStart(2, '0');
  31. const h = String(date.getHours()).padStart(2, '0');
  32. const min = String(date.getMinutes()).padStart(2, '0');
  33. const sec = String(date.getSeconds()).padStart(2, '0');
  34. return `${y}-${m}-${d} ${h}:${min}:${sec}`;
  35. }
  36. export function chartMinWidth(categories: number, perItem = 48) {
  37. return Math.max(360, categories * perItem);
  38. }