I need to transform an object array with data in another object array in order to plot a chart with the library Plotly.
This is the input format:
var input = [
{
"COUNT_DEVICES": 48,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "7 days"
},
{
"COUNT_DEVICES": 25,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "7 days"
},
{
"COUNT_DEVICES": 73,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "15 days"
},
{
"COUNT_DEVICES": 57,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "15 days"
},
{
"COUNT_DEVICES": 3,
"PRODUCT_ID": "16756",
"TIME_OFFLINE": "15 days"
},
{
"COUNT_DEVICES": 91,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "30 days"
},
{
"COUNT_DEVICES": 69,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "30 days"
},
{
"COUNT_DEVICES": 139,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "60 days"
},
{
"COUNT_DEVICES": 99,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "60 days"
},
{
"COUNT_DEVICES": 280,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "90 days"
},
{
"COUNT_DEVICES": 262,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "90 days"
},
{
"COUNT_DEVICES": 61,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "180 days"
},
{
"COUNT_DEVICES": 323,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "180 days"
},
{
"COUNT_DEVICES": 3181,
"PRODUCT_ID": "16054",
"TIME_OFFLINE": "360 days"
},
{
"COUNT_DEVICES": 384,
"PRODUCT_ID": "10485",
"TIME_OFFLINE": "360 days"
},
{
"COUNT_DEVICES": 1,
"PRODUCT_ID": "3073",
"TIME_OFFLINE": "360 days"
}
]
And this is the output format:
output = [
{
x: ["16054","10485", "3073"],
y: [3181,384,1],
name: "360 days"
type: 'bar'
},
{
x: ["16054","10485"],
y: [661,323],
name: "180 days"
type: 'bar'
},
{
x: ["16054","10485"],
y: [280,262],
name: "90 days"
type: 'bar'
},
{
x: ["16054","10485"],
y: [139,99],
name: "60 days"
type: 'bar'
},
{
x: ["16054","10485"],
y: [91,69],
name: "30 days"
type: 'bar'
},
{
x: ["16054","10485","16756"],
y: [73,57,3],
name: "15 days"
type: 'bar'
}
]
So I am looking to encapsulate this as a generical function:
function( input, x_label, y_label, name_label)
and this case it will be:
function(input, 'PRODUCT_ID', 'COUNT_DEVICES', 'TIME_OFFLINE')
It will be the base to take the input data to general charts as bar charts or others kind of plots