Dynamically Adding/ Removing Data Series - CanvasJS

Dynamically Adding/Removing Data Series - CanvasJS

by canvasjs

HTML

<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
<button id="add" >Add Series</button>
<button id="remove">Remove Series</button>

JavaScript

var chart = new CanvasJS.Chart("chartContainer",
{      
	data: [
	{
		type: "column",
		dataPoints: [
		{ x: 10, y: 71 },
		{ x: 20, y: 55},
		{ x: 30, y: 50 },
		{ x: 40, y: 65 },
		{ x: 50, y: 95 }
	]
	}
	]
});

chart.render();

$("#add").click(function(){  
    var newSeries = {
        type: "column",
        dataPoints: [
            { x: 10, y: 71 },
            { x: 20, y: 55},
            { x: 30, y: 50 },
            { x: 40, y: 65 },
            { x: 50, y: 95 }
        ]
    };
    chart.options.data.push(newSeries);
    //chart.options.data[0].dataPoints.push({ x: 100, y: 14});
    chart.render();
});

$("#remove").click(function(){ 
    chart.options.data.pop();
    chart.render();
});