1

I am using charts in my webapp and for that I am using angular-charts. The html file for charts is

    <canvas id="pie" class="chart chart-pie" chart-data="data"
    chart-labels="labels">
    </canvas>

and the controller file for this is

    angular.module('myApp')
    .controller('myController', function ($scope) { 

    $scope.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales"];
    $scope.data = [300, 500, 100];
    });

Now I just want to know how to add data dynamically into this pie chart.

Thanks in advance

3
  • what is the chart library you are using? Commented Apr 6, 2016 at 5:32
  • I am using Chart.js and angular-chart.js . Commented Apr 6, 2016 at 5:34
  • just push data to your data & labels arrays dynamically Commented Apr 6, 2016 at 5:35

2 Answers 2

1

You might have a button , when you click you populate arrays your charts are bound to.

$scope.onClick = function(item,label)
{
  //item and label can come from anywhere, i just add here as parameters to illustrate as example
  $scope.data.push(item);
  $scope.labels.push(label);
}
Sign up to request clarification or add additional context in comments.

Comments

0

To add dynamically data to the chart you have to push data to both the data arrays and the labels array.

Note: Data array is not actually an array, it is an array inside an array. So data is not [] but rather a [[]].

Code to make it work:

  1. If using $scope: $scope.data[0].push(data) and also $scope.labels.push(label)
  2. If using vm: vm.data[0].push(data) and also vm.labels.push(label)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.