JSFiddle - React, Tailwind, and code Playground
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 compareDataPointX(dataPoint1, dataPoint2) {
return dataPoint1.y - dataPoint2.y;
}
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Sort Datapoints by x-value"
},
data: [
{
type: "pie",
indexLabel: "{label}:{y}",
startAngle: 270,
dataPoints: [
{ label: "Apple", y: 71 },
{ label: "Mango", y: 50 },
{ label: "Kiwi", y: 55},
{ label: "Pineapple", y: 65 },
{ label: "Watermelon", y: 68 },
{ label: "Papaya", y: 28 },
]
}
]
});
//Comment out the next line to see the difference.
chart.options.data[0].dataPoints.sort(compareDataPointX);
chart.render();