-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathwidget-gain-located-selectors.js
129 lines (121 loc) · 4.37 KB
/
widget-gain-located-selectors.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
import { createSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import uniqBy from 'lodash/uniqBy';
import sumBy from 'lodash/sumBy';
import { sortByKey } from 'utils/data';
import { format } from 'd3-format';
// get list data
const getGain = state => state.gain || null;
const getExtent = state => state.extent || null;
const getSettings = state => state.settings || null;
const getOptions = state => state.options || null;
const getIndicator = state => state.indicator || null;
const getLocation = state => state.location || null;
const getLocationsMeta = state => state.meta || null;
const getLocationNames = state => state.locationNames || null;
const getColors = state => state.colors || null;
export const getSortedData = createSelector(
[getGain, getExtent, getSettings, getLocation, getLocationsMeta, getColors],
(data, extent, settings, location, meta, colors) => {
if (!data || isEmpty(data) || !meta || isEmpty(meta)) return null;
const dataMapped = [];
data.forEach(d => {
const region = meta.find(l => d.id === l.value);
if (region) {
const locationExtent = extent.filter(l => l.id === d.id);
const percentage = d.gain / locationExtent[0].extent * 100;
dataMapped.push({
label: (region && region.label) || '',
gain: d.gain,
percentage,
value: settings.unit === 'ha' ? d.gain : percentage,
path: `/country/${location.country}/${
location.region ? `${location.region}/` : ''
}${d.id}`,
color: colors.main
});
}
});
return sortByKey(uniqBy(dataMapped, 'label'), 'value', true);
}
);
export const getChartData = createSelector([getSortedData], data => {
if (!data || !data.length) return null;
const topRegions = data.length > 10 ? data.slice(0, 10) : data;
const totalGain = sumBy(data, 'gain');
const otherRegions = data.length > 10 ? data.slice(10) : [];
const othersGain = otherRegions.length && sumBy(otherRegions, 'gain');
const otherRegionsData = otherRegions.length
? {
label: 'Other regions',
percentage: othersGain ? othersGain / totalGain * 100 : 0,
color: otherRegions[0].color
}
: {};
return [...topRegions, otherRegionsData];
});
export const getSentence = createSelector(
[
getSortedData,
getSettings,
getOptions,
getLocation,
getIndicator,
getLocationNames
],
(data, settings, options, location, indicator, locationNames) => {
if (!data || !options || !indicator || !locationNames) return '';
const totalGain = sumBy(data, 'gain');
const currentLocation =
locationNames && locationNames.current && locationNames.current.label;
const topRegion = data.length && data[0];
const avgGainPercentage = sumBy(data, 'percentage') / data.length;
const avgGain = sumBy(data, 'gain') / data.length;
let percentileGain = 0;
let percentileLength = 0;
let sentence = '';
if (indicator.value !== 'gadm28') {
sentence += `For <b>${indicator.label}</b> in <b>${
currentLocation
}</b>, `;
} else {
sentence += `In <b>${currentLocation}</b>, `;
}
while (
(percentileLength < data.length && percentileGain / totalGain < 0.5) ||
(percentileLength < 10 && data.length > 10)
) {
percentileGain += data[percentileLength].gain;
percentileLength += 1;
}
const topGain = percentileGain / totalGain * 100;
if (percentileLength > 1) {
sentence += `the top <b>${
percentileLength
}</b> regions were responsible <b>`;
} else {
sentence += `<b>${topRegion.label}</b> was responsible <b>`;
}
if (!location.region) {
sentence += `more than half (${format('.0f')(topGain)}%)`;
} else {
sentence += `${format('.0f')(topGain)}%`;
}
sentence += '</b> of all region tree cover gain. ';
sentence += `${
percentileLength > 1 ? `<b>${topRegion.label}</b>` : 'This region'
} has the largest tree cover gain at `;
if (topRegion.percentage > 1 && settings.unit === '%') {
sentence += `<b>${format('.0f')(
topRegion.percentage
)}%</b> compared to an average of <b>${format('.0f')(
avgGainPercentage
)}%</b>.`;
} else {
sentence += `<b>${format('.3s')(
topRegion.gain
)}ha</b> compared to an average of <b>${format('.3s')(avgGain)}ha</b>.`;
}
return sentence;
}
);