简体   繁体   中英

Javascript - AmCharts hide and show graph using legends

I am using Amcharts plugins in making a graph. But I am having issues in hiding and showing the graph using legends.

This is my function code:

function handleLegendClick( graph ) {
  var chart = graph.chart;
  for( var i = 0; i < chart.graphs.length; i++ ) {
    if ( graph.id == chart.graphs[i].id )
      chart.hideGraph(chart.graphs[i]);
    else 
     chart.showGraph(chart.graphs[i]);
  }

  if (graph.id == chart.graphs['3'].id)
      chart.hideGraph(chart.graphs['4']);

  if (graph.id ==chart.graphs['4'].id)
      chart.hideGraph(chart.graphs['3']);

    chart.validateNow();
  // return false so that default action is canceled
  return true;
}

This code is working but my problem is, it hides only one graph and show the graph again when the user click another legend. What I need to do is to hide multiple graphs by clicking it's assigned label text and marker, and when the graph is hidden I should click the same legend to show the hidden graph.

Please help me. Thank you.

From your description, the default legend behavior does exactly what you're asking for without needing to add your handleClick code. I'm not sure what's the point of it.

var chart = AmCharts.makeChart("chartdiv", {
  // ...
  "legend": { }, //default setup
});

Here's an example with multiple graphs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM