Skip to content

Commit

Permalink
align service graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
idreyn committed Jun 18, 2024
1 parent 32d79d0 commit 35cdfea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export const TimeSeriesChart = <Data extends Dataset[]>(props: Props<Data>) => {
},
};

console.log({ unit, time });

Check failure on line 153 in common/components/charts/TimeSeriesChart/TimeSeriesChart.tsx

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Unexpected console statement

return {
x: {
min: timeAxis.from,
Expand Down
10 changes: 10 additions & 0 deletions common/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const indexByProperty = <T extends { [key: string]: any }>(
array: T[],

Check failure on line 2 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `··`
property: keyof T

Check failure on line 3 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `··`
) => {
const res: Record<string, T> = {};

Check failure on line 5 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `··`
array.forEach((el) => {

Check failure on line 6 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `··`
res[el[property]] = el;

Check failure on line 7 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `····`
});

Check failure on line 8 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `··`
return res;

Check failure on line 9 in common/utils/array.ts

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Delete `··`
};
30 changes: 24 additions & 6 deletions modules/service/ServiceGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { DeliveredTripMetrics, ScheduledService } from '../../common/types/
import type { ParamsType } from '../speed/constants/speeds';
import { ChartBorder } from '../../common/components/charts/ChartBorder';
import { DownloadButton } from '../../common/components/buttons/DownloadButton';
import { indexByProperty } from '../../common/utils/array';
import { ScheduledAndDeliveredGraph } from './ScheduledAndDeliveredGraph';
import { getShuttlingBlockAnnotations } from './utils/graphUtils';

Expand Down Expand Up @@ -36,11 +37,27 @@ export const ServiceGraph: React.FC<ServiceGraphProps> = (props: ServiceGraphPro
}));
}, [data]);

const allDates = [
...new Set([
...predictedData.counts.map((point) => point.date),
...data.map((point) => point.date),
]),
].sort((a, b) => (a > b ? 1 : -1));

const scheduledDataByDate = indexByProperty(predictedData.counts, 'date');
const deliveredDataByDate = indexByProperty(data, 'date');

console.log(allDates);

Check failure on line 50 in modules/service/ServiceGraph.tsx

View workflow job for this annotation

GitHub Actions / frontend (20, 3.12)

Unexpected console statement

const scheduled = useMemo(() => {
return {
label: 'Scheduled round trips',
data: predictedData.counts.map(({ date, count }, index) => {
const value = data[index]?.miles_covered > 0 && count ? count / 2 : 0;
data: allDates.map((date) => {
const scheduledToday = scheduledDataByDate[date];
const deliveredToday = deliveredDataByDate[date];
const anyDeliveredToday = deliveredToday?.miles_covered > 0;
const value =
scheduledToday.count && anyDeliveredToday ? Math.round(scheduledToday.count) / 2 : 0;
return { date, value };
}),
style: {
Expand All @@ -51,14 +68,15 @@ export const ServiceGraph: React.FC<ServiceGraphProps> = (props: ServiceGraphPro
},
},
};
}, [data, predictedData, peak]);
}, [allDates, deliveredDataByDate, peak, scheduledDataByDate]);

const delivered = useMemo(() => {
return {
label: 'Daily round trips',
data: data.map((datapoint) => {
const value = datapoint.miles_covered ? Math.round(datapoint.count) : 0;
return { date: datapoint.date, value };
data: allDates.map((date) => {
const deliveredToday = deliveredDataByDate[date];
const value = deliveredToday?.miles_covered ? Math.round(deliveredToday.count) : 0;
return { date, value };
}),
style: {
tooltipLabel: (point) => {
Expand Down

0 comments on commit 35cdfea

Please sign in to comment.