下面这篇文章最终的结论就是 Flot 插件 结合 Angular 的Directive 来处理 图表的绘制

给出github上的一个demo源码。https://gist.github.com/flyysr/ba3a51cdbfcae7f53dec

最近项目中遇到了要显示图形报表的问题,项目的前端架构主要是基于 AngularJs 的,故,找js插件来显示图表(chart).

找到了Flot (http://www.flotcharts.org/), 说明一下,Flot是一个绘制图表的Js库。

下面问题的关键是怎么将 Flot和 Angular 整合起来。

----------------------------------------------------

显然,绘制图表涉及到大量的 DOM操作,而Angular的 Directive是有关html自定义标签的,所以这里的整合主要的用到 Angular的Directive的知识。

下面是我在StackOverflow上搜到的类似的提问:

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(这几个也是js绘制图表的插件), or any other charting solution.

下面的是 stackOverflow上的最佳答案,这里记录之。

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

2、Data can be kept in the controller

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

 And you create custom html tag (Can be an attribute too) 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 achive what we want .

Here the full Example

Intergate flot with Angular js ——Angular 图形报表的更多相关文章

  1. MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录

    注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...

  2. paip.提升效率--数据绑定到table原理和流程Angular js jquery实现

    paip.提升效率--数据绑定到table原理和流程Angular js  jquery实现 html #--keyword 1 #---原理和流程 1 #----jq实现的代码 1 #-----An ...

  3. 比較Backbone.js, Angular.js, Ember.js, Knockout.js 心得

    還記得第一次寫網站的時候,我無意間寫成了 SPA(single page application),當時還沒有SPA這個詞,後來因為廣告主需要不同 url location 頁面的廣告展示,只好把部分 ...

  4. paip.提高工作效率--数据绑定到table原则和过程Angular js jquery实现

    paip.提高工作效率--数据绑定到table原理和流程Angular js  jquery实现 html #--keyword 1 #---原理和流程 1 #----jq实现的代码 1 #----- ...

  5. Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结

    Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结 1. 本文范围 1 2. Angular的优点 1 2.1. 双向数据绑定 1 2.2. dsl ...

  6. angular.js:13920 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- testServe

    angular.js:13920 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- testSer ...

  7. (翻译)Angular.js为什么如此火呢?

    在本文中让我们来逐步发掘angular为什么如此火: Angular.js 是一个MV*(Model-View-Whatever,不管是MVC或者MVVM,统归MDV(model Drive View ...

  8. angular.js规范写法

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. angular.js写法不规范导致错误

    以下写法:没有明确指定module和controller,写法不规范. 更改angular.js版本会出bug. <html ng-app> <head> <title& ...

随机推荐

  1. 九、oracle 事务

    一.什么是事务事务用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败.如:网上转账就是典型的要用事务来处理,用于保证数据的 ...

  2. Linux Ubuntu 14.04安装LAMP(Apache+MySQL+PHP)网站环境

    从虚拟主机到VPS/服务器的过度,对于普通的非技术型的站长用户来说可能稍许有一些困难,麦子建议我们如果能够在虚拟主机环境中满足建站需要的, 还是用虚拟主机比较好.除非我们真的有需要或者希望从虚拟主机过 ...

  3. 当引用了Properties.Settings后,如果执行的时候,出现"配置系统无法初始化" 或者 某某节点不正确

    自定义了一个 PowerConfig命名空间   PowerSettings.Settings 然后一个exe,引用了该dll,在app.cinfig里增加了配置项 <applicationSe ...

  4. Javascript:scrollWidth,clientWidth,offsetWidth的区别(转)

    网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offsetW ...

  5. php文件去重复,二维数组筛选

    http://www.porter.com/fr/fr/product/648162|Sneakershttp://www.porter.com/fr/fr/product/642115|Bootsh ...

  6. 用js 将long类型转换成日期格式

    //扩展Date的format方法 Date.prototype.format = function (format) { var o = { "M+": this.getMont ...

  7. hdu_1513_Palindrome(LCS+滚动数组)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:给你一个字符串,问你最少插入多少个字符使其为回文字符. 题解:将字符串倒着保存,然后求一下 ...

  8. allocator 类

    allcator是一个模板类 定义在memory头文件中,将内存分配与对象构造分开,分配的内存是原始的.未构造的 一.how to use 因其实一个类,则使用allcator时需要首先声明一个类对象 ...

  9. Git 提交修改内容和查看被修改的内容

    我们将仓库里的readme.txt文件修改一下,改成如下内容: Git is a distributed version control systemGit is free software. 运行g ...

  10. Windows下搭建PHP开发环境【总结】

    一.准备工作-下载所需软件 Apache 进入apache服务器官网http://httpd.apache.org/ ,下面是下载的教程:http://jingyan.baidu.com/articl ...