-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDotGridWrapper.jsx
245 lines (218 loc) · 7.36 KB
/
DotGridWrapper.jsx
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
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import * as d3 from 'd3';
import isEqual from 'lodash/isEqual';
import * as pt from '../common/reactPropTypeDefs';
import {
dateRangesSelector,
primaryEntityValuesFilteredSelector,
secondaryEntityValuesFilteredSelector,
customGeographyValuesFilteredSelector,
referenceEntityValuesFilteredSelector,
} from '../common/reduxSelectors';
import DotGridChart from '../components/DotGridCharts/DotGridChart';
const mapStateToProps = (state, props) => {
const { browser, filterType } = state;
const { entityType, period, personType } = props;
const dateRange = dateRangesSelector(state, props);
let values = [];
switch (entityType) {
case 'primary':
values = primaryEntityValuesFilteredSelector(state, props);
break;
case 'secondary':
values = secondaryEntityValuesFilteredSelector(state, props);
break;
case 'citywide':
values = referenceEntityValuesFilteredSelector(state, props);
break;
case 'custom':
values = customGeographyValuesFilteredSelector(state, props);
break;
default:
throw new Error('DotGridWrapper got unexpected entityType');
}
return {
appWidth: browser.width,
filterType,
startDate: dateRange.startDate,
endDate: dateRange.endDate,
period,
personType,
entityType,
values,
};
};
/*
* Class that wraps the Dot Grid Chart connecting it to parts of the Redux Store
* Unlike the Line Charts, each Dot Grid Chart gets a single geo entity, time period, and person type
*/
class DotGridWrapper extends Component {
static propTypes = {
appWidth: PropTypes.number.isRequired,
entityType: pt.key.isRequired,
customGeography: pt.coordinatelist,
filterType: pt.filterType.isRequired,
period: PropTypes.string.isRequired,
values: PropTypes.arrayOf(PropTypes.object),
startDate: pt.date.isRequired,
endDate: pt.date.isRequired,
subheadHeights: PropTypes.shape({
cyclist: PropTypes.number,
motorist: PropTypes.number,
pedestrian: PropTypes.number,
}),
radius: PropTypes.number,
strokeWidth: PropTypes.number,
title: PropTypes.string,
personType: PropTypes.string.isRequired,
};
static defaultProps = {
values: [],
customGeography: [], // only if entityType=='custom' see DotGridChartsContainer.jsx
subheadHeights: {},
radius: 5,
strokeWidth: 1,
title: '',
};
constructor() {
super();
// state stores a "transformed" view of the data,
// this contains values necessary for the grid of circles drawn by the DotGridChart
this.state = {
valuesTransformed: {},
};
this.chartContainer = null;
}
componentDidMount() {
const { values, filterType } = this.props;
// if we already have filtered values, group them so the chart can be drawn
if (values.length) {
this.groupEntityData(filterType, values);
}
}
componentWillReceiveProps(nextProps) {
const { appWidth, values, filterType, startDate, endDate } = nextProps;
// if we receive filtered values
if (values.length) {
// and
// we didn't have filtered values before
// or either start or end date changed
// or crash filter types changed
// or the viewport was resized
if (
!this.props.values.length ||
startDate !== this.props.startDate ||
endDate !== this.props.endDate ||
!isEqual(filterType, this.props.filterType) ||
appWidth !== this.props.appWidth
) {
// group them so the chart can be drawn
this.groupEntityData(filterType, values);
}
}
// if an entity is deselected, clear the component state
if (!values.length && this.props.values.length) {
this.setState({
valuesTransformed: {},
});
}
}
getContainerSize() {
const cWidth = this.chartContainer.clientWidth - 10; // account for padding
const cHeight = this.chartContainer.clientHeight - 40; // account for padding
return {
height: cHeight,
width: cWidth,
};
}
groupEntityData(filterType, values) {
// creates the grid used by the chart to draw & position svg circle elements
const { radius, strokeWidth, personType } = this.props;
const width = this.getContainerSize().width;
const chartWidth = width;
const personInjuredStr = `${personType}_injured`; // map person type to field name injured
const personKilledStr = `${personType}_killed`; // map person type to field name killed
// allowed keys in values object
const allowed = [personInjuredStr, personKilledStr];
// make sure there will be data before transforming it
const test = Object.keys(values[0]).filter(key => allowed.includes(key));
if (test.length) {
// filter data by person type (e.g. 'pedestrian' or 'motorist' or 'cyclist')
// but if no injury or fatality was selected for one of the above, this will be an array of empty objects
const filtered = values.map(item =>
Object.keys(item)
.filter(key => allowed.includes(key))
.reduce((obj, key) => {
obj[key] = item[key];
return obj;
}, {})
);
// total up injuries and fatalities if they were included in crash type filters
const injuredTotal = test.includes(personInjuredStr)
? d3.sum(filtered, d => d[personInjuredStr])
: 0;
const killedTotal = test.includes(personKilledStr)
? d3.sum(filtered, d => d[personKilledStr])
: 0;
// use the total of both injury & fatality totals for a "grand total" to calculate the grid
let harmedTotal = 0;
if (injuredTotal && killedTotal) {
harmedTotal = injuredTotal + killedTotal;
} else if (injuredTotal && !killedTotal) {
harmedTotal = injuredTotal;
} else {
harmedTotal = killedTotal;
}
// compute the grid for the circles
const columns = Math.floor(chartWidth / (radius * 3));
const rows = Math.floor(harmedTotal / columns);
const gridHeight = rows * radius * 3 + (radius + strokeWidth) * 2;
const gridWidth = chartWidth;
const grid = d3.range(harmedTotal).map(d => ({
x: (d % columns) * radius * 3,
y: Math.floor(d / columns) * radius * 3,
}));
// set state with the new data which will cause a re-render and pass the data to the chart
this.setState({
valuesTransformed: {
filtered,
injuredTotal,
killedTotal,
harmedTotal,
columns,
rows,
gridHeight,
gridWidth,
grid,
},
});
} else {
// if there was no data to be processed, reset component state
this.setState({
valuesTransformed: {},
});
}
}
render() {
const { radius, strokeWidth, personType } = this.props;
const { valuesTransformed } = this.state;
const drawdots = this.props.entityType !== 'citywide'; // issue 58, dots not certain; skip if citywide
return (
<div
className="DotGridWrapper"
ref={_ => {
this.chartContainer = _;
}}
>
<DotGridChart
data={valuesTransformed}
drawdots={drawdots}
{...{ radius, strokeWidth, personType }}
/>
</div>
);
}
}
export default connect(mapStateToProps, null)(DotGridWrapper);