File size: 1,260 Bytes
7f88cfb | 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 | import * as d3Fetch from 'd3-fetch'
import {
capitalizeFirstLetter,
getImportExportSuffix
} from '../helpers/helpers'
const countriesLabels = {
usa: 'USA',
hkg: 'Hong Kong',
row: 'Rest of World',
china: "China's global trade surplus"
}
const order = ['usa', 'hkg', 'row']
async function parseData(url) {
let data = await d3Fetch
.csv(url, d => {
for (var i in d) {
d[i] = +d[i]
}
return d
})
.then(data => {
const groups = data.columns
.filter(d => d !== 'year')
.map(d => ({
label: countriesLabels[d.split('_')[0]],
id: d,
group: d.split('_')[0],
type: getType(d),
suffix: getImportExportSuffix(d, true)
}))
const sortedGroups = groups.reduce((acc, curr) => {
acc[curr.type] = acc[curr.type] || []
acc[curr.type].push(curr)
acc[curr.type].sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id))
return acc
}, {})
let dataset = {
values: data,
groups: groups,
sortedGroups: sortedGroups
}
return dataset
})
return data
}
function getType(group) {
return capitalizeFirstLetter(group.split('_')[1])
}
export default parseData
|