Intergate flot with Angular js ——Angular 图形报表
下面这篇文章最终的结论就是 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();
}
};
});
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 .
Intergate flot with Angular js ——Angular 图形报表的更多相关文章
- 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 的目的是 ...
- paip.提升效率--数据绑定到table原理和流程Angular js jquery实现
paip.提升效率--数据绑定到table原理和流程Angular js jquery实现 html #--keyword 1 #---原理和流程 1 #----jq实现的代码 1 #-----An ...
- 比較Backbone.js, Angular.js, Ember.js, Knockout.js 心得
還記得第一次寫網站的時候,我無意間寫成了 SPA(single page application),當時還沒有SPA這個詞,後來因為廣告主需要不同 url location 頁面的廣告展示,只好把部分 ...
- paip.提高工作效率--数据绑定到table原则和过程Angular js jquery实现
paip.提高工作效率--数据绑定到table原理和流程Angular js jquery实现 html #--keyword 1 #---原理和流程 1 #----jq实现的代码 1 #----- ...
- Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结
Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结 1. 本文范围 1 2. Angular的优点 1 2.1. 双向数据绑定 1 2.2. dsl ...
- angular.js:13920 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- testServe
angular.js:13920 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- testSer ...
- (翻译)Angular.js为什么如此火呢?
在本文中让我们来逐步发掘angular为什么如此火: Angular.js 是一个MV*(Model-View-Whatever,不管是MVC或者MVVM,统归MDV(model Drive View ...
- angular.js规范写法
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- angular.js写法不规范导致错误
以下写法:没有明确指定module和controller,写法不规范. 更改angular.js版本会出bug. <html ng-app> <head> <title& ...
随机推荐
- SQL IO监控
DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO { ON | OFF } SET STATISTICS TIME { ON | OFF }
- 第三十二节,datetime时间模块
首先要引入import datetime时间模块 datetime.date.today()模块函数 功能:输出系统年月日输出格式 2016-01-26[无参] 使用方法:datetime.date. ...
- H5的新应用-指定视频的播放进度
<!DOCTYPE html> <html> <head> <title>指定视频的播放进度</title> <meta http-e ...
- tomcat 高并发配置 与优化
公司的一个服务器使用Tomcat6默认配置,在后台一阵全点击服务器就报废了,查了一下就要是PERMSIZE默认值过小造成(16-64) TOMCAT_HOME/bin/catalina.sh 添加一行 ...
- java 利用SMB读取远程文件
package com.yss.test.FileReadWriter; import java.io.BufferedInputStream; import java.io.BufferedO ...
- EF中用Newtonsoft.Json引发的循环引用问题
描述: 1.双向关系表a->b b->aList 2.在查询a引用b以后 3.用Newtonsoft.Json 去tojsonstring 4.一个只有6条数据的json串 出现了一屏幕字 ...
- mysql数据库参数innodb_buffer_pool_size和max_connections
接到报故,查看mysql数据库以下参数 1.innodb_buffer_pool_size 2.max_connections 该参数定义了数据缓冲区buffer pool大小,类似于oracle的d ...
- 17 个 tar 命令实用示例【转】
Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...
- iOS传值之通知传值(三)
输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字典,将label的值通过key值设置传递 ...
- 访问WEB-INF下的jsp/html
基于Servlet的声明,WEB-INF不作为Web应用的公共文档树的一部分.因此,WEB-INF 目录下的资源不是为客户直接服务的.因此客户端不能直接访问WEB-INF下的文件,但是可以通过requ ...