Plotly.js is Easier Than Chart.js in Dynamically Rendering Charts

In this article, we provide a couple ways to update ot redraw the chrats when using Chart.js. Unfortunatly, it is a lot of work to do redraw chrats with Chart.js. Plotly.js is much easier.

With Plotly.js, all you need to do is to recall the function, and it works, unlike chart.js causing an error by recalling new Chart. The Plotly.newPlot() method would just grab the div with a certain id and clear the div, and draw on it. So every time Plotly.newPlot() is called, w a new chart is drawn.

Plotly.newPlot();

Playgound

Playground Code

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<button onclick="draw()">Draw</button>
<div id="myPlot" style="max-width: 700px; width: 100%;"></div>
<script>
function draw(){
  var xArray = [];
  var yArray = [];
  var i = 0;  
  setInterval(()=>{
    xArray.push(i++);
    yArray.push(Math.random()*10);  
    var data = [{
 	 x: xArray,
  	y: yArray,
	  mode:"lines"
	}];
    var layout = {
 	 title: "Random Numbers"
 	};
    Plotly.newPlot("myPlot", data, layout);
    },100);
};
</script>

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser