Skip to content

Commit

Permalink
Fix round trip numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
devinmatte committed Sep 13, 2024
1 parent cd8963b commit 759e47e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 18 additions & 7 deletions common/components/charts/ByHourHistogram/ByHourHistogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'chart.js';
import Color from 'color';

import type { Context } from 'chartjs-plugin-datalabels';
import { useBreakpoint } from '../../../hooks/useBreakpoint';
import { watermarkLayout } from '../../../constants/charts';
import type { ByHourDataset, DisplayStyle, ValueAxis as ValueAxis } from './types';
Expand Down Expand Up @@ -68,6 +69,20 @@ export const ByHourHistogram: React.FC<Props> = (props) => {
const ref = useRef();
const isMobile = !useBreakpoint('md');

const tooltipFormat = React.useCallback(
({ datasetIndex, dataIndex }: Context) => {
const dataset = data[datasetIndex];
const value = dataset.data[dataIndex];
const { label } = dataset;

if (datasetRoundTrips) {
return `${label}: ${value} ${valueAxis.tooltipItemLabel ?? ''} (${Math.round(60 / value)}m headways)`.trim();
}
return `${label}: ${value} ${valueAxis.tooltipItemLabel ?? ''}`.trim();
},
[data, datasetRoundTrips, valueAxis.tooltipItemLabel]
);

const chartData = useMemo(() => {
return {
labels: timeLabels,
Expand Down Expand Up @@ -118,25 +133,21 @@ export const ByHourHistogram: React.FC<Props> = (props) => {
mode: 'index' as const,
callbacks: {
label: (context) => {
const { datasetIndex, dataIndex } = context;
const dataset = data[datasetIndex];
const value = dataset.data[dataIndex];
const { label } = dataset;
return `${label}: ${value} ${valueAxis.tooltipItemLabel ?? ''} ${datasetRoundTrips ? `(${Math.round((60 / value) * 2)}m headways)` : ''}`.trim();
return tooltipFormat(context);
},
},
},
},
};
}, [valueAxis.title, valueAxis.tooltipItemLabel, isMobile, data, datasetRoundTrips]);
}, [valueAxis.title, isMobile, tooltipFormat]);

return (
<BarChart
redraw
ref={ref}
data={chartData}
options={chartOptions}
height={isMobile ? 240 : 80}
height={isMobile ? 200 : 75}
plugins={[ChartjsPluginWatermark]}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions modules/service/DailyServiceHistogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export const DailyServiceHistogram: React.FC<Props> = (props) => {
return [
{
label: prettyDate(startDate),
data: startServiceLevels!,
data: startServiceLevels!.map((value) => value / 2),
style: { opacity: 0.5 },
},
{
label: prettyDate(endDate),
data: endServiceLevels!,
data: endServiceLevels!.map((value) => value / 2),
},
];
}, [scheduledService, dayKind]);
Expand Down

0 comments on commit 759e47e

Please sign in to comment.