Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service page improvements #1007

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions common/components/charts/ByHourHistogram/ByHourHistogram.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useMemo } from 'react';
import React, { useMemo, useRef } from 'react';
import { Bar as BarChart } from 'react-chartjs-2';
import ChartjsPluginWatermark from 'chartjs-plugin-watermark';
import {
Chart as ChartJS,
CategoryScale,
Expand All @@ -11,6 +12,7 @@ import {
import Color from 'color';

import { useBreakpoint } from '../../../hooks/useBreakpoint';
import { watermarkLayout } from '../../../constants/charts';
import type { ByHourDataset, DisplayStyle, ValueAxis as ValueAxis } from './types';
import { resolveStyle } from './styles';

Expand All @@ -20,6 +22,8 @@ interface Props {
style?: Partial<DisplayStyle>;
data: ByHourDataset[];
valueAxis: ValueAxis;
/** Whether the dataset is roundtrips, and we want to additionally convert and show the values as headways */
datasetRoundTrips?: boolean;
}

const allTimeLabels = ['AM', 'PM']
Expand Down Expand Up @@ -51,11 +55,17 @@ const stripZeroHoursAndRotateMidnightToEnd = (
};

export const ByHourHistogram: React.FC<Props> = (props) => {
const { data: dataWithZeros, valueAxis, style: baseStyle = null } = props;
const {
data: dataWithZeros,
valueAxis,
style: baseStyle = null,
datasetRoundTrips = false,
} = props;
const { data, timeLabels } = useMemo(
() => stripZeroHoursAndRotateMidnightToEnd(dataWithZeros),
[dataWithZeros]
);
const ref = useRef();
const isMobile = !useBreakpoint('md');

const chartData = useMemo(() => {
Expand Down Expand Up @@ -96,6 +106,9 @@ export const ByHourHistogram: React.FC<Props> = (props) => {
},
},
},
responsive: true,
maintainAspectRatio: false,
watermark: watermarkLayout(isMobile),
plugins: {
legend: {
display: true,
Expand All @@ -109,13 +122,22 @@ export const ByHourHistogram: React.FC<Props> = (props) => {
const dataset = data[datasetIndex];
const value = dataset.data[dataIndex];
const { label } = dataset;
return `${label}: ${value} ${valueAxis.tooltipItemLabel ?? ''}`.trim();
return `${label}: ${value} ${valueAxis.tooltipItemLabel ?? ''} ${datasetRoundTrips ? `(${Math.round((60 / value) * 2)}m headways)` : ''}`.trim();
},
devinmatte marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
};
}, [valueAxis.title, valueAxis.tooltipItemLabel, data]);
}, [valueAxis.title, valueAxis.tooltipItemLabel, isMobile, data, datasetRoundTrips]);

return <BarChart data={chartData} options={chartOptions} height={isMobile ? 50 : 70} />;
return (
<BarChart
redraw
ref={ref}
data={chartData}
options={chartOptions}
height={isMobile ? 240 : 80}
plugins={[ChartjsPluginWatermark]}
/>
);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useMemo } from 'react';
import { Line as LineChart } from 'react-chartjs-2';
import ChartjsPluginWatermark from 'chartjs-plugin-watermark';
import {
Chart as ChartJS,
CategoryScale,
Expand All @@ -17,7 +18,6 @@ import {
} from 'chart.js';
import Annotation from 'chartjs-plugin-annotation';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import ChartjsPluginWatermark from 'chartjs-plugin-watermark';
import 'chartjs-adapter-date-fns';
import type { ChartData } from 'chart.js';

Expand All @@ -26,6 +26,7 @@ import { useBreakpoint } from '../../../hooks/useBreakpoint';
import { ChartDiv } from '../ChartDiv';
import { CHART_COLORS, COLORS } from '../../../constants/colors';

import { watermarkLayout } from '../../../constants/charts';
import type {
AppliedDisplayStyle,
Benchmark,
Expand Down Expand Up @@ -225,6 +226,7 @@ export const TimeSeriesChart = <Data extends Dataset[]>(props: Props<Data>) => {
interaction: {
intersect: false,
},
watermark: watermarkLayout(isMobile),
plugins: {
datalabels: {
display: false,
Expand Down
14 changes: 9 additions & 5 deletions modules/service/DailyServiceHistogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ByHourHistogram } from '../../common/components/charts/ByHourHistogram'
import { useDelimitatedRoute } from '../../common/utils/router';
import { prettyDate } from '../../common/utils/date';
import { ButtonGroup } from '../../common/components/general/ButtonGroup';
import { CarouselGraphDiv } from '../../common/components/charts/CarouselGraphDiv';

interface Props {
scheduledService: ScheduledService;
Expand Down Expand Up @@ -39,11 +40,14 @@ export const DailyServiceHistogram: React.FC<Props> = (props) => {

return (
<>
<ByHourHistogram
data={data}
style={{ color }}
valueAxis={{ title: 'Scheduled round trips', tooltipItemLabel: 'round trips' }}
/>
<CarouselGraphDiv>
devinmatte marked this conversation as resolved.
Show resolved Hide resolved
<ByHourHistogram
data={data}
style={{ color }}
valueAxis={{ title: 'Scheduled round trips', tooltipItemLabel: 'round trips' }}
datasetRoundTrips
/>
</CarouselGraphDiv>
<div className={'flex w-full justify-center pt-2'}>
<ButtonGroup
line={line}
Expand Down
Loading