I'll give an answer with vanilla js and I'll try to come back and give a d3 answer once I've read enough of the docs.
You can do it like this:
csv:
DateTime, A, B, C
2020/2/27 7:16, 3, 0, 1
2020/2/26 23:44, 1, 0, 4
2020/2/26 21:35, 1, 1, 0
2020/2/26 15:14, 1, 0, 3
2020/2/25 20:35, 1, 1, 0
2020/2/25 16:10, 1, 0, 3
Which we could convert to a JSON / JavaScript array many ways, such as:
Taken from: https://stackoverflow.com/a/61080978/9792594:
const csv2json = (str, delimiter = ',') => {
const titles = str.slice(0, str.indexOf('\n')).split(delimiter);
const rows = str.slice(str.indexOf('\n') + 1).split('\n');
return rows.map(row => {
const values = row.split(delimiter);
return titles.reduce((object, curr, i) => (object[curr.trim()] = values[i].trim(), object), {})
});
};
let csv =
`DateTime, A, B, C
2020/2/27 7:16, 3, 0, 1
2020/2/26 23:44, 1, 0, 4
2020/2/26 21:35, 1, 1, 0
2020/2/26 15:14, 1, 0, 3
2020/2/25 20:35, 1, 1, 0
2020/2/25 16:10, 1, 0, 3`;
let word_array = csv2json(csv,',');
console.log(word_array)
.as-console-wrapper { max-height: 100% !important; top: 0; }
JSON / JavaScript Array:
[
{
"DateTime": "2020/2/27 7:16",
"A": 3,
"B": 0,
"C": 1
},
{
"DateTime": "2020/2/26 23:44",
"A": 1,
"B": 0,
"C": 4
},
{
"DateTime": "2020/2/26 21:35",
"A": 1,
"B": 1,
"C": 0
},
{
"DateTime": "2020/2/26 15:14",
"A": 1,
"B": 0,
"C": 3
},
{
"DateTime": "2020/2/25 20:35",
"A": 1,
"B": 1,
"C": 0
},
{
"DateTime": "2020/2/25 16:10",
"A": 1,
"B": 0,
"C": 3
}
]
Answer to your question (aggregate data based on date):
- use reduce, to create an object with each day as a key (parse date)
- when the key already exists, merge the values on A, B, C
- convert Object to an array with
Object.values
const myData = [{"DateTime":"2020/2/27 7:16","A":3,"B":0,"C":1},{"DateTime":"2020/2/26 23:44","A":1,"B":0,"C":4},{"DateTime":"2020/2/26 21:35","A":1,"B":1,"C":0},{"DateTime":"2020/2/26 15:14","A":1,"B":0,"C":3},{"DateTime":"2020/2/25 20:35","A":1,"B":1,"C":0},{"DateTime":"2020/2/25 16:10","A":1,"B":0,"C":3}];
const groupedData = Object.values(myData.reduce((aggObj, item) => {
const dateOnly = new Date(item.DateTime).toLocaleDateString();
if (aggObj.hasOwnProperty(dateOnly)){
Object.entries(aggObj[dateOnly]).forEach(([key,val]) => {
if (key != 'DateTime'){
aggObj[dateOnly][key] = val + item[key];
}
});
} else {
aggObj[dateOnly] = {...item, "DateTime": dateOnly};
}
return aggObj;
}, {}));
console.log(groupedData);
.as-console-wrapper { max-height: 100% !important; top: 0; }