I am new to JavaScript and D3.js and atm trying to create an Angular App with a stacked bar chart visualisation of the number of different test results 'OK', 'NOK' and 'Aborted' of each day as a stacked bar in the y-axis and the dates as the x-axis.
My data.csv looks like this:
Date;Result
20-05-2021 17:54:02;Aborted
20-05-2021 17:55:24;OK
21-05-2021 21:48:45;NOK
22-05-2021 17:55:24;OK
22-05-2021 17:54:02;Aborted
22-05-2021 17:55:24;OK
Since I need to count the results per day I first parse the date into the right format using timeParse and then I use the timeDay.floor method to get rid of the time:
let jsonObj = await d3.dsv(";", "/assets/data.csv", function (d) {
let time = timeParse("%d-%m-%Y %-H:%M:%S")(d['Date'])
let date = timeDay.floor(new Date(time));
return {
Date: date,
Result: d.Result
};
})
If I understand correctly, this gives me an array with < date | string > for every test result.
To now summarize the counts of the test results of identical days I use rollup:
let data_count = rollup(jsonObj, v => v.length, d => d.Date, d => d.Result)
Now I have a nested Map with the date (only one for each day) as key and the values as the test result each with the summed number for that day.
I foud several examples on how to continue that don't need to use the rollup method e.g. here and tried to adapt it to my map:
let processed_data = data_count.map( d => {
let y0 = 0;
let total = 0;
return {
date: d.key,
//call second mapping function on every test-result
values: (d.valules).map( d => {
let return_object = {
result: d.key,
count: d.values,
y0: y0,
y1: y0 + d.values
};
//calculate the updated y0 for each new test-result on a given date
y0 = y0 + d.values;
//add the total for a given test-result to the sum total for that test-result
total = total + d.values;
return return_object;
}),
total: total
};
});
but I am getting an error:
Property 'map' does not exist on type 'InternMap<Date, InternMap<string, number>>'.ts(2339)
I understand that the map function can't be used on maps, I guess. I also tried to rewrite this part into separate functions to not use the map function but it doesn't work either. Maybe I have some syntax error or something but I get:
TypeError: Cannot read property 'key' of undefined
I probably need to use the get() method for the values of a map, but not sure how to implement it.
Now I am stuck as to how I should continue to prepare the data for the stacked bar chart. In this example from bl.ocks.org the CSV looks different. I was thinking about somehow manipulating my data to fit that example's shape:
Date;OK;NOK;Aborted
20-05-2021 17:54:02;1;0;1
21-05-2021 21:48:45;0;1;0
22-05-2021 17:55:24;2;0;1
But I have no idea how to go about it. Any help as to how to prepare my data would be welcome. Maybe I should not use the rollup method, but I feel like it's a perfect fit for my needs.