Update y value on button click- CanvasJS

Update y value on button click - CanvasJS

by canvasjs

HTML

<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainer" style="height: 280px; width: 100%;"></div>
<button id="button">Update y-Value</button>

JavaScript

var chart = new CanvasJS.Chart("chartContainer",
{
	title:{
		text: "Update y value on button click",
	},
	data: [
	{
		type: "pie",
        indexLabel: "{label} : {y}",
        indexLabelFontColor: "black",
		dataPoints: [
 			{  y: 8.24,  label: "Banana"  },
            {  y: 7.16,  label: "Orange"  },
            {  y: 4.67,  label: "Grapes"  },
            {  y: 3.67,  label: "Mango"   },       
            {  y: 5.98,  label: "Papaya"  }
		]
	}
	]
});
chart.render();

function dataPointChanged() {
	
    var i = 0;
    
    chart.options.data[0].dataPoints[i].y += 1;   // you can update value here   
    chart.render();
}

var button = document.getElementById( "button" );
button.addEventListener( "click",  dataPointChanged);