0

What I'm attempting to do: I'm building a single-page app using Angular UI-Router. One of my pages has a line chart so I am using angular-chart.js.

What I don't understand: I don't understand how to get the charts to show inside the UI-Router state. I can get the graph to work if I don't include it into a single-page app. I have a feeling I need to add a controller to the UI-Router state containing the $scope labels, series and data, but I haven't been able to get it to work properly.

Angular-Chart.js Code

var chartApp = angular.module('myApp', ['chart.js']);

chartApp.controller("LineCtrl", function ($scope) {
    'use strict';   
    $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
    $scope.series = ['Motivation', 'Workload'];
    $scope.data = [
        [65, 59, 80, 81, 56, 55, 40],
        [28, 48, 40, 19, 86, 27, 90]
    ];
    $scope.onClick = function (points, evt) {
        console.log(points, evt);
    };
});

UI-Router Code

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // chart page
    .state('charts', {
        url: '/charts',
        templateUrl: 'app/charts.html'
    });
});
1
  • Yes, the controller has to be instantiated on the chart template, either by ng-init or by adding it to the state. Commented Aug 2, 2015 at 13:28

2 Answers 2

1

In your javascript code, you did not include the controller. It should be:

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // chart page
    .state('charts', {
        url: '/charts',
        templateUrl: 'app/charts.html'
        controller: 'LineCtrl'
    });
});

Although, there is a better way than this. You could resolve the data coming from a service so that the page would wait for that data before it shows the compiled html.

var myApp = angular.module('myApp', ['ui.router', 'someService']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // chart page
    .state('charts', {
        url: '/charts',

        //I use the 'views' property to ready my code for nested views as well as to
        //isolate the properties for each view on that particular url
        views: {
            '':{
                templateUrl: 'app/charts.html',
                controller: 'LineCtrl',
                controllerAs: 'lineCtrl',
                resolve:{
                    labels: function(someService){
                        return someService.getAllLabels();
                    },
                    series: function(someService){
                        return someService.getAllSeries();
                    },
                    data: function(someService){
                        return someService.getAllData();
                    }
               }
            }
        }
    });
});

And your controller should look like this:

var chartApp = angular.module('myApp', ['chart.js']);

chartApp.controller("LineCtrl", ['$scope', 'labels', 'series', 'data', function ($scope, labels, series, data) {
    'use strict';   
    $scope.labels = labels;
    $scope.series = series;
    $scope.data = data;
    $scope.onClick = function (points, evt) {
        console.log(points, evt);
    };

}]);
Sign up to request clarification or add additional context in comments.

1 Comment

Immanuel, thanks for the insight. I hadn't thought to use views. That would allow me to create multiple graphs and call them from the HTML with <div ui-view=""></div>, correct? I'll give this a shot.
0

I couldn't get Immanuel's code to work for me, but he led me on the right track to use views. I read up on multiple views using UI Router from this link: https://scotch.io/tutorials/angular-routing-using-ui-router. Almost immediately, I was able to get it to work.

The charts.html page called the ui-view:

<div class="row"> <div class="col-md-6 col-sm-12" id="line-chart" ui-view="line-chart"></div> <div class="col-md-6 col-sm-12" id="bar-chart" ui-view="bar-chart"></div> </div> <div class="row"> <div class="col-md-6 col-sm-12" id="doughnut-chart" ui-view="doughnut-chart"></div> <div class="col-md-6 col-sm-12" id="radar-chart" ui-view="radar-chart"></div> </div>

My app.js code:

var myApp = angular.module('myApp', ['ui.router', 'chart.js']);
myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // charts page
    .state('charts', {
        url: '/charts',
        views: {
            '': { templateUrl: 'app/charts.html', },
            'line-chart@charts': {
                templateUrl: 'app/charts-line.html',
                controller: 'lineController'
            },
            "bar-chart@charts": {
                templateUrl: 'app/charts-bar.html',
                controller: 'barController'
            },
            "doughnut-chart@charts": {
                templateUrl: 'app/charts-doughnut.html',
                controller: 'doughnutController'
            },
            "radar-chart@charts": {
                templateUrl: 'app/charts-radar.html',
                controller: 'radarController'
            }
        }
    })
});

Then I called the controller for each. Here is the line chart:

// Define the charts controller called from the charts state
myApp.controller('lineController', function($scope) {
    'use strict';
    $scope.message = 'There should be a line chart here:';
    $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
    $scope.series = ['Motivation', 'Workload'];
    $scope.data = [
        [65, 59, 80, 81, 56, 55, 40],
        [28, 48, 40, 19, 86, 27, 90]
    ];
});

And that was it!

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.