Pendigard's picture
Upload folder using huggingface_hub (part 93)
7f88cfb verified
Raw History Blame Contribute Delete
1.26 kB
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