Search code examples
javascriptamcharts

amcharts hideGraph vs graphs.hidden


chart = initialiseGraph();
chart.graphs.forEach(function(elm){
 elm.hidden=true;
});
chart.validateNow();

and

function handleLegendClick( graph ) {
  var chart = graph.chart;
  for( var i = 0; i < chart.graphs.length; i++ ) {
    if ( graph.id == chart.graphs[i].id )
      chart.showGraph(chart.graphs[i]);
    else
      chart.hideGraph(chart.graphs[i]);
  }
  // return false so that default action is canceled
  return false;
}

What are the differences between these two codes and performance wise which one is better?


Solution

  • The showGraph/hideGraph methods has the same effect as calling validateNow after each call which redraws the entire graph completely after each call, whereas setting the hidden property does nothing until you call validateNow to update the chart.

    In a looping situation, modifying the hidden property on multiple graphs first before calling validateNow once at the end of the loop is significantly faster than calling the show/hide method at each iteration.