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>
CSS
.dark-grey {
background-color: #e9e9e9 !important;
}
.green {
background-color: #c4dca2 !important;
}
JavaScript
let columnTotals = [];
function getTotalValues(result) {
columnTotals = [];
result.data.forEach(function(item) {
//get totals to calculate the value to total ratio
if (item.c0 && item.v0) {
columnTotals.push(item.v0);
}
});
}
function customizeCellFunction(cell, data) {
//condition on values
if (data && data.type == "value" && !data.isGrandTotal) {
let cellWidth = cell.style["min-width"] || cell.style.width;
//value to total ratio
let widthPercent = data.value / columnTotals[data.columnIndex - 1];
//width in pixels
let widthPx = `${isNaN(widthPercent) ? `0`: widthPercent*(parseInt(cellWidth)-8.5)}px`;
//insert bar into cell
cell.text = `<div class="container">
<div class="dark-grey">
<div class="container green" style="width: ${widthPx};">${data.escapedLabel || "$0"}</div>
</div>
</div>`
}
}
var pivot = new Flexmonster({
container: "pivot-container",
customizeCell: customizeCellFunction,
componentFolder: "https://cdn.flexmonster.com/",
licenseFilePath: "https://cdn.flexmonster.com/jsfiddle.charts.key",
width: "100%",
height: 600,
toolbar: true,
report: {
dataSource: {
filename: "data/sales.csv"
},
slice: {
rows: [{
uniqueName: "Month"
}, {
uniqueName: "[Measures]"
}],
columns: [{
uniqueName: "Category",
levelName: "Product Name",
filter: {
members: [
"category.[condiments].[bbq sauce]",
"category.[breakfast cereals].[corn flakes]",
"category.[confectionery]",
"category.[bakery].[chocolate biscuits]",
"category.[fruit preserves].[apple jam]",
"category.[bakery].[apple cake]",
"category.[soups].[tomato soup]"
]
}
}],
measures: [{
"uniqueName": "Revenue",
"aggregation": "sum"
}]
},
formats: [{
name: "",
decimalPlaces:...