JavaScript PivotGrid - rows, columns and pivot cells with custom HTML.
A live JavaScript/jQuery example of jqxPivotGrid, the jQWidgets PivotGrid component, with its complete source code below. It is one of 12 jqxPivotGrid demos for jQuery.
source property – Gets or sets pivot source adapter used to supply data to the pivot grid.multipleSelectionEnabled property – Gets or sets whether the multiple selection in the pivot grid is enabled or disabled.treeStyleRows property – Gets or sets the rows of the pivot grid are displayed as a tree structure or using classic OLAP style.autoResize property – Gets or sets if the size of pivot grid adjusts automatically to display the entire content.itemsRenderer property – Custom rendering function used to render the pivot rows and columns.cellsRenderer property – Custom rendering function used to render the pivot cells.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxpivot.js, jqxpivotgrid.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).
$(document).ready(function () {
// prepare sample data
var data = new Array();
var firstNames =
[
"Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
];
var lastNames =
[
"Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"
];
var productNames =
[
"Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"
];
var priceValues =
[
"2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
];
for (var i = 0; i < 500; i++) {
var row = {};
var productindex = Math.floor(Math.random() * productNames.length);
var price = parseFloat(priceValues[productindex]);
var quantity = 1 + Math.round(Math.random() * 10);
row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
row["productname"] = productNames[productindex];
row["price"] = price;
row["quantity"] = quantity;
row["total"] = price * quantity;
data[i] = row;
}
// create a data source and data adapter
var source =
{
localdata: data,
datatype: "array",
datafields:
[
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' },
{ name: 'quantity', type: 'number' },
{ name: 'price', type: 'number' },
{ name: 'total', type: 'number' }
]
};
var dataAdapter = new $.jqx.dataAdapter(source);
dataAdapter.dataBind();
// create a pivot data source from the dataAdapter
var pivotDataSource = new $.jqx.pivot(
dataAdapter,
{
pivotValuesOnRows: false,
rows: [{ dataField: 'firstname' }, { dataField: 'lastname'}],
columns: [{ dataField: 'productname'}],
filters: [
{
dataField: 'productname',
filterFunction: function (value) {
if (value == "Black Tea" || value == "Green Tea")
return true;
return false;
}
}
],
values: [
{ dataField: 'price', 'function': 'sum', text: 'Sum', formatSettings: { prefix: '$', decimalPlaces: 2} },
{ dataField: 'price', 'function': 'count', text: 'Count' },
{ dataField: 'price', 'function': 'average', text: 'Average', formatSettings: { prefix: '$', decimalPlaces: 2} }
]
});
// create a pivot grid
$('#divPivotGrid').jqxPivotGrid(
{
source: pivotDataSource,
treeStyleRows: false, // change this property to switch between treestyle and olap style display
autoResize: false,
multipleSelectionEnabled: true,
itemsRenderer: function (pivotItem) {
var backgroundColor = pivotItem.isColumn ? 'rgba(187, 232, 227, 255)' : 'rgba(203, 254, 187, 255)';
if (pivotItem.isSelected)
backgroundColor = pivotItem.isColumn ? 'rgba(167, 212, 207, 255)' : 'rgba(183, 234, 157, 255)';
var sortElement = '';
if (pivotItem.hierarchy.getSortItem() == pivotItem)
{
var elementClass = pivotItem.hierarchy.getSortOrder() == 'desc' ? "jqx-icon-arrow-down" : "jqx-icon-arrow-up";
sortElement = "<div id='sortElement' class='" + elementClass + "' style='position: relative; float: right; margin-right: 0px; width: 16px; height: 100%; line-height: 100%;'></div>";
}
return "<div style='background: "
+ backgroundColor
+ "; width: calc(100% - 8px); height: calc(100% - 8px); padding: 4px;'>"
+ pivotItem.text
+ sortElement
+ "</div>";
},
cellsRenderer: function (pivotCell) {
var colors = ['rgba(248, 105, 107, 255)', 'rgba(250,170,120,255)', 'rgba(255,230,130,255)', 'rgba(175,215,130,255)', 'rgba(100,190,120,255)'];
var selectedColors = ['rgba(228, 85, 87, 255)', 'rgba(230,150,100,255)', 'rgba(235,210,110,255)', 'rgba(155,195,110,255)', 'rgba(80,170,100,255)'];
var val = Math.min(pivotCell.value, 20);
var backgroundColor = pivotCell.isSelected ? selectedColors[Math.round(val / 5)] : colors[Math.round(val / 5)];
if (pivotCell.pivotColumn.text != 'Sum')
backgroundColor = pivotCell.isSelected ? 'rgba(225, 225, 225, 255)' : 'rgba(255, 255, 255, 255)';
if (pivotCell.isSelected)
backgroundColor
var cellText = pivotCell.value == 0 ? '' : pivotCell.formattedValue;
return "<div style='background: " + backgroundColor + "; width: calc(100%-8px); height: 100%; padding: 4px; margin: 0px;'>" + cellText + "</div>";
},
});
});
<div id="divPivotGrid" style="height: 400px; width: 800px; background-color: white;">
</div>