I was working on this code and thought, I really think that could be a recursive function.
But also new that converting it would take me a lot of learning and work.
{
let year = []
for (const [nameYear, year_value] of so.entries()) {
let body = []
for (const [nameBody, body_value] of year_value.entries()) {
let motor = []
for (const [nameMotor, motor_value] of body_value.entries()) {
motor.push({ name:nameMotor, value: motor_value.length})
}
body.push({ name:nameBody, children: motor})
}
year.push({name:nameYear,children:body});
}
return year
}
So I asked chatGPT if it could be optimized into a recursive functions.
And it say, yes and here is how it would look.
function rollupData(map) {
let result = [];
for (const [key, value] of map.entries()) {
if (value instanceof Map) {
result.push({ name: key, children: rollupData(value) });
} else {
result.push({ name: key, value: value.length });
}
}
return result;
}
I only was working with three levels in my tree but this code means that ‘so’ can have as many levels as I desire or in any order.
so = d3.group(tsco,
d=> d.Year_of_Registration_date,
d=> d.Body_type,
d=> d.Motor_tech,
d=> d.CO2_band
)
// Year_of_Registration_date
// Month_of_Registration_date
// Body_type
// Import_status
// Motor_tech
// Purchase_region
// Vehicle_class
// CO2_band
// Country
// Engine_vol
// GVM
// Purchase_cust_type
// Veh_Mfr
// CO2_WLTP
The data it rolls works with this. Zoomable Treemap / D3 | Observable