Skip to content

Commit a99c7c7

Browse files
authored
Add tooltips for stop purpose groups in 'Traffic Stops By Stop Purpose and Race Count'. (#357)
1 parent 657b4ea commit a99c7c7

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

frontend/src/Components/Charts/TrafficStops/TrafficStops.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import TrafficStopsStyled, {
88
PieWrapper,
99
StopGroupsContainer,
1010
SwitchContainer,
11+
Tooltip,
1112
} from './TrafficStops.styled';
1213
import * as S from '../ChartSections/ChartsCommon.styled';
1314
import { useTheme } from 'styled-components';
1415
import cloneDeep from 'lodash.clonedeep';
16+
import { usePopper } from 'react-popper';
17+
import tooltipLanguage from '../../../util/tooltipLanguage';
1518

1619
// Util
1720
import {
@@ -678,6 +681,31 @@ function TrafficStops(props) {
678681
return `Traffic Stops by Percentage for ${subject} since ${stopsByPercentageData.labels[0]}`;
679682
};
680683

684+
const [tooltipText, setTooltipText] = useState('');
685+
const [tooltipReferenceElement, setTooltipReferenceElement] = useState(null);
686+
const [popperElement, setPopperElement] = useState(null);
687+
const { styles, attributes } = usePopper(tooltipReferenceElement, popperElement, {
688+
placement: 'top-start',
689+
modifiers: [
690+
{
691+
name: 'offset',
692+
options: {
693+
offset: [0, 5],
694+
},
695+
},
696+
],
697+
});
698+
699+
const showTooltip = (key) => {
700+
setTooltipText(tooltipLanguage(key === 'Regulatory/Equipment' ? 'Regulatory Equipment' : key));
701+
popperElement.setAttribute('data-show', true);
702+
};
703+
704+
const hideTooltip = () => {
705+
setTooltipText('');
706+
popperElement.removeAttribute('data-show');
707+
};
708+
681709
return (
682710
<TrafficStopsStyled>
683711
{/* Traffic Stops by Percentage */}
@@ -838,6 +866,13 @@ function TrafficStops(props) {
838866
</LineChartWithPieContainer>
839867
</S.ChartSection>
840868
<S.ChartSection marginTop={5} id="stops_by_purpose_and_count">
869+
<Tooltip
870+
ref={setPopperElement}
871+
style={{ ...styles.popper, width: '300px' }}
872+
{...attributes.popper}
873+
>
874+
{tooltipText}
875+
</Tooltip>
841876
<ChartHeader
842877
chartTitle="Traffic Stops By Stop Purpose and Race Count"
843878
handleViewData={showGroupedStopPurposeModal}
@@ -873,17 +908,22 @@ function TrafficStops(props) {
873908
</SwitchContainer>
874909
<div style={{ marginTop: '1em' }}>
875910
<P weight={WEIGHTS[1]}>Toggle graphs:</P>
876-
<div style={{ display: 'flex', gap: '10px', flexDirection: 'row', flexWrap: 'wrap' }}>
911+
<div
912+
style={{ display: 'flex', gap: '10px', flexDirection: 'row', flexWrap: 'wrap' }}
913+
ref={setTooltipReferenceElement}
914+
>
877915
{visibleStopsGroupedByPurpose.map((vg, i) => (
878-
<Checkbox
879-
height={25}
880-
width={25}
881-
label={vg.title}
882-
value={vg.key}
883-
key={i}
884-
checked={vg.visible}
885-
onChange={toggleGroupedPurposeGraphs}
886-
/>
916+
<div onMouseEnter={() => showTooltip(vg.title)} onMouseLeave={hideTooltip}>
917+
<Checkbox
918+
height={25}
919+
width={25}
920+
label={vg.title}
921+
value={vg.key}
922+
key={i}
923+
checked={vg.visible}
924+
onChange={toggleGroupedPurposeGraphs}
925+
/>
926+
</div>
887927
))}
888928
</div>
889929
</div>

frontend/src/Components/Charts/TrafficStops/TrafficStops.styled.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,18 @@ export const LineChartWithPieContainer = styled.div`
7777
export const PieContainer = styled.div`
7878
width: 300px;
7979
`;
80+
81+
export const Tooltip = styled.div`
82+
background: #333;
83+
color: white;
84+
font-weight: bold;
85+
padding: 4px 8px;
86+
font-size: 13px;
87+
border-radius: 4px;
88+
visibility: hidden;
89+
z-index: 5;
90+
91+
&[data-show='true'] {
92+
visibility: visible;
93+
}
94+
`;

0 commit comments

Comments
 (0)