Lorelie
Lorelie

Reputation: 23

Can I save a state in amchart graph?

I was task to add additional feature that can save its state after a user toggle the legend items when he logged out and the state should be the same when he logged in. Is it possible to do it in amchart?

Upvotes: 2

Views: 179

Answers (1)

xorspark
xorspark

Reputation: 16012

AmCharts doesn't provide any built-in solutions to save/load chart state, so you need to write your own custom code. As @Abhijit mentioned, you can use localStorage to accomplish this. I already provided a sample solution in our support forum using the legend events but here it is again for you and anyone else who is looking:

AmCharts.makeChart("...", {
  // ...
  graphs: [{
      // ...
      //get hidden value from localStorage. needs to cast stored string "1" or "0" to a number, then casted to a boolean if a value exists
      hidden:
        localStorage.carsHidden !== undefined
          ? !!+localStorage.carsHidden
          : false,
   },
  // repeat
  ],
  legend: {
    // ...
    listeners: [
      {
        event: "showItem",
        method: saveState
      },
      {
        event: "hideItem",
        method: saveState
      }
    ]
  }
});

function saveState(e) {
  localStorage[e.dataItem.valueField + "Hidden"] =
    e.type === "hideItem" ? 1 : 0;
}

Demo

Upvotes: 2

Related Questions