Rounding dates in Elasticsearch using the mapping.fixed_interval property

Mapping configuration

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>

<button onclick="roundDates('1m')">Default dates</button>
<button onclick="roundDates('3h')">Rounded dates</button>

<div id="pivot-container"></div>

CSS

@import url(https://cdn.flexmonster.com/assets/jsfiddle-styles.css);

JavaScript

let pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  toolbar: true,
  report: {
  	dataSource: {
    	type: "elasticsearch",
      node: "https://olap.flexmonster.com:9200",
      index: "kibana_sample_data_flights",
      mapping: {
      	"timestamp": {
        	format: "dd/MM/yyyy HH:mm",
          fixed_interval: "3h",
          caption: "Arrival date"
        },
        "DestCityName": {
        	caption: "Destination city"
        },
        "DistanceMiles": {
        	caption: "Distance"
        }
      }
    },
    slice: {
    	rows: [
      	{
        	uniqueName: "timestamp",
          filter: {
          	query: {
            	between: ["09/10/2018", "09/12/2018"]
            }
          }
        }
      ],
      columns: [
      	{
      		uniqueName: "Carrier"
      	},
      	{
      		uniqueName: "[Measures]"
      	}
      ],
      measures: [
      	{
        	uniqueName: "DistanceMiles",
          aggregation: "sum"
        }
      ]
    },
    formats: [
    	{
      	name: "",
        decimalPlaces: 2
      }
    ],
    conditions: [
   		{
      	formula: '#value > 50000',
        format: {
        	backgroundColor: '#F57C00',
          color: '#FFFFFF'
        }
      }
    ]
  }
});

function roundDates(interval) {
	let report = pivot.getReport();
  report.dataSource.mapping["timestamp"].fixed_interval = interval;
  pivot.setReport(report);
}