javascript - How to get rid of old data and of old graph in Chart.js? -


i'm using chart.js make graph.

i encounter problem when use graph. old graph old values not gone when hover cursor on graph. according doc, tried use .destroy() or .clear() rid of old data, doesn't work.

here following codes:

<!doctype html>  <html>  <head>  	<title>hp mp</title>  	<meta charset="utf-8">  	<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.1.3/chart.min.js"></script>  </head>    <body onload="hpmpgraph()">  	<script>  		function hpmpgraph() {  			var hp = document.hpmpform.hp.value,  				mp = document.hpmpform.mp.value,  				ctx = document.getelementbyid('hpmpratio').getcontext('2d');  				  			var	data = {  				labels: ["hp","mp"],  				datasets:   					[{  						data: [hp,mp],  						backgroundcolor: ["#ef0000","#0000ef"],  						hoverbackgroundcolor: ["#ff0000","#0000ff"]  					}]  			};  			var options = {};    			var mypiechart = new chart(ctx, {  				type: 'pie',  				data: data,  				options: options  			});  		}  	</script>    	<h1>hp & mp</h1>  	<form name=hpmpform method=post>  		hp : <input type="text" name="hp" value="100"><br>  		mp : <input type="text" name="mp" value="100"><br>  		<button type="button" onclick="hpmpgraph();">click</button>  		<h2>pie graph</h2>  		<canvas id="hpmpratio" height="50"></canvas>  	</form>  </body>  </html>

hp , mp both set @ 100. try set 1000 hp. put cursor on graph , move in circle. sometime, old graph show up.

you should not recreate chart since not allowed in v2, instead need replace data:

<!doctype html>  <html>  <head>  	<title>hp mp</title>  	<meta charset="utf-8">  	<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.1.3/chart.min.js"></script>  </head>    <body onload="hpmpgraph()">  	<script>    	var mypiechart = null          var config = {options:{},type:'pie'}  		function hpmpgraph() {  			var hp = document.hpmpform.hp.value,  				mp = document.hpmpform.mp.value,  				ctx = document.getelementbyid('hpmpratio').getcontext('2d');  			                          config.data = {  				labels: ["hp","mp"],  				datasets:   					[{  						data: [hp,mp],  						backgroundcolor: ["#ef0000","#0000ef"],  						hoverbackgroundcolor: ["#ff0000","#0000ff"]  					}]  			};  			if(mypiechart == null){                              mypiechart = new chart(ctx, config);                          }else{                              mypiechart.update()                          }                   		}  	</script>    	<h1>hp & mp</h1>  	<form name=hpmpform method=post>  		hp : <input type="text" name="hp" value="100"><br>  		mp : <input type="text" name="mp" value="100"><br>  		<button type="button" onclick="hpmpgraph();">click</button>  		<h2>pie graph</h2>  		<canvas id="hpmpratio" height="50"></canvas>  	</form>  </body>  </html>

<!doctype html>  <html>  <head>  	<title>hp mp</title>  	<meta charset="utf-8">  	<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.1.3/chart.min.js"></script>  </head>    <body onload="hpmpgraph()">  	<script>  		function hpmpgraph() {  			var hp = document.hpmpform.hp.value,  				mp = document.hpmpform.mp.value,  				ctx = document.getelementbyid('hpmpratio').getcontext('2d');  				  			var	data = {  				labels: ["hp","mp"],  				datasets:   					[{  						data: [hp,mp],  						backgroundcolor: ["#ef0000","#0000ef"],  						hoverbackgroundcolor: ["#ff0000","#0000ff"]  					}]  			};  			var options = {};    			var mypiechart = new chart(ctx, {  				type: 'pie',  				data: data,  				options: options  			});  		}  	</script>    	<h1>hp & mp</h1>  	<form name=hpmpform method=post>  		hp : <input type="text" name="hp" value="100"><br>  		mp : <input type="text" name="mp" value="100"><br>  		<button type="button" onclick="hpmpgraph();">click</button>  		<h2>pie graph</h2>  		<canvas id="hpmpratio" height="50"></canvas>  	</form>  </body>  </html>


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -