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>
<button onclick="showAllMonths()">
All months
</button>
<button onclick="showLastMonths()">
Last months
</button>
<div id="pivot-container"></div>
JavaScript
let json_data = [{
"date": "2018-12-01",
"value": 100
},
{
"date": "2018-12-31",
"value": 1
},
{
"date": "2019-01-01",
"value": 1
},
{
"date": "2019-06-30",
"value": 1
},
{
"date": "2019-06-29",
"value": 1
},
{
"date": "2019-12-31",
"value": 1
},
{
"date": "2020-06-30",
"value": 1
},
{
"date": "2020-12-30",
"value": 1
}
];
new Flexmonster({
container: "#pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
width: "100%",
height: 600,
report: {
dataSource: {
data: preprocessData(json_data),
mapping: {
"year": {
"type": "string"
},
"month": {
"type": "string"
},
"value": {
"type": "number"
},
"last_month": {
"type": "string"
}
},
},
"slice": {
"rows": [{
"uniqueName": "[Measures]"
}],
"columns": [{
"uniqueName": "month",
}],
"measures": [{
"uniqueName": "value",
"aggregation": "runningtotalsofrow"
}]
},
"options": {
"viewType": "charts",
"showEmptyValues": "false",
"chart": {
"type": "line",
"showDataLabels": true,
"reversedAxes": true,
}
}
}
});
function preprocessData(data) {
const months = {
"01": "Dec",
"02": "Jan",
"03": "Mar",
"04": "Apr",
"05": "May",
"06": "Jun",
"07": "Jul",
"08": "Aug",
"09": "Sep",
"10": "Oct",
"11": "Nov",
"12": "Dec",
}
let i;
for (i = 0; i < data.length; i++) {
let yearAndMonth = data[i].date.split('-', 2);
let year = yearAndMonth[0];
let month = yearAndMonth[1];
// adding year and month fields to the dataset
data[i].year = year;
data[i].month = `${year} ${month} ${months[month]}`;
// implement your custom logic to take last available month here
if (month == 12) {
...