JSFiddle - React, Tailwind, and code Playground
by Flexmonster Pivot Table
HTML
<link rel="stylesheet" href="https://s3.amazonaws.com/flexmonster/2.3/demo.css">
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<div id="pivot-container"></div>
JavaScript
var colorScheme = [
"#F8746D",
"#FA9172",
"#FBAE78",
"#FDD17F",
"#F7E683",
"#DBE081",
"#C3D980",
"#9FCF7E",
"#7DC57C",
"#68BF7B"
]
var minValue = 0;
var maxValue = 0;
function onReportComplete() {
pivot.getData({}, function(result) {
minValue = Number.MAX_VALUE;
maxValue = Number.MIN_VALUE;
result.data.forEach(function(item) {
if (item.r0 && item.c0 && item.v0) { // skip totals and empty values
minValue = Math.min(minValue, item.v0);
maxValue = Math.max(maxValue, item.v0);
}
});
pivot.refresh();
});
}
function customizeCellFunction(cell, data) {
if (minValue == 0 && maxValue == 0) {
return;
}
if (data && data.type == "value" && !data.isGrandTotal) {
// Apply color scale if "Bakery" is in column:
if (data.columns) {
data.columns.forEach(column => {
if (column.hierarchyName === "Category" && column.uniqueName.includes("bakery")) {
cell.style["background-color"] = getColorFromRange(data.value);
}
});
}
// Apply color scale if "Bakery" is in row:
if (data.rows) {
data.rows.forEach(row => {
if (row.hierarchyName === "Category" && row.uniqueName.includes("bakery")) {
cell.style["background-color"] = getColorFromRange(data.value);
}
});
}
}
}
function getColorFromRange(value) {
if (isNaN(value)) {
value = minValue;
}
value = Math.max(minValue, Math.min(value, maxValue));
var perc = (minValue + value) / maxValue;
var colorIdx = Math.round((colorScheme.length - 1) * perc);
return colorScheme[colorIdx];
}
var pivot = new Flexmonster({
container: "pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
licenseFilePath: "https://cdn.flexmonster.com/jsfiddle.charts.key",
width: "100%",
height: 600,
toolbar: true,
customizeCell: customizeCellFunction,
reportcomplete: onReportComplete,
report: {
dataSource: {
filename:...