Visibility of Data Series on external button click - CanvasJS HTML5-JavaScript chart

Visibility of Data Series on external button click - CanvasJS HTML5-JavaScript chart

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>

<button id="button1">
Series 1
</button>

<button id="button2">
Series 2
</button>

JavaScript

var chart = new CanvasJS.Chart("chartContainer",
	{
      title:{
        text: "Visibility of Data Series on external button click"
      },  
      data: [
      {        
        name: "Series 1",
        visible: false, //***hidden        
        dataPoints: [
        { x: 10, y: 41},
        { x: 20, y: 55},
        { x: 30, y: 50 },
        { x: 40, y: 65 },
        { x: 50, y: 85 },
        { x: 60, y: 68 },
        { x: 70, y: 28 },
        { x: 80, y: 34 },
        { x: 90, y: 14}
        ]
      },

      {        
        name:"Series 2",
		visible: true,
        dataPoints: [
        { x: 10, y: 34},
        { x: 20, y: 45},
        { x: 30, y: 19 },
        { x: 40, y: 65 },
        { x: 50, y: 25 },
        { x: 60, y: 60 },
        { x: 70, y: 48 },
        { x: 80, y: 04 },
        { x: 90, y: 35}
        ]
      }

      ]
    });

 chart.render();
 
 document.getElementById("button1").onclick =  function(){
   chart.options.data[0].visible = !chart.options.data[0].visible;
   chart.render();
 };
 
 document.getElementById("button2").onclick =  function(){
   chart.options.data[1].visible = !chart.options.data[1].visible;
   chart.render();
 };