Search code examples
javascriptarrayscanvasjs

Hide all series button in CanvasJS graph (javascript foreach usage)


The question related to CanvasJS, but probably any expert in pure javascript could help.

I need a button to hide/unhide all elements in canvasjs graph. There is a working code that can hide element using array index:

chart.options.data[0].visible = !chart.options.data[0].visible;

I'm trying to go through array, but it doesn't work, obviously my code is wrong:

chart.options.data.forEach(visible = !visible);

How should I fix it?

The full code snippet is:

 var chart = new CanvasJS.Chart("chartContainer",
    {
      title:{
      text: "Test Button to Hide All Series"  
      },
      
      legend: {
            cursor: "pointer",
            itemclick: function (e) {
                //console.log("legend click: " + e.dataPointIndex);
                //console.log(e);
                if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                    e.dataSeries.visible = false;
                } else {
                    e.dataSeries.visible = true;
                }
 
                e.chart.render();
            }
        },
      
      data: [
      { 
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 5 },
        { x: 20, y: 9},
        { x: 30, y: 17 },
        { x: 40, y: 32 },
        { x: 50, y: 22 },
        { x: 60, y: 14 },
        { x: 70, y: 25 },
        { x: 80, y: 18 },
        { x: 90, y: 20}
      
        ]
      },
        {
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 31 },
        { x: 20, y: 35},
        { x: 30, y: 30 },
        { x: 40, y: 35 },
        { x: 50, y: 35 },
        { x: 60, y: 38 },
        { x: 70, y: 38 },
        { x: 80, y: 34 },
        { x: 90, y: 44}
      
        ]
      },
        {
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 25 },
        { x: 20, y: 30},
        { x: 30, y: 20 },
        { x: 40, y: 45 },
        { x: 50, y: 30 },
        { x: 60, y: 10 },
        { x: 70, y: 15 },
        { x: 80, y: 18 },
        { x: 90, y: 20}
      
        ]
      }
      ]
    });
 
    chart.render();
    
 document.getElementById("showAllSeries").onclick =  function(){
   //Works for a single element using index:
   chart.options.data[0].visible = !chart.options.data[0].visible;
   //Doesn't work, need to modify
   //chart.options.data.forEach(visible = !visible);
   chart.render();
 };
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<center><button id= "showAllSeries" style= "margin: 10px;">Show/Hide All series</button></center>

UP: Found solution with for loop:

   for (var i = 0; i < chart.options.data.length; i++) {
   chart.options.data[i].visible = !chart.options.data[i].visible;
   }

But still interesting how should it work with foreach


Solution

  • forEach is an Array method that you can use to execute a function on each element in an array. On the other hand for is a loop that is more flexible.

    In your case, you can hide all dataSeries onclick of button either using for or forEach. Check the code below:

    var chart = new CanvasJS.Chart("chartContainer", {
    	title:{
    		text: "Test Button to Hide All Series"  
    	},
    
    	legend: {
    		cursor: "pointer",
    		itemclick: function (e) {
    			if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
    				e.dataSeries.visible = false;
    			} else {
    				e.dataSeries.visible = true;
    			}
    			e.chart.render();
    		}
    	},
    
    	data: [
    		{ 
    		showInLegend: true,
    		type: "line",
    		dataPoints: [
    			{ x: 10, y: 5 },
    			{ x: 20, y: 9 },
    			{ x: 30, y: 17 },
    			{ x: 40, y: 32 },
    			{ x: 50, y: 22 },
    			{ x: 60, y: 14 },
    			{ x: 70, y: 25 },
    			{ x: 80, y: 18 },
    			{ x: 90, y: 20 }
    		]
    		},
    		{
    		showInLegend: true,
    		type: "line",
    		dataPoints: [
    			{ x: 10, y: 31 },
    			{ x: 20, y: 35 },
    			{ x: 30, y: 30 },
    			{ x: 40, y: 35 },
    			{ x: 50, y: 35 },
    			{ x: 60, y: 38 },
    			{ x: 70, y: 38 },
    			{ x: 80, y: 34 },
    			{ x: 90, y: 44 }
    		]
    		},
    		{
    		showInLegend: true,
    		type: "line",
    		dataPoints: [
    			{ x: 10, y: 25 },
    			{ x: 20, y: 30 },
    			{ x: 30, y: 20 },
    			{ x: 40, y: 45 },
    			{ x: 50, y: 30 },
    			{ x: 60, y: 10 },
    			{ x: 70, y: 15 },
    			{ x: 80, y: 18 },
    			{ x: 90, y: 20 }
    		]
    		}
    	]
    });
    
    chart.render();
    
    document.getElementById("showAllSeries").onclick =  function(){
    	chart.options.data.forEach(function(dataSeries) {
      	if (typeof (dataSeries.visible) === "undefined" || dataSeries.visible) {
    			dataSeries.visible = false;
    		} else {
    			dataSeries.visible = true;
    		}
    	});
    	/*var dataSeries;
      	for(var i = 0; i < chart.data.length; i++){
      	dataSeries = chart.options.data[i];
    		if (typeof (dataSeries.visible) === "undefined" || dataSeries.visible) {
    			dataSeries.visible = false;
    		} else {
    			dataSeries.visible = true;
    		}
    	}*/  
    	chart.render();
    };
    <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
    <div id="chartContainer" style="height: 260px; width: 100%;"></div>
    <center><button id= "showAllSeries" style= "margin: 10px;">Show/Hide All series</button></center>