Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Integrate Flot with AngularJS?

I am new to Angular and Flot, but am experienced with Jquery and Javascript. I am a bit confused about how to go about binding a Flot chart to Angular data models, since Flot is a JQuery plugin. I've searched around, but haven't been able to find an example.

I would also be happy to use highcharts, google-charts, or any other charting solution.

like image 324
JBCP Avatar asked Oct 27 '12 19:10

JBCP


People also ask

How to integrate Dialogflow chatbots with Angular JS?

To get started, you would need a DialogFlow chatbot or working knowledge of Dialogflows and Angular JS. To integrate the chatbots with Angular, you will need a Kommunicate account. To install the Angular CLI globally, open a new workspace and initial app project.

How can I improve the performance of my angular app?

Add a loading spinner: Also know as a throbber, this will be displayed until the task is done so that the end user knows that something is happening. Refactor the Angular Controller: Right now there’s too much happening (logic) in the controller. We need to move the majority of the functionality into a service. We’ll discuss both the why and how.

What versions of angular and Python does flask support?

03/22/2016: Upgraded to Python version 3.5.1 and Angular version 1.4.9. 02/22/2015: Added Python 3 support. Remember: Here’s what we’re building - A Flask app that calculates word-frequency pairs based on the text from a given URL.

How does the Poller work in Angular 2?

Within the poller () function, we called the /results/job_id endpoint. Using the $timeout service, this function continues to fire every 2 seconds until the timeout is cancelled when a 200 response is returned along with the word counts. Check out the Angular documentation for an awesome description of how the $timeout service works.


2 Answers

To use jQuery plugins with angularJS, you have to wrap them in directives, you can find some exemples of jQuery plugins directives by reading the source code of angularUI directives: https://github.com/angular-ui/angular-ui/tree/master/modules/directives

like image 29
Guillaume86 Avatar answered Oct 12 '22 02:10

Guillaume86


Since charting involves heavy DOM manipulation, directives are the way to go.

Data can be kept in the Controller

App.controller('Ctrl', function($scope) {     $scope.data = [[[0, 1], [1, 5], [2, 2]]]; }); 

And you can create a custom HTML tag1 specifying the model you want to get data from

 <chart ng-model='data'></chart> 

which angular can compile through a directive

App.directive('chart', function() {     return {         restrict: 'E',         link: function(scope, elem, attrs) {             var data = scope[attrs.ngModel];             $.plot(elem, data, {});             elem.show();         }     }; }); 

Example here.

This process is similar for most plugins that modify the DOM.

-*-

Also, you can watch for changes in the chart's underlying data and redraw it, so this way you can grab data through the $http service from your controller and update the view automatically. This can be achieved by modifying the linking function in the directive definition object

   var chart = null,        opts  = { };      scope.$watch(attrs.ngModel, function(v){         if(!chart){             chart = $.plot(elem, v , opts);             elem.show();         }else{             chart.setData(v);             chart.setupGrid();             chart.draw();         }     }); 

Notice the usage of Flot's API within the directive to achieve what we want.

Here the full example


1 Can be an attribute too.

like image 79
jaime Avatar answered Oct 12 '22 04:10

jaime