Sorting Data Points in CanvasJS HTML5 Charts - CanvasJS Javascript Charts

Sorting Data Points in CanvasJS HTML5 Charts - 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

function compareDataPointYAscend(dataPoint1, dataPoint2) {
		return dataPoint1.y - dataPoint2.y;
}

function compareDataPointYDescend(dataPoint1, dataPoint2) {
		return dataPoint2.y - dataPoint1.y;
}



var chart = new CanvasJS.Chart("chartContainer",
	{
        
      data: [
      {
        type: "column",
        dataPoints: [
        { label: "A", y: 71 },        
        { label: "B", y: 50 },
        { label: "C", y: 55},
        { label: "D", y: 65 },        
        { label: "E", y: 68 },
        { label: "F", y: 28 },
        { label: "G", y: 95 },
        { label: "H", y: 34 },
        { label: "I", y: 14}
        ]
      }
      ]
    });

//Comment out the next line to see the difference.

//comment both to get default order


//increasing order
chart.options.data[0].dataPoints.sort(compareDataPointYAscend);


		//descending order
//chart.options.data[0].dataPoints.sort(compareDataPointYDescend);

chart.render();