-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverview.js
294 lines (266 loc) · 8.9 KB
/
Overview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import React, { useState, useEffect } from 'react';
import * as S from '../ChartSections/ChartsCommon.styled';
import OverviewStyled from './Overview.styled';
// Router
import { useHistory, useRouteMatch } from 'react-router-dom';
// Constants
import {
calculatePercentage,
YEARS_DEFAULT,
RACES,
calculateYearTotal,
reduceYearsToTotal,
} from '../chartUtils';
import * as slugs from '../../../Routes/slugs';
// Hooks
import useMetaTags from '../../../Hooks/useMetaTags';
// Data
import useDataset, {
AGENCY_DETAILS,
STOPS,
SEARCHES,
USE_OF_FORCE,
} from '../../../Hooks/useDataset';
// Children
import ChartHeader from '../ChartSections/ChartHeader';
import useOfficerId from '../../../Hooks/useOfficerId';
import PieChart from '../../NewCharts/PieChart';
import { pieChartConfig, pieChartLabels } from '../../../util/setChartColors';
function Overview(props) {
const { agencyId, agencyName, yearRange, year } = props;
const history = useHistory();
const match = useRouteMatch();
const officerId = useOfficerId();
useDataset(agencyId, STOPS);
useDataset(agencyId, SEARCHES);
const [chartState] = useDataset(agencyId, USE_OF_FORCE);
const initChartData = {
labels: pieChartLabels,
datasets: [
{
data: [],
...pieChartConfig,
},
],
loading: true,
};
const [censusPieData, setCensusPieData] = useState(initChartData);
const [trafficStopsData, setTrafficStopsData] = useState(initChartData);
const [searchesData, setSearchesData] = useState(initChartData);
const [useOfForceData, setUseOfForceData] = useState(initChartData);
const renderMetaTags = useMetaTags();
const subjectObserving = () => {
if (officerId) {
return 'by this officer';
}
if (agencyId === '-1') {
return 'for the entire state';
}
return 'by this department';
};
const getYearPhrase = () => (year && year !== 'All' ? ` in ${year}` : '');
const getChartModalSubHeading = (title) => `${title} ${subjectObserving()}${getYearPhrase()}.`;
const getOverviewSubheader = () =>
`Shows the race/ethnic composition of drivers ${subjectObserving()}${getYearPhrase()} reported using force against.`;
/* Build Data */
// CENSUS
useEffect(() => {
if (chartState.data[AGENCY_DETAILS].census_profile) {
const data = chartState.data[AGENCY_DETAILS].census_profile;
const chartData = RACES.map((race) => calculatePercentage(data[race], data['total']));
setCensusPieData({
labels: pieChartLabels,
datasets: [
{
data: chartData,
...pieChartConfig,
},
],
});
}
}, [chartState.data[AGENCY_DETAILS]]);
function buildPieData(data, setFunc, dropdownYear = null) {
let chartData = [0, 0, 0, 0, 0];
if (!dropdownYear || dropdownYear === 'All') {
const totals = {};
RACES.forEach((race) => {
totals[race] = reduceYearsToTotal(data, race)[race];
});
const total = calculateYearTotal(totals, RACES);
chartData = RACES.map((race) => calculatePercentage(totals[race], total));
} else {
const yearData = data.find((d) => d.year === dropdownYear);
if (yearData) {
const total = RACES.map((race) => yearData[race]).reduce((a, b) => a + b, 0);
chartData = RACES.map((race) => calculatePercentage(yearData[race], total));
}
}
setFunc({
labels: pieChartLabels,
datasets: [
{
data: chartData,
...pieChartConfig,
},
],
});
}
const pieChartTitle = (chartTitle) => {
let subject = agencyName;
if (officerId) {
subject = `Officer ${officerId}`;
}
let title = `${chartTitle} for ${subject}`;
if (chartTitle !== 'Census Demographics') {
title = `${title} ${
year === YEARS_DEFAULT ? `since ${yearRange[yearRange.length - 1]}` : `in ${year}`
}`;
}
return title;
};
// TRAFFIC STOPS
useEffect(() => {
if (chartState.data[STOPS]) {
buildPieData(chartState.data[STOPS], setTrafficStopsData, year);
}
}, [chartState.data[STOPS], year]);
// SEARCHES
useEffect(() => {
if (chartState.data[SEARCHES]) {
buildPieData(chartState.data[SEARCHES], setSearchesData, year);
}
}, [chartState.data[SEARCHES], year]);
// USE OF FORCE
useEffect(() => {
if (chartState.data[USE_OF_FORCE]) {
buildPieData(chartState.data[USE_OF_FORCE], setUseOfForceData, year);
}
}, [chartState.data[USE_OF_FORCE], year]);
const getPageTitleForShare = () => `Traffic Stop statistics for ${agencyName}`;
const useOfForcePieChartCopy = () => {
if (officerId) {
return 'this officer';
}
return 'law enforcement officers';
};
const buildUrl = (slug) => {
let url = `${match.url}${slug}`;
url = url.replace('//', '/');
if (officerId) url += `/?officer=${officerId}`;
return history.push(url);
};
return (
<OverviewStyled>
{renderMetaTags()}
<ChartHeader
chartTitle="Overview"
shareProps={{
twitterTitle: getPageTitleForShare(),
}}
/>
<S.SectionWrapper />
<S.ChartsWrapper>
<S.PieContainer>
<S.ChartTitle>Census Demographics</S.ChartTitle>
<S.PieWrapper>
<PieChart
data={censusPieData}
displayTitle
maintainAspectRatio
showWhiteBackground={false}
modalConfig={{
tableHeader: 'Census Demographics',
tableSubheader: `This data reflects the race/ethnic composition based on the most recent census data.
While it can be used for general comparative purposes, the actual driving population may
vary significantly from these figures.`,
agencyName,
chartTitle: pieChartTitle('Census Demographics'),
}}
/>
</S.PieWrapper>
<S.Note>
<strong>NOTE: </strong>
This data reflects the race/ethnic composition based on the most recent census data.
While it can be used for general comparative purposes, the actual driving population may
vary significantly from these figures.
</S.Note>
</S.PieContainer>
<S.PieContainer>
<S.ChartTitle>Traffic Stops</S.ChartTitle>
<S.PieWrapper>
<PieChart
data={trafficStopsData}
displayTitle
maintainAspectRatio
showWhiteBackground={false}
modalConfig={{
tableHeader: 'Traffic Stops',
tableSubheader: getChartModalSubHeading(
'Shows the race/ethnic composition of drivers stopped'
),
agencyName,
chartTitle: pieChartTitle('Traffic Stops'),
}}
/>
</S.PieWrapper>
<S.Note>
Shows the race/ethnic composition of drivers stopped {subjectObserving()}.
</S.Note>
<S.Link onClick={() => buildUrl(slugs.TRAFFIC_STOPS_SLUG)}>
View traffic stops over time
</S.Link>
</S.PieContainer>
</S.ChartsWrapper>
<S.ChartsWrapper>
<S.PieContainer>
<S.ChartTitle>Searches</S.ChartTitle>
<S.PieWrapper>
<PieChart
data={searchesData}
displayTitle
maintainAspectRatio
showWhiteBackground={false}
modalConfig={{
tableHeader: 'Searches',
tableSubheader: getChartModalSubHeading(
'Shows the race/ethnic composition of drivers searched'
),
agencyName,
chartTitle: pieChartTitle('Searches'),
}}
/>
</S.PieWrapper>
<S.Note>
Shows the race/ethnic composition of drivers searched {subjectObserving()}.
</S.Note>
<S.Link onClick={() => buildUrl(slugs.SEARCHES_SLUG)}>View searches over time</S.Link>
</S.PieContainer>
<S.PieContainer>
<S.ChartTitle>Use of Force</S.ChartTitle>
<S.PieWrapper>
<PieChart
data={useOfForceData}
displayTitle
maintainAspectRatio
showWhiteBackground={false}
modalConfig={{
tableHeader: 'Use of Force',
tableSubheader: getOverviewSubheader(),
agencyName,
chartTitle: pieChartTitle('Use of Force'),
}}
/>
</S.PieWrapper>
<S.Note>
Shows the race/ethnic composition of drivers whom {useOfForcePieChartCopy()} reported
using force against.
</S.Note>
<S.Link onClick={() => buildUrl(slugs.USE_OF_FORCE_SLUG)}>
View use of force over time
</S.Link>
</S.PieContainer>
</S.ChartsWrapper>
</OverviewStyled>
);
}
export default Overview;