I am working on a d3.js gannt chart – and I’ve got the following data set coming out – but I need to flatten the array by date, then also group it by type — so there is one entry for the starting/ending time and refactor a tool tip textual parameter that can showcase the grouped information
So like on hovering on a bar — it reads (Device: pH, Humidity)(Report: Swelling)(Clinician Assessment: Pain Level)
What would be the best approach to refactoring this data set to something as mentioned.
let array = [
{text: "pH", starting_time: 1693347129000, ending_time: 1693350729000, type: "device"},
{text: "humidity", starting_time: 1693347129000, ending_time: 1693350729000, type: "device"},
{text: "pH", starting_time: 1693339929000, ending_time: 1693343529000, type: "device"},
{text: "pH", starting_time: 1692958329000, ending_time: 1692961929000, type: "device"},
{text: "humidity", starting_time: 1692958329000, ending_time: 1692961929000, type: "device"},
{text: "pH", starting_time: 1692738729000, ending_time: 1692742329000, type: "device"},
{text: "humidity", starting_time: 1692738729000, ending_time: 1692742329000, type: "device"},
{text: "temperature", starting_time: 1692735129000, ending_time: 1692738729000, type: "device"},
{text: "pH", starting_time: 1692735129000, ending_time: 1692738729000, type: "device"},
{text: "humidity", starting_time: 1692213729000, ending_time: 1692217329000, type: "device"},
{text: "humidity", starting_time: 1692130929000, ending_time: 1692134529000, type: "device"},
{text: "pain level", starting_time: 1695972618000, ending_time: 1695976218000, type: "report"},
{text: "swelling", starting_time: 1695597028000, ending_time: 1695600628000, type: "report"},
{text: "level of pain", starting_time: 1695597028000, ending_time: 1695600628000, type: "clinicianAssessment"}
]
would it be something like this
function alertBreakdown(result){
let schedule = [...result];
//flat all the dates to one array
const timestamps = schedule.flatMap(_ => _.starting_time);
//flatten also for ending_time? so if same starting/ending time?
//console.log("timestamps", timestamps);
//count the occurrences of each date in a dictionary structure
const day2count = {};
timestamps.forEach(timestamp => {
//console.log("--<timestamp", timestamp);
const day = moment(timestamp).format("YYYY-MM-DD");//.toString().split('T')[0];
//const day = timestamp.toString().split('T')[0];
day2count[day] = (day2count[day] || 0) + 1;
});
//console.log("day2count", day2count);
// convert the map to an array of entries
const scheduleResult = Object.entries(day2count).map(([date, value]) => ({date: moment(date), value: value, toolTipText: "test" }))
.sort((a,b) => moment(a.date) - moment(b.date))
return scheduleResult;
}