amcharts / dataloader

Apache License 2.0
25 stars 25 forks source link

ZoomToindex not working on dataLoader #21

Closed jomayan closed 6 years ago

jomayan commented 6 years ago

"listeners": [{ "event": "init", "method": function(e) { e.chart.zoomToIndex(e.chart.dataLoader.length - 70, e.chart.dataLoader.length - 1); } }],

The code above does not work on AmCharts Serial with dataLoader. Can anyone help?

amcharts commented 6 years ago

New comment from Zendesk by Anthony Piris on ticket 38830. (replying here will automatically notify amCharts support agent)

The dataLoader is a config object for the plugin and contains no data; it simply loads data to your chart's dataProvider at your specified URL. Even if you fixed it to use dataProvider, the plugin also fires on init so the chart's dataProvider is not guaranteed to be populated when your init event fires as well.

You can use either the the complete callback provided by the dataProvider wrapped in a setTimeout since data is populated asynchronously during the callback, or use the chart's dataUpdated event instead:

complete example:

  "dataLoader": {
    "url": "...",
    "complete": function(chart) {
      setTimeout(function() {
        chart.zoomToIndexes(chart.dataProvider.length - 70, chart.dataProvider.length - 1)
      }, 100);
    }
  },

dataUpdated example:

  "listeners": [{
    "event": "dataUpdated",
    "method": function(e) {
        e.chart.zoomToIndexes(e.chart.dataProvider.length - 70, e.chart.dataProvider.length - 1)
    }
  }]

Demo: https://codepen.io/team/amcharts/pen/f2849c99499cc6f2a85a67fa827dce3f?editors=0010

I hope this helps.

Best,

Anthony Piris amCharts

jomayan commented 6 years ago

Thanks Anthony for this :)

amcharts commented 6 years ago

New comment from Zendesk by Anthony Piris on ticket 38830. (replying here will automatically notify amCharts support agent)

You're welcome! Glad to help.

Best,

Anthony Piris amCharts