GrantMStevens / amCharts-Angular

Angular directive for amCharts library
Apache License 2.0
63 stars 39 forks source link

Can't reference data when creating charts dynamically #67

Closed davidjllo closed 7 years ago

davidjllo commented 8 years ago

I'm creating charts dynamically and trying changing data depending on a button group but cant find a way to reference the charts. Do I use its id?

trying to do this: chart.dataProvider = chartData[d]; chart.validateData(); chart.animateAgain();

SteveShe commented 8 years ago

I just had to figure this out as well and I think the right way to do this is to broadcast the right events to get the chart to redraw. You do this in two steps:

  1. Include the $rootScope on whatever controller is in scope when you want to do this.
  2. Broadcast the events that you want amCharts to execute.
app.controller('myController', ['$scope', '$rootScope', function ($scope, $rootScope) {
...
        $rootScope.$broadcast('amCharts.renderChart');
...

The available events that he is listening for are defined in amChartsDirective.js

         var onAmChartsTriggerChartAnimate = $scope.$on('amCharts.triggerChartAnimate', function (event, id) {
            if (id === $el[0].id || !id) {
              chart.animateAgain();
            }
          });

          var onAmChartsUpdateData = $scope.$on('amCharts.updateData', function (event, data, id) {
            if (id === $el[0].id || !id) {
              chart.dataProvider = data;
              chart.validateData();
            }

          });

          var onAmChartsValidateNow = $scope.$on('amCharts.validateNow', function (event, validateData, skipEvents, id) {
            if (id === $el[0].id || !id) {
              chart.validateNow(validateData === undefined ? true : validateData,
                skipEvents === undefined ? false : skipEvents);
            }
          });

          var onAmChartsRenderChart = $scope.$on('amCharts.renderChart', function (event, amChartOptions, id) {
            if (id === $el[0].id || !id) {
              chart.clear();
              renderChart(amChartOptions);
            }
          });