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& ...
随机推荐
- LR错误整理
1.LoadRunner超时错误: 在录制Web服务器端,如果超过120秒服务器协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 错误现象1:Action.c(16): Er ...
- Linux查找文件中的字符串命令
grep -nr 'archermind' -r, --recursive Read all files under each directory, recursively, following sy ...
- wordpress建站过程4——index.php
<?php get_header(); ?> <div id="primary" class="content-area col-md-9"& ...
- android中分割线的实现
一种是在线性布局中天家虚线的图片 一种是在3.0以后实现的 在UI中配置 android:layout_width="match_parent&quo ...
- VBS脚本合集(自制脚本)
一.视频搜索器选择: Selectitem() '######################################### Function Selectitem() 'Set Vedio ...
- Entity Framework中对存储过程的返回值的处理
很早就开始注意到EF了,但一直没有机会用,换了工作后,第一个项目就使用EF6进行开发. 项目不是很大,EF完全可以胜任. 但是开发过程中,难免还是会遇到一些复杂的运算,需要频繁访问数据库. 此时,想到 ...
- 安卓四大组件的作用、安卓Service的作用
Activity好像是应用程式的眼睛,提供与user互动之窗. BroadcastReceiver好像是耳朵,接收来自各方的Intent. Service是在后台运行的. 一个Service 是一段长 ...
- 安卓srcCompat弄死我了
<ImageView android:layout_width="150dp" android:layout_height="120dp" app:src ...
- 用Chrome开发者工具做JavaScript性能分析
来源: http://blog.jobbole.com/31178/ 你的网站正常运转.现在我们来让它运转的更快.网站的性能由页面载入速度和代码执行效率决定.一些服务可以让你的网站载入更快,比如压缩J ...
- DataGridView直接导出EXCEL
1public void DataToExcel(DataGridView m_DataView) { SaveFileDialog kk = new SaveFileDialog(); kk.Tit ...