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

Fix cover located list #3437

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ export const getSentence = createSelector(
let percentileLength = 0;

while (
(percentileLength < data.length && percentileCount / totalCount < 0.5) ||
(percentileLength < 10 && data.length > 10)
percentileLength < data.length &&
percentileCount / totalCount < 0.5 &&
data.length !== 10
) {
percentileCount += data[percentileLength].count;
percentileLength += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ export const getSentence = createSelector(
let percentileLength = 0;

while (
(percentileLength < data.length && percentileGain / totalGain < 0.5) ||
(percentileLength < 10 && data.length > 10)
percentileLength < data.length &&
percentileGain / totalGain < 0.5 &&
data.length !== 10
) {
percentileGain += data[percentileLength].gain;
percentileLength += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ export const getSentence = createSelector(
let percentileLength = 0;

while (
(percentileLength < data.length && percentileLoss / totalLoss < 0.5) ||
(percentileLength < 10 && data.length > 10)
percentileLength < data.length &&
percentileLoss / totalLoss < 0.5 &&
data.length !== 10
) {
percentileLoss += data[percentileLength].loss;
percentileLength += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ export default {
},
sentences: {
globalInitial:
'{location}, {count} regions represent {percentage} of all tree cover. {region} has the largest tree cover at {value} compared to an average of {average}.',
globalHasPercentage:
'{location}, {region} had the largest relative tree cover of {value}, compared to a national average of {average}.',
globalHasIndicator:
'Within For {indicator} {location}, {count} regions represent {percentage} of all tree cover. {region} has the largest tree cover at {value} compared to an average of {average}.',
'{location} as of {year}, the top {count} countries represent {percentage} of all tree cover. {region} had the most tree cover at {value} compared to an average of {average}.',
globalWithIndicator:
'{location} as of {year}, the top {count} countries represent {percentage} of all tree cover in {indicator}. {region} had the most tree cover at {value} compared to an average of {average}.',
initial:
'In {location}, {count} regions represent {percentage} of all tree cover. {region} has the largest tree cover at {value} compared to an average of {average}.',
'In {location} as of {year}, the top {count} regions represent {percentage} of all tree cover. {region} had the most tree cover at {value} compared to an average of {average}.',
hasIndicator:
'For {indicator} in {location}, {count} regions represent {percentage} of all tree cover. {region} has the largest tree cover at {value} compared to an average of {average}.',
hasPercentage:
'{region} had the largest relative tree cover of {value}, compared to a regional average of {average}.',
'In {location} as of {year}, the top {count} regions represent {percentage} of all tree cover in {indicator}. {region} had the most tree cover at {value} compared to an average of {average}.',
percGlobalInitial:
'{location} as of {year}, the top {count} countries represent {percentage} of all tree cover. {region} had the most relative tree cover at {value} compared to an average of {average}.',
percGlobalWithIndicator:
'{location} as of {year}, the top {count} countries represent {percentage} of all tree cover in {indicator}. {region} had the most relative tree cover at {value} compared to an average of {average}.',
percInitial:
'In {location} as of {year}, the top {count} regions represent {percentage} of all tree cover. {region} had the most relative tree cover at {value} compared to an average of {average}.',
percHasIndicator:
'In {location} as of {year}, the top {count} regions represent {percentage} of all tree cover in {indicator}. {region} had the most relative tree cover at {value} compared to an average of {average}.',
noCover: 'No tree cover was identified in {location}.'
},
data: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getCurrentLocation = state => state.currentLabel || null;
const getColors = state => state.colors || null;
const getSentences = state => state.config && state.config.sentences;

export const parseData = createSelector(
export const parseList = createSelector(
[getData, getSettings, getLocation, getLocationsMeta, getColors],
(data, settings, location, meta, colors) => {
if (isEmpty(data) || isEmpty(meta)) return null;
Expand All @@ -37,12 +37,18 @@ export const parseData = createSelector(
});
}
});
return sortByKey(uniqBy(dataMapped, 'label'), 'value', true);
return sortByKey(dataMapped, 'extent', true);
}
);

export const parseData = createSelector([parseList], data => {
if (isEmpty(data)) return null;
return sortByKey(uniqBy(data, 'label'), 'value', true);
});

export const getSentence = createSelector(
[
parseList,
parseData,
getSettings,
getOptions,
Expand All @@ -51,15 +57,26 @@ export const getSentence = createSelector(
getCurrentLocation,
getSentences
],
(data, settings, options, location, indicator, currentLabel, sentences) => {
(
list,
data,
settings,
options,
location,
indicator,
currentLabel,
sentences
) => {
if (!data || !options || !currentLabel) return null;
const {
hasPercentage,
initial,
hasIndicator,
globalInitial,
globalHasPercentage,
globalWithIndicator,
percInitial,
percHasIndicator,
percGlobalInitial,
percGlobalWithIndicator,
noCover
} = sentences;
const topRegion = data.length && data[0];
Expand All @@ -69,11 +86,11 @@ export const getSentence = createSelector(
let percentileExtent = 0;
let percentileLength = 0;
while (
(percentileLength < data.length &&
percentileExtent / totalExtent < 0.5) ||
(percentileLength < 10 && data.length > 10)
percentileLength < data.length &&
percentileExtent / totalExtent < 0.5 &&
data.length !== 10
) {
percentileExtent += data[percentileLength].extent;
percentileExtent += list[percentileLength].extent;
percentileLength += 1;
}
const topExtent = percentileExtent / (totalExtent || 0) * 100;
Expand All @@ -83,6 +100,7 @@ export const getSentence = createSelector(
region: topRegion.label,
indicator: indicator && indicator.label.toLowerCase(),
percentage: topExtent ? `${format('.2r')(topExtent)}%` : '0%',
year: settings.extentYear,
value:
settings.unit === '%'
? `${format('.2r')(topRegion.percentage)}%`
Expand All @@ -91,19 +109,24 @@ export const getSentence = createSelector(
settings.unit === '%'
? `${format('.2r')(avgExtentPercentage)}%`
: `${format('.3s')(avgExtent)}ha`,
count: percentileLength,
metric: settings.unit === '%' ? 'relative tree cover' : 'tree cover'
count: percentileLength
};

let sentence = currentLabel === 'global' ? globalInitial : initial;
if (settings.unit === '%') {
sentence =
currentLabel === 'global' ? globalHasPercentage : hasPercentage;
} else if (indicator) {
sentence = currentLabel === 'global' ? globalWithIndicator : hasIndicator;
}
if (params.percentage === '0%') {
sentence = noCover;
let sentence = noCover;
if (params.percentage !== '0%' && settings.unit === '%') {
sentence = currentLabel === 'global' ? percGlobalInitial : percInitial;
if (indicator) {
sentence =
currentLabel === 'global'
? percGlobalWithIndicator
: percHasIndicator;
}
} else if (params.percentage !== '0%' && settings.unit === 'ha') {
sentence = currentLabel === 'global' ? globalInitial : initial;
if (indicator) {
sentence =
currentLabel === 'global' ? globalWithIndicator : hasIndicator;
}
}

return {
Expand Down