JSFiddle - React, Tailwind, and code Playground
Data source
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
const pivot = new Flexmonster({
container: "pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
toolbar: true,
report: {
dataSource: {
data: getData(),
},
slice: {
columns: [
{
uniqueName: "[Measures]",
},
],
rows: [
{
uniqueName: "type",
},
{
uniqueName: "name",
},
],
expands: { expandAll: true },
measures: [
{
caption: "Weighted Average",
formula: "'value_x_weight' / sum('weight')",
uniqueName: "weighted_avg",
format: "format1",
},
{
aggregation: "sum",
uniqueName: "weight",
},
{
active: false,
formula: "sum('value') * sum('weight')",
individual: true,
uniqueName: "value_x_weight",
},
],
},
formats: [
{
name: "format1",
decimalPlaces: 2,
},
],
},
})
function getData() {
return [
{
type: "A",
name: "A1",
value: 1000,
weight: 0.3,
},
{
type: "A",
name: "A2",
value: 2000,
weight: 0.6,
},
{
type: "B",
name: "B1",
value: 5000,
weight: 0.1,
},
{
type: "B",
name: "B2",
value: 7000,
weight: 0.2,
},
]
}