1

Can someone help me in approach for passing Angular Variable to Google Chart Function which is present in the same Java Script with Angular Directive

And Also I wanted to load Google Charts to load only after passing the values.I just read some question they mentioned something like Promises How can i use here.

Java Script

In Below Code I wanted to pass Angular Variables openCount,closedCount to Google Chart Function like with Angular JS

Demo For Google Chart with Angular JS

In the above Demo I am hard coding the values

"rows": [
        {c: [
            {v: "Defect Opened"},
            {v:1}
        ]},

        {c: [
            {v: "Defects Closed"},
            {v: 2}

        ]

Now instead of hard coding values I wanted to pass the values from below controller. How can I achieve this[What modification required for this]

var app = angular.module('defectApp', ["ngTable"]);
app.controller('defectController', [
    '$scope',
    '$http',
    function($scope, $http) {
        $http({method: 'POST', url: '/angular/defect/defect.php'}).success(function(response) {
            $scope.post = response;

            console.log(response);

            var openCount = $filter('filter')(response.data, {status: 'Open'}).length,
            closedCount = $filter('filter')(response.data, {status: 'Closed'}).length,
            assignedCount = $filter('filter')(response.data, {status: 'Assigned'}).length;

            console.log(openCount);
        })
    },
]);
4
  • when you call drawSarahChart? Commented Nov 1, 2016 at 15:25
  • I wanted to call after assigning the values to openCount,closedCount Commented Nov 1, 2016 at 15:27
  • can't you pass openCount and closedCount to drawSarahChart in controller after openCount and closedCount get assigned? Commented Nov 1, 2016 at 15:30
  • No Because I need to pass this function into Google Chart as below later google.charts.load('current', {'packages':['corechart']}); // Draw the pie chart for Sarah's pizza when Charts is loaded. google.charts.setOnLoadCallback(drawSarahChart); Commented Nov 1, 2016 at 15:35

1 Answer 1

1

One option would be to introduce a service and move the method for getting data from defectController into service:

.service('defectService', function ($http, $filter) {
    return {
        getInfo: function () {
            return $http({ method: 'POST', url: '/angular/defect/defect.php' })
                .then(function (response) {
                    return {
                        "openCount": $filter('filter')(response.data, { status: "Open" }).length,
                        "closedCount": $filter('filter')(response.data, { status: 'Closed' }).length,
                        "assignedCount": $filter('filter')(response.data, { status: 'Assigned' }).length
                    }
                });
        }
    };
})

Then inject defectService service info chart contoller. Modify chart controller to draw a chart once the data is retrieved:

.controller("chartCtrl", function ($scope, defectService) {

    defectService.getInfo()
        .then(function (info) {
            console.log(info);
            $scope.drawChart(info);
        });


    $scope.drawChart = function (info) {
        //code is omitted for clarity..
    };


});

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Worked like a charm well explained and Thanks for the demo :)
I am finding little trouble with my defect.json Can you please help me with this { "data": [ { "type": "defect", "id": 1, "user-01": "John", "status": "Closed", "severity": "1 - Urgent" }, { "type": "defect", "id": 4, "user-01": "John", "status": "Closed", "severity": "4 - Medium" }, { "type": "defect", "id": 5, "user-01": "John", "status": "Open", "severity": "4 - Medium" } ] } this is the real structure of my defect.json
Sure, here is an updated plunker. Key point: $filter function which expects an array for the first parameter, it should be response.data.data since response.data corresponds to response body and data root element

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.