Custom Label Formatter on Zooming - CanvasJS JavaScript Charts

Custom Label Formatter on Zooming - CanvasJS JavaScript Charts

by canvasjs

HTML

<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>

JavaScript

var chart = new CanvasJS.Chart("chartContainer", {  
  title:{
    text: "Custom Label Formatter on Zooming" 
  },
  zoomEnabled: true,
  rangeChanging: axisXLabelFormatter,
	data: [{
		type: "column",
		dataPoints: [
		   	{ x: new Date(2010, 0, 2), y: 50 },
				{ x: new Date(2010, 0, 4), y: 40 },
				{ x: new Date(2010, 0, 6), y: 60 },
				{ x: new Date(2010, 0, 8), y: 58 },
				{ x: new Date(2010, 0, 10), y: 74 },
				{ x: new Date(2010, 0, 12), y: 60 },
        { x: new Date(2010, 0, 14), y: 50 },
				{ x: new Date(2010, 0, 16), y: 74 },
				{ x: new Date(2010, 0, 18), y: 60 }
		]
	}]
});

chart.render();

function axisXLabelFormatter(e) {
    if(!e.chart.options.axisX)
      e.chart.options.axisX = {};

    if(e.trigger === "zoom") {
      e.chart.options.axisX.labelFormatter = function ( e ) {
        return CanvasJS.formatDate( e.value, "DD MMMM" );  
      };
    }

    else if(e.trigger === "reset") {
      e.chart.options.axisX.labelFormatter = function ( e ) {
        return CanvasJS.formatDate( e.value, "MMM DD YYYY" );  
      };
    }
}