-
Notifications
You must be signed in to change notification settings - Fork 97
Feature/future carbon gains #3681
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
443476f
futureCarbonGains data requests & template files
dfrico f03ffca
working with data structure
dfrico 1fe556d
Fixing response data structure, removing req status validation
dfrico 427ab39
Widget showing stacked coloured bars
dfrico 6addeb0
Carbon gains widget sentence and parameters
dfrico edf2359
Using correct colour palette and units
dfrico a5fa6d3
Widget settings now allow to switch between C and CO2 gains
dfrico 8ee6e45
Fixed small bug in tooltip
dfrico 6cd6210
Merge branch 'develop' of github.com:Vizzuality/gfw into feature/futu…
edbrett bfb3462
Changed carbon dioxide to CO2 and added format to units in tooltip
dfrico 2bfefc3
futureCarbonGains countries whitelist
dfrico d398c3e
Small requested changes
dfrico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/javascript/components/widgets/widgets/climate/future-carbon-gains/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import axios from 'axios'; | ||
import { getEmissions } from 'services/climate'; | ||
|
||
export default ({ params }) => | ||
axios.all([...getEmissions({ ...params })]).then( | ||
axios.spread( | ||
( | ||
cYSF, | ||
cMASF, | ||
cPasture, | ||
cCrops, | ||
co2YSF, | ||
co2MASF, | ||
co2Pasture, | ||
co2Crops | ||
) => { | ||
const data = { | ||
cGain: { | ||
YSF: cYSF.data && cYSF.data.values, | ||
MASF: cMASF.data && cMASF.data.values, | ||
Pasture: cPasture.data && cPasture.data.values, | ||
Crops: cCrops.data && cCrops.data.values | ||
}, | ||
co2Gain: { | ||
YSF: co2YSF.data && co2YSF.data.values, | ||
MASF: co2MASF.data && co2MASF.data.values, | ||
Pasture: co2Pasture.data && co2Pasture.data.values, | ||
Crops: co2Crops.data && co2Crops.data.values | ||
} | ||
}; | ||
return data; | ||
} | ||
) | ||
); |
25 changes: 25 additions & 0 deletions
25
app/javascript/components/widgets/widgets/climate/future-carbon-gains/config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export default { | ||
widget: 'futurecarbongains', | ||
title: { | ||
initial: 'Annual tree cover loss by dominant driver in {location}', | ||
global: 'Global annual tree cover loss by dominant driver' | ||
}, | ||
categories: ['climate'], | ||
colors: 'climate', | ||
types: ['country', 'region'], | ||
admins: ['adm0', 'adm1', 'adm2'], | ||
options: { | ||
units: ['co2Gain', 'cGain'] | ||
}, | ||
layers: ['fffa76d3-5008-48b7-afeb-2c7054548f2e'], | ||
metaKey: 'potential_tree_biomass_gain', | ||
sortOrder: { | ||
summary: 1, | ||
forestChange: 1, | ||
global: 1 | ||
}, | ||
sentences: { | ||
initial: | ||
'In {location}, potential carbon sequestration may reach {amount} of {variable} by {maxYear}.' | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
7
app/javascript/components/widgets/widgets/climate/future-carbon-gains/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Component from 'components/widgets/components/widget-composed-chart'; | ||
import getData from './actions'; | ||
import getProps from './selectors'; | ||
import config from './config'; | ||
import settings from './settings'; | ||
|
||
export { getData, getProps, Component, config, settings }; |
110 changes: 110 additions & 0 deletions
110
app/javascript/components/widgets/widgets/climate/future-carbon-gains/selectors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { createSelector, createStructuredSelector } from 'reselect'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import { yearTicksFormatter } from 'components/widgets/utils/data'; | ||
import { formatNumber } from 'utils/format'; | ||
|
||
const getData = state => state.data || null; | ||
const getSettings = state => state.settings || null; | ||
const getLocationName = state => state.locationName || null; | ||
const getSentences = state => state.config && state.config.sentences; | ||
const getColors = state => state.colors || null; | ||
|
||
export const parseData = createSelector( | ||
[getData, getSettings], | ||
(data, settings) => { | ||
if (isEmpty(data)) return null; | ||
const years = {}; | ||
const selectedData = data[settings.unit]; | ||
Object.keys(selectedData).forEach(key => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could use a reduce here to make this simpler. |
||
selectedData[key].forEach(obj => { | ||
if (years[obj.year]) years[obj.year][key] = obj.value; | ||
else years[obj.year] = { year: obj.year, [key]: obj.value }; | ||
}) | ||
); | ||
return Object.values(years); | ||
} | ||
); | ||
|
||
export const parseConfig = createSelector( | ||
[getData, getSettings, getColors], | ||
(data, settings, colors) => { | ||
if (isEmpty(data)) return null; | ||
const selectedData = data[settings.unit]; | ||
const yKeys = {}; | ||
Object.keys(selectedData).forEach((k, i) => { | ||
yKeys[k] = { | ||
fill: colors.ramp && colors.ramp[i], | ||
stackId: 1 | ||
}; | ||
}); | ||
let tooltip = [ | ||
{ | ||
key: 'year' | ||
} | ||
]; | ||
const labels = { | ||
YSF: 'Young Secondary Forest', | ||
MASF: 'Mid-Age Secondary Forests' | ||
}; | ||
tooltip = tooltip.concat( | ||
Object.keys(selectedData) | ||
.map((k, i) => ({ | ||
key: k, | ||
label: labels[k] ? labels[k] : k, | ||
color: colors.ramp && colors.ramp[i] | ||
})) | ||
.reverse() | ||
); | ||
return { | ||
height: 250, | ||
xKey: 'year', | ||
yKeys: { | ||
bars: yKeys | ||
}, | ||
xAxis: { | ||
ticksFormatter: yearTicksFormatter | ||
}, | ||
tooltip | ||
}; | ||
} | ||
); | ||
|
||
export const parseSentence = createSelector( | ||
[getSettings, getLocationName, getSentences, parseData], | ||
(settings, location, sentences, parsedData) => { | ||
if (isEmpty(parsedData)) return null; | ||
const maxYear = parsedData[parsedData.length - 1]; | ||
const amount = Object.values(maxYear).reduce( | ||
(acc, n) => acc + n, | ||
-maxYear.year | ||
); | ||
const variables = { | ||
cGain: 'carbon', | ||
co2Gain: 'carbon dioxide' | ||
}; | ||
const variable = variables[settings.unit]; | ||
const { initial } = sentences; | ||
|
||
return { | ||
sentence: initial, | ||
params: { | ||
location, | ||
amount: formatNumber({ num: amount * 1000000, unit: 't' }), | ||
variable, | ||
maxYear: maxYear.year | ||
} | ||
}; | ||
} | ||
); | ||
|
||
export const parseTitle = createSelector( | ||
[], | ||
() => 'Potential tree biomass gain' | ||
); | ||
|
||
export default createStructuredSelector({ | ||
data: parseData, | ||
dataConfig: parseConfig, | ||
sentence: parseSentence, | ||
title: parseTitle | ||
}); |
3 changes: 3 additions & 0 deletions
3
app/javascript/components/widgets/widgets/climate/future-carbon-gains/settings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
unit: 'co2Gain' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import request from 'utils/request'; | ||
|
||
const REQUEST_URL = | ||
'http://climate.globalforestwatch.org/api/indicators/{indicator}?thresh={thresh}&iso={iso}&id_1={id}&area={area}'; | ||
|
||
const INDICATORS = [ | ||
3110, // (Carbon) Young Secondary Forest | ||
3111, // (Carbon) Mid-Age Secondary Forests | ||
3112, // (Carbon) Pasture | ||
3113, // (Carbon) Crops | ||
3114, // C02 Young Secondary Forest | ||
3115, // C02 Mid-Age Secondary Forests | ||
3116, // C02 Pasture | ||
3117 // C02 Crops | ||
]; | ||
|
||
export const getEmissions = ({ threshold, adm0, adm1, adm2 }) => | ||
INDICATORS.map(indicator => { | ||
const url = REQUEST_URL.replace('{indicator}', indicator) | ||
.replace('{thresh}', threshold || '0') | ||
.replace('{iso}', adm0 ? String(adm0) : '') | ||
.replace('{id}', adm1 ? String(adm1) : '') | ||
.replace('{area}', adm2 ? String(adm1) : ''); | ||
return request.get(url); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we camelCase this like other widgets?