Last active
April 9, 2018 10:19
-
-
Save zymr-keshav/a1bc7b95701e129099339a8514c33ed6 to your computer and use it in GitHub Desktop.
generate chartjs data from response which comes as key and data and turn into lables and data
This file contains hidden or 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
remakeChartData(input) { | |
const output = {}; | |
input.map((item) => { | |
for (let key in item) { | |
if (!(key in output)) { | |
output[key] = []; | |
} | |
output[key].push(item[key]); | |
} | |
}); | |
const entries = (<any>Object).entries(output); // in case of typesctipt otherwise remove <any> and () | |
return entries.map((e) => { | |
let label = e[0]; | |
// make first letter uppercase | |
label = `${label.charAt(0).toUpperCase()}${label.substring(1)}`; | |
let data = e[1]; | |
return { label, data }; | |
}); | |
} | |
input: | |
[ | |
{ | |
"alpha": 10, | |
"beta": 11 | |
}, | |
{ | |
"alpha": 0, | |
"beta": 4 | |
}, | |
{ | |
"alpha": 21, | |
"beta": 9 | |
}, | |
{ | |
"alpha": 3, | |
"beta": 1 | |
}, | |
{ | |
"alpha": 7, | |
"beta": 0 | |
}, | |
{ | |
"alpha": 0, | |
"beta": 0 | |
} | |
] | |
output: | |
[ | |
{ | |
"label": "Alpha", | |
"data": [10, 0, 21, 3, 7, 0 ] | |
}, | |
{ | |
"label": "Beta", | |
"data": [11, 4, 9, 1, 0, 0 ] | |
} | |
] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment