0

For a radar chart, I'm trying to toggle (with a switch button) or slide (with a slider) between different sets of data to display. (I'll include the button here first, but I'm eventually try to extend that to a slider later.). 1. Initialize button and keep track of user toggle. 2. Pick which data set to use. both generateRadarData and generateRadarData2 work well on their own if I use chart.data = either one. The below is the edited attempt:

var chartSwitchButton = chart.chartContainer.createChild(am4core.SwitchButton);

chartSwitchButton.events.on("toggle", function () {chart.data = whichData();})

 function whichData() {
  var dataToUse = chart.data;
  if (chartSwitchButton.isActive) {dataToUse = generateRadarData();
                                         } else {
                                           dataToUse = generateRadarData2();} 
  return dataToUse};

chart.data = whichData();
 

I have tried commenting out the last line (since ideally it would have been updated via the event listener), but then no data displays.

Here is a more recent attempt to update the data using invalidateRawData:

chartSwitchButton.events.on("toggle", function (event) {
  chart.data = whichData();
  chart.invalidateRawData();
  
});

function whichData() {
  var data = [];
  if (chartSwitchButton.isActive) {
    chart.data = generateRadarData();
   
  } else {
    chart.data = generateRadarData2();
   
  }
 chart.invalidateRawData(); //also tried invalidateData. tried this command in event listener as well as here.
 data.push(chart.data); //attempt to replace/update raw data
  //console.log(chart.data);
return chart.data; //this return line is necessary to output data but theoretically shouldn't be.
}

and have tried implementing the if-else w/in the event listener without having to use whichData as a separate function like so:

chartSwitchButton.events.on("toggle", function () {if (chartSwitchButton.isActive) {
    chart.data = generateRadarData();
  
  } else {
    chart.data = generateRadarData2();
  
  }
 chart.invalidateRawData();}) 

I'm still unable to switch between the two sets of data with user interaction. In fact, if I don't return something for chart.data or declare what chart.data is supposed to be outside of the events.on or whichData(), then none of my data prints at all.

If anybody has suggestions on how to do this with a button (or a slider would be even better) that would be awesome.

Basically, after setting up the button, I tried to (a) keep track of the state of the button (as determined by user), (b) determine which state the button is in, and (c) pick a data set to use based off of that info. This version is edited from a previous attempt as per initial comments below. Thanks for your help.

  • Hey @SebastianSimon just edited w new updates as per the comments. The above is the latest version with fluff/unnecessary variables out. I have no idea if putting the event listener w/in whichData should work... there's nothing in what I've read about events that suggests this would help. Anything that sticks out to you? Many many thanks again. – Bianca Swidler Jan 09 '23 at 21:54

1 Answers1

0

Documentation is "toggled" not "toggle" in the events listener. The event does not recognize "toggle" but needed "toggled". https://www.amcharts.com/docs/v4/reference/switchbutton/#toggled_event