I'm using ChartKick to represent some data in the graphs in Multiple series line chart. The required format is:
let data = [
{ name: 'sharry', data: { '2008-08-02': 10006097, '2018-08-03': 10670, etc }},
{ name: 'stuart', data: { '2018-08-01' :1401231321, '2018-08-04': 2500000000, etc }}
];
The data I get from the database is:
let user_usage = [
{ "name":"stuart", "data": { "2018-08-02":10006097 } },
{ "name":"stuart", "data": { "2018-08-03":10670 } },
{ "name":"stuart", "data": { "2018-08-05":5800000 } },
{ "name":"stuart", "data": { "2018-08-07":10789000 } },
{ "name":"stuart", "data": { "2018-08-08":1033000000 } },
{ "name":"sharry", "data": { "2018-08-01":1401231321 } },
{ "name":"sharry", "data": { "2018-08-04":2500000000 } }
];
I've been trying for 2 days to get the database response into the required format above Any help greatly appreciated guys. Here is what I have so far. I'm using Ramda but ES6 is fine to use:
let chartdata = [];
let u = R.groupBy(R.prop('name'))(user_usage);
Object.keys(u).forEach((name, index) => {
chartdata.push({ name , data: {} });
let usage = u[name];
Object.keys(usage).forEach(row => {
let data = usage[row].data;
let date = Object.keys(data)[0];
let total = data[Object.keys(data)[0]];
chartdata[index].data = Object.assign({}, {`${date}: ${total}`};
});
});