I am using d3 to make some pretty graphs, all of the same raw data. This raw data, however, contains arrays.
var data = [
{name: "Alfred", age: "27", medication: [{name: "Aspirin", dose: "14", unit: "tablets"}]},
{name: "Brian", age: "62", medication: []},
{name: "Chris", age: "46", medication: [{name: "Bisoprolol", dose: "3", unit: "ml"}, {name: "Clotrimazol", dose: "2", unit: "mg"}]},
{name: "David", age: "68", medication: [{name: "Aspirin", dose: "4", unit: "tablets"}, {name: "Bisoprolol", dose: "1.5", unit: "ml"}, {name: "Clotrimazol", dose: "2", unit: "mg"}]}
]
Now obviously, I can easily group those by, for example, age or name.
The problem arises when I want to show medications (by name), as any patient can have one, none, or several of those. Each patient can therefore be in none or several groups. So far, I see three ways of doing this:
- duplicate patients with more than one medication, each copy with a different single medication
This creates redundancies, also I want draw a whole barrage of diagrams from the same data. The key for nesting (the function that returns the value to group by for each patient) will be created dynamically from a user interface - as it is a getter, not a setter, I would need to figure out how to reset medication for each copy, too. The copying might, depending on the complexity of my raw data, also not be trivial.
- Concatenate medication names in the key function (with a unique separator) and split and add as needed when drawing.
Every occurring combination of medications will have its own group, and I would need to calculate sums from all groups containing a specific medication to get the total of it's frequency. This seems like a bad idea.
- Write my own nest function that handles arrays.
I would lose d3's rollup and sort functionality unless I implement that, too, from scratch.
Is there a better way?