splitSeriesIntoCompleteAndIncomplete.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import partition from 'lodash/partition';
  2. import type {TimeseriesData} from '../common/types';
  3. import {markDelayedData} from './markDelayedData';
  4. export function splitSeriesIntoCompleteAndIncomplete(
  5. serie: TimeseriesData,
  6. delay: number
  7. ): Array<TimeseriesData | undefined> {
  8. const markedTimeserie = markDelayedData(serie, delay);
  9. const [completeData, incompleteData] = partition(
  10. markedTimeserie.data,
  11. datum => !datum.delayed
  12. );
  13. // If there is both complete and incomplete data, prepend the incomplete data
  14. // with the final point from the complete data. This way, when the series are
  15. // plotted, there's a connecting line between them
  16. const finalCompletePoint = completeData.at(-1);
  17. if (incompleteData.length > 0 && finalCompletePoint) {
  18. incompleteData.unshift({...finalCompletePoint});
  19. }
  20. // Discard the delayed property since the split series already communicate
  21. // that information
  22. return [
  23. completeData.length > 0
  24. ? {
  25. ...serie,
  26. data: completeData.map(discardDelayProperty),
  27. }
  28. : undefined,
  29. incompleteData.length > 0
  30. ? {
  31. ...serie,
  32. data: incompleteData.map(discardDelayProperty),
  33. }
  34. : undefined,
  35. ];
  36. }
  37. function discardDelayProperty(
  38. datum: TimeseriesData['data'][number]
  39. ): Omit<TimeseriesData['data'][number], 'delayed'> {
  40. const {delayed: _delayed, ...other} = datum;
  41. return other;
  42. }