I am trying to use the NVD3 Box Charts in Angular-JS. I wish to update the charts when the data is changed in the controller.
To update the chart, I need to use the following line
$scope.api.updateWithData($scope.data);
However, in my controller, I am not able to access $scope.api
However, if I follow the App and Controller Definition method given in Plunker-Example or as in Question it works perfectly.
I guess the problem is with the way I am defining the App and Controller.
I wish to get it working with below method. Could you please help me here
HTML-DIV
<div ng-controller="MyController">
<nvd3 options="options" data="data" api="api" config="{refreshDataOnly: true}"> </nvd3>
</div>
App Def
(function () {
'use strict';
angular
.module('app', ['ngRoute', 'ngCookies', 'smart-table', 'ui.bootstrap', 'ngFileUpload', 'angular-img-cropper','nvd3'])
.config(config)
.run(run);
config.$inject = ['$routeProvider', '$locationProvider'];
function config($routeProvider, $locationProvider) {
}
run.$inject = ['$rootScope', '$location', '$cookieStore', '$http'];
function run($rootScope, $location, $cookieStore, $http) {
}
})();
Controller Def
(function () {
'use strict';
angular
.module('app')
.controller('MyController', MyController);
MyController.$inject = ['UserService', '$rootScope', '$scope', '$cookieStore', 'AuthenticationService'];
function MyController(UserService, $rootScope, $scope, $cookieStore, AuthenticationService) {
var vm = this;
$scope.data = [ ];
// D3 JS Data
$scope.options = {
chart: {
type: 'boxPlotChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 60,
left: 40
},
color:['darkblue', 'darkorange', 'green', 'darkred', 'darkviolet'],
x: function(d){return d.label;},
// y: function(d){return d.values.Q3;},
useInteractiveGuideline: true,
transitionDuration:500,
maxBoxWidth: 75,
yDomain: [0, 300]
}
};
$scope.btnDisplayData = function() {
// $scope.getOpParamsAcrossSites();
$scope.data = [
{
label: "Sample A",
values: {
Q1: 180,
Q2: 200,
Q3: 250,
whisker_low: 115,
whisker_high: 400,
outliers: [50, 100, 425]
}
},
{
label: "Sample B",
values: {
Q1: 180,
Q2: 200,
Q3: 250,
whisker_low: 115,
whisker_high: 400,
outliers: [50, 100, 425]
}
}
];
$scope.api.updateWithData($scope.data);
$scope.$apply();
}
}})();