Access DataPoint index in Scatter Chart - CanvasJS

Access DataPoint index in Scatter Chart - CanvasJS

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: "Click on any dataPoint"
		},
		data: [
		{
		type: "scatter",
		click: onClick,
		dataPoints: [
        { x: 20, y: 45, index: 0 },
		{ x: 10, y: 34, index: 1 },
		{ x: 30, y: 19, index: 2 },
		{ x: 40, y: 75, index: 3 },
		{ x: 50, y: 15, index: 4 },
		{ x: 60, y: 60, index: 5 },
		{ x: 70, y: 48, index: 6 },
		{ x: 80, y: 04, index: 7 },
		{ x: 90, y: 35, index: 8 }
		]
		}
		]
	});

chart.render();

function onClick(e) {
		alert( e.dataSeries.type 
              + ", dataPoint { x:" + e.dataPoint.x + ", y: "+ e.dataPoint.y + " }, index=" + e.dataPoint.index );
}