FastPivot.js is a simple, small, and fast JavaScript library to shift or pivot data, often needed for drawing JavaScript charts and graphs. It has no dependencies, can be used with really large JSON data.
##Usage Pass an array to fastpivot which will return columns as objects with:
- _data - contains frequency of any value in a column, used for lookup
- _labels - contains unique values in an array, used to show labels in a chart
- _labelsdata - frequency of values in an array, used to show data for each label in a graph
##Example Consider some tabular data
id | make | color | age |
---|---|---|---|
1 | Audi | 2015 | |
2 | Toyota | green | 2010 |
3 | Honda | 2013 | |
4 | Audi | red | 2011 |
5 | BMW | blue | 2012 |
6 | Audi | red | 2012 |
7 | Honda | 2013 | |
8 | BMW | blue | 2011 |
9 | Audi | red | 2010 |
10 | Toyota | red | 2012 |
That you have in JSON:
var mydata='[{"id":"1","make":"Audi","color":"","age":2015},{"id":"2","make":"Toyota","color":"green","age":2010},{"id":"3","make":"Honda","color":null,"age":2013},{"id":"4","make":"Audi","color":"red","age":2011},{"id":"5","make":"BMW","color":"blue","age":2012},{"id":"6","make":"Audi","color":"red","age":2012},{"id":"7","make":"Honda","color":null,"age":2013},{"id":"8","make":"BMW","color":"blue","age":2011},{"id":"9","make":"Audi","color":"red","age":2010},{"id":"10","make":"Toyota","color":"red","age":2012}]';
Pass it to FastPivot:
var json = JSON.parse(mydata);
var data = fastpivot(json);
data["color"]._labels //returns ["", "green", "null", "red", "blue"]
data["color"]._labelsdata //returns [1, 1, 2, 4, 2]
data["color"]._data["red"] //returns 4