Skip to content

Instantly share code, notes, and snippets.

@jthomassie
Created April 1, 2014 21:57
Show Gist options
  • Save jthomassie/9923962 to your computer and use it in GitHub Desktop.
Save jthomassie/9923962 to your computer and use it in GitHub Desktop.
zero inject
// zero injection
var testdata = {
"columns": [
{
"label": "Top 5 _type",
"rows": [
{
"label": "Top 5 response",
"xAxisLabel": "Daily @timestamp",
"yAxisLabel": "Count",
"series": [
{
"label": "html",
"values": [
{
"x": 1395187200000,
"y": 131
},
{
"x": 1395273600000,
"y": 140
},
{
"x": 1395360000000,
"y": 135
},
{
"x": 1395446400000,
"y": 127
},
{
"x": 1395532800000,
"y": 130
},
{
"x": 1395619200000,
"y": 145
},
{
"x": 1395705600000,
"y": 130
},
{
"x": 1395792000000,
"y": 153
},
{
"x": 1395878400000,
"y": 151
},
{
"x": 1395964800000,
"y": 117
},
{
"x": 1396051200000,
"y": 149
}
]
},
{
"label": "php",
"values": [
{
"x": 1395187200000,
"y": 102
},
{
"x": 1395273600000,
"y": 112
},
{
"x": 1395360000000,
"y": 96
},
{
"x": 1395446400000,
"y": 103
},
{
"x": 1395532800000,
"y": 109
},
{
"x": 1395619200000,
"y": 87
},
{
"x": 1395705600000,
"y": 98
},
{
"x": 1395792000000,
"y": 89
},
{
"x": 1395878400000,
"y": 97
},
{
"x": 1395964800000,
"y": 111
},
{
"x": 1396051200000,
"y": 89
}
]
},
{
"label": "png",
"values": [
{
"x": 1395187200000,
"y": 56
},
{
"x": 1395273600000,
"y": 56
},
{
"x": 1395360000000,
"y": 40
},
{
"x": 1395446400000,
"y": 59
},
{
"x": 1395532800000,
"y": 47
},
{
"x": 1395619200000,
"y": 45
},
{
"x": 1395705600000,
"y": 43
},
{
"x": 1395792000000,
"y": 56
},
{
"x": 1395878400000,
"y": 48
},
{
"x": 1395964800000,
"y": 38
},
{
"x": 1396051200000,
"y": 37
},
{
"x": 1396137600000,
"y": 1
}
]
},
{
"label": "css",
"values": [
{
"x": 1395187200000,
"y": 21
},
{
"x": 1395273600000,
"y": 42
},
{
"x": 1395360000000,
"y": 42
},
{
"x": 1395446400000,
"y": 34
},
{
"x": 1395532800000,
"y": 38
},
{
"x": 1395619200000,
"y": 28
},
{
"x": 1395705600000,
"y": 33
},
{
"x": 1395792000000,
"y": 41
},
{
"x": 1395878400000,
"y": 32
},
{
"x": 1395964800000,
"y": 29
},
{
"x": 1396051200000,
"y": 30
}
]
},
{
"label": "gif",
"values": [
{
"x": 1395187200000,
"y": 17
},
{
"x": 1395273600000,
"y": 17
},
{
"x": 1395360000000,
"y": 14
},
{
"x": 1395446400000,
"y": 20
},
{
"x": 1395532800000,
"y": 23
},
{
"x": 1395619200000,
"y": 15
},
{
"x": 1395705600000,
"y": 9
},
{
"x": 1395792000000,
"y": 16
},
{
"x": 1395878400000,
"y": 15
},
{
"x": 1395964800000,
"y": 17
},
{
"x": 1396051200000,
"y": 15
}
]
}
]
},
{
"label": "Top 5 response",
"xAxisLabel": "Daily @timestamp",
"yAxisLabel": "Count",
"series": [
{
"label": "html",
"values": [
{
"x": 1395187200000,
"y": 5
},
{
"x": 1395273600000,
"y": 5
},
{
"x": 1395360000000,
"y": 4
},
{
"x": 1395446400000,
"y": 5
},
{
"x": 1395532800000,
"y": 10
},
{
"x": 1395619200000,
"y": 6
},
{
"x": 1395705600000,
"y": 6
},
{
"x": 1395792000000,
"y": 8
},
{
"x": 1395878400000,
"y": 10
},
{
"x": 1395964800000,
"y": 5
},
{
"x": 1396051200000,
"y": 12
}
]
},
{
"label": "php",
"values": [
{
"x": 1395187200000,
"y": 5
},
{
"x": 1395273600000,
"y": 10
},
{
"x": 1395360000000,
"y": 4
},
{
"x": 1395446400000,
"y": 3
},
{
"x": 1395532800000,
"y": 5
},
{
"x": 1395619200000,
"y": 12
},
{
"x": 1395705600000,
"y": 4
},
{
"x": 1395792000000,
"y": 3
},
{
"x": 1395878400000,
"y": 7
},
{
"x": 1395964800000,
"y": 5
},
{
"x": 1396051200000,
"y": 5
}
]
},
{
"label": "png",
"values": [
{
"x": 1395187200000,
"y": 6
},
{
"x": 1395273600000,
"y": 4
},
{
"x": 1395360000000,
"y": 3
},
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395619200000,
"y": 7
},
{
"x": 1395705600000,
"y": 2
},
{
"x": 1395792000000,
"y": 1
},
{
"x": 1395878400000,
"y": 5
},
{
"x": 1396051200000,
"y": 1
}
]
},
{
"label": "css",
"values": [
{
"x": 1395187200000,
"y": 2
},
{
"x": 1395360000000,
"y": 1
},
{
"x": 1395446400000,
"y": 2
},
{
"x": 1395532800000,
"y": 2
},
{
"x": 1395619200000,
"y": 2
},
{
"x": 1395705600000,
"y": 1
},
{
"x": 1395792000000,
"y": 2
},
{
"x": 1395878400000,
"y": 2
},
{
"x": 1395964800000,
"y": 1
},
{
"x": 1396051200000,
"y": 1
}
]
},
{
"label": "gif",
"values": [
{
"x": 1395187200000,
"y": 1
},
{
"x": 1395360000000,
"y": 1
},
{
"x": 1395532800000,
"y": 1
},
{
"x": 1395792000000,
"y": 1
},
{
"x": 1395878400000,
"y": 1
},
{
"x": 1396051200000,
"y": 2
}
]
}
]
},
{
"label": "Top 5 response",
"xAxisLabel": "Daily @timestamp",
"yAxisLabel": "Count",
"series": [
{
"label": "html",
"values": [
{
"x": 1395187200000,
"y": 3
},
{
"x": 1395273600000,
"y": 1
},
{
"x": 1395360000000,
"y": 5
},
{
"x": 1395446400000,
"y": 4
},
{
"x": 1395532800000,
"y": 6
},
{
"x": 1395619200000,
"y": 6
},
{
"x": 1395705600000,
"y": 3
},
{
"x": 1395792000000,
"y": 10
},
{
"x": 1395878400000,
"y": 4
},
{
"x": 1395964800000,
"y": 2
},
{
"x": 1396051200000,
"y": 6
}
]
},
{
"label": "php",
"values": [
{
"x": 1395187200000,
"y": 3
},
{
"x": 1395273600000,
"y": 5
},
{
"x": 1395360000000,
"y": 3
},
{
"x": 1395446400000,
"y": 2
},
{
"x": 1395532800000,
"y": 3
},
{
"x": 1395619200000,
"y": 3
},
{
"x": 1395705600000,
"y": 7
},
{
"x": 1395792000000,
"y": 4
},
{
"x": 1395878400000,
"y": 2
},
{
"x": 1395964800000,
"y": 6
},
{
"x": 1396051200000,
"y": 1
}
]
},
{
"label": "png",
"values": [
{
"x": 1395187200000,
"y": 2
},
{
"x": 1395273600000,
"y": 1
},
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395532800000,
"y": 1
},
{
"x": 1395619200000,
"y": 1
},
{
"x": 1395705600000,
"y": 3
},
{
"x": 1395792000000,
"y": 3
},
{
"x": 1395878400000,
"y": 2
},
{
"x": 1395964800000,
"y": 1
},
{
"x": 1396051200000,
"y": 3
}
]
},
{
"label": "css",
"values": [
{
"x": 1395187200000,
"y": 1
},
{
"x": 1395360000000,
"y": 1
},
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395705600000,
"y": 1
},
{
"x": 1395878400000,
"y": 1
},
{
"x": 1395964800000,
"y": 4
},
{
"x": 1396051200000,
"y": 3
}
]
},
{
"label": "gif",
"values": [
{
"x": 1395273600000,
"y": 3
},
{
"x": 1395619200000,
"y": 2
},
{
"x": 1395792000000,
"y": 1
},
{
"x": 1395878400000,
"y": 1
},
{
"x": 1395964800000,
"y": 2
}
]
}
]
}
]
},
{
"label": "Top 5 _type",
"rows": [
{
"label": "Top 5 response",
"xAxisLabel": "Daily @timestamp",
"yAxisLabel": "Count",
"series": [
{
"label": "html",
"values": [
{
"x": 1395187200000,
"y": 40
},
{
"x": 1395273600000,
"y": 45
},
{
"x": 1395360000000,
"y": 40
},
{
"x": 1395446400000,
"y": 41
},
{
"x": 1395532800000,
"y": 35
},
{
"x": 1395619200000,
"y": 34
},
{
"x": 1395705600000,
"y": 33
},
{
"x": 1395792000000,
"y": 39
},
{
"x": 1395878400000,
"y": 37
},
{
"x": 1395964800000,
"y": 33
},
{
"x": 1396051200000,
"y": 31
}
]
},
{
"label": "php",
"values": [
{
"x": 1395187200000,
"y": 33
},
{
"x": 1395273600000,
"y": 20
},
{
"x": 1395360000000,
"y": 23
},
{
"x": 1395446400000,
"y": 24
},
{
"x": 1395532800000,
"y": 22
},
{
"x": 1395619200000,
"y": 28
},
{
"x": 1395705600000,
"y": 20
},
{
"x": 1395792000000,
"y": 26
},
{
"x": 1395878400000,
"y": 18
},
{
"x": 1395964800000,
"y": 26
},
{
"x": 1396051200000,
"y": 32
}
]
},
{
"label": "png",
"values": [
{
"x": 1395187200000,
"y": 12
},
{
"x": 1395273600000,
"y": 14
},
{
"x": 1395360000000,
"y": 18
},
{
"x": 1395446400000,
"y": 17
},
{
"x": 1395532800000,
"y": 13
},
{
"x": 1395619200000,
"y": 14
},
{
"x": 1395705600000,
"y": 14
},
{
"x": 1395792000000,
"y": 10
},
{
"x": 1395878400000,
"y": 10
},
{
"x": 1395964800000,
"y": 7
},
{
"x": 1396051200000,
"y": 10
}
]
},
{
"label": "css",
"values": [
{
"x": 1395187200000,
"y": 4
},
{
"x": 1395273600000,
"y": 7
},
{
"x": 1395360000000,
"y": 9
},
{
"x": 1395446400000,
"y": 8
},
{
"x": 1395532800000,
"y": 11
},
{
"x": 1395619200000,
"y": 7
},
{
"x": 1395705600000,
"y": 6
},
{
"x": 1395792000000,
"y": 3
},
{
"x": 1395878400000,
"y": 7
},
{
"x": 1395964800000,
"y": 7
},
{
"x": 1396051200000,
"y": 6
}
]
},
{
"label": "gif",
"values": [
{
"x": 1395187200000,
"y": 3
},
{
"x": 1395273600000,
"y": 4
},
{
"x": 1395360000000,
"y": 4
},
{
"x": 1395446400000,
"y": 2
},
{
"x": 1395532800000,
"y": 3
},
{
"x": 1395619200000,
"y": 3
},
{
"x": 1395705600000,
"y": 4
},
{
"x": 1395792000000,
"y": 4
},
{
"x": 1395878400000,
"y": 2
},
{
"x": 1395964800000,
"y": 2
},
{
"x": 1396051200000,
"y": 8
}
]
}
]
},
{
"label": "Top 5 response",
"xAxisLabel": "Daily @timestamp",
"yAxisLabel": "Count",
"series": [
{
"label": "php",
"values": [
{
"x": 1395187200000,
"y": 1
},
{
"x": 1395273600000,
"y": 1
},
{
"x": 1395360000000,
"y": 2
},
{
"x": 1395446400000,
"y": 2
},
{
"x": 1395532800000,
"y": 3
},
{
"x": 1395792000000,
"y": 2
},
{
"x": 1395878400000,
"y": 2
},
{
"x": 1395964800000,
"y": 3
},
{
"x": 1396051200000,
"y": 1
}
]
},
{
"label": "html",
"values": [
{
"x": 1395273600000,
"y": 2
},
{
"x": 1395360000000,
"y": 2
},
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395532800000,
"y": 1
},
{
"x": 1395705600000,
"y": 1
},
{
"x": 1395792000000,
"y": 3
},
{
"x": 1395878400000,
"y": 2
},
{
"x": 1395964800000,
"y": 3
},
{
"x": 1396051200000,
"y": 1
}
]
},
{
"label": "css",
"values": [
{
"x": 1395187200000,
"y": 1
},
{
"x": 1395619200000,
"y": 2
},
{
"x": 1395705600000,
"y": 2
}
]
},
{
"label": "png",
"values": [
{
"x": 1395273600000,
"y": 1
},
{
"x": 1395619200000,
"y": 1
},
{
"x": 1395705600000,
"y": 1
},
{
"x": 1395792000000,
"y": 1
}
]
},
{
"label": "gif",
"values": [
{
"x": 1395360000000,
"y": 1
},
{
"x": 1395878400000,
"y": 1
}
]
}
]
},
{
"label": "Top 5 response",
"xAxisLabel": "Daily @timestamp",
"yAxisLabel": "Count",
"series": [
{
"label": "php",
"values": [
{
"x": 1395187200000,
"y": 1
},
{
"x": 1395273600000,
"y": 1
},
{
"x": 1395360000000,
"y": 3
},
{
"x": 1395446400000,
"y": 2
},
{
"x": 1395532800000,
"y": 1
},
{
"x": 1395619200000,
"y": 3
},
{
"x": 1395705600000,
"y": 1
},
{
"x": 1395792000000,
"y": 1
},
{
"x": 1395878400000,
"y": 1
},
{
"x": 1396051200000,
"y": 1
}
]
},
{
"label": "html",
"values": [
{
"x": 1395360000000,
"y": 1
},
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395532800000,
"y": 1
},
{
"x": 1395619200000,
"y": 1
},
{
"x": 1395705600000,
"y": 2
},
{
"x": 1395878400000,
"y": 1
}
]
},
{
"label": "png",
"values": [
{
"x": 1395273600000,
"y": 1
},
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395878400000,
"y": 1
}
]
},
{
"label": "css",
"values": [
{
"x": 1395446400000,
"y": 1
},
{
"x": 1395532800000,
"y": 1
}
]
},
{
"label": "gif",
"values": [
{
"x": 1396051200000,
"y": 1
}
]
}
]
}
]
}
]
};
function alignData (data) {
console.log('data', data);
// look at keys in first
var testkeys = data.columns[0].rows[0].series[0].values.map(function(n) {
//console.log(n.x);
return n.x;
});
//console.log('keys', testkeys);
// get x values from every chart layer
var uniqueXs = [];
var dataMapper = data.columns.map(function(col) {
return col.rows.map(function (row) {
return row.series.map(function (ser) {
// console.log(ser.values.length);
return ser.values.map(function (val) {
// console.log(val.x);
uniqueXs.push(val.x);
});
})
})
});
// get uniques from list
uniqueXs = d3.set(uniqueXs).values();
console.log('uniqueXs', uniqueXs);
// add values in same order as uniques
var addXs = data.columns.map(function(col) {
return col.rows.map(function (row) {
return row.series.map(function (ser) {
var i = 0;
uniqueXs.forEach(function (u) {
if (!ser.values[i]) {
// means it the last value
ser.values.push({x: +u, y:0, added:"true"});
} else {
// if not a match then splice
if (ser.values[i].x !== +u) {
ser.values.splice(i,0,{x: +u, y:0, added:"true"});
}
}
console.log( i, +u, ser.values[i] );
i++;
});
})
})
});
console.log(addXs);
}
alignData(testdata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment