i have a json as shared below and I am trying to nest the data prior to visualization. I wish to plot under gov and non-gov how every vendor is distributed for high and low projects so trying to create a json object like [{v1: {Gov: {high:3, low:2}, {Non-Gov: {high:12, low:1}}}, {v2:{Gov: {high:3, low:2}, {Non-Gov: {high:12, low:1}}}, ...]
I am able to nest the data but unable to get the respective counts. Any guidance is appreciated. Apologies if the question construct is vague.
[
{
"vendor": "V1",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V2",
"ptype": "Gov",
"critical": "low"
},
{
"vendor": "V3",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V4",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V5",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V6",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V7",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V8",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V9",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V10",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V1",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V2",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V3",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V4",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V5",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V6",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V7",
"ptype": "Gov",
"critical": "low"
},
{
"vendor": "V8",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V9",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V10",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V1",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V2",
"ptype": "Gov",
"critical": "low"
},
{
"vendor": "V3",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V4",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V5",
"ptype": "Non-Gov",
"critical": "high"
},
{
"vendor": "V6",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V7",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V8",
"ptype": "Non-Gov",
"critical": "low"
},
{
"vendor": "V9",
"ptype": "Gov",
"critical": "high"
},
{
"vendor": "V10",
"ptype": "Gov",
"critical": "low"
}
]
without rollup
n = d3.nest().key(function(d){return d.vendor;}) .key(function(d){return d.ptype;}) .key(function(d){return d.critical;}) .entries(j);
with rollup
n = d3.nest().key(function(d){return d.vendor;}) .key(function(d){return d.ptype;}) .key(function(d){return d.critical;}) .rollup(function(leaf){ return[ {key:'GH', value:leaf[0].values.length} ,{key:'GL',value:leaf[1].values.length} ]}) .entries(j);
i could do it this way..
Thanks!
I don't know D3, but using vanilla JS you can transform the data as follows:
Or if you can use ES6 syntax you can make the
.map()
part a lot shorter:EDIT: It occurred to me that maybe you don't want to hard-code the possible
ptype
andcritical
values, so...the following (ugly, unoptimsed) version grabs the values as it goes: