下面这篇文章最终的结论就是 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. js实现图形报表 ECharts插件

    ECharts是一个很强大的插件 可以实现很多强大的图形报表功能 下图是部分官方实例 使用方法也是非常简单 首先打开插件的官方网站 http://echarts.baidu.com/echarts2/ ...

  2. 如何快速上手 angular.js

    摘要:angular.js 准确的来说,应该不是一个框架,是一个 js 库,一个依赖于 jQuery 的进一步封装,去除繁琐的 DOM 操作,使用数据驱动的 MVC 模块化库. 哎,很难受,连续两个大 ...

  3. 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 的目的是 ...

  4. 初识angular.js之爱恨情仇

    angular.js Angular.JS 是一组用来开发Web页面的框架.模板以及数据绑定和丰富UI组件.它支持整个开发进程,提供web应用的架构,无需进行手工DOM操作. AngularJS很小, ...

  5. [Angular JS教程] HeroService: getHeroes failed: undefined 问题解决方法

    最近在学习入门Angular JS,学习资源是https://angular.cn/tutorial, 在学习到 "https://angular.cn/tutorial/toh-pt6模拟 ...

  6. angular.js 嵌套路由

    介绍 AngularJS 嵌套路由:,来看看嵌套的ui-router状态是怎么回事. ui-router和同属AngularJS框架一部分的ng-route一样强大. ui-router提供了让我们可 ...

  7. 如何使用多个参数调用Angular.js过滤器?

    本文翻译自:How do I call an Angular.js filter with multiple arguments? As from the documentation , we can ...

  8. 如何在Angular.js选择框中使用默认选项

    本文翻译自:How to have a default option in Angular.js select box I have searched Google and can't find an ...

  9. angular.js phonecat翻译

    AngularJS 手机目录应用教程 概述 这个应用将带领开发者贯穿使用angularjs来开发一个web-app程序.这个应用程序是基于 Google Phone Gallery 但它现在已经不存在 ...

  10. Angular js 具体应用(一)

    1,首先引用Angular  百度静态资源库搜索Angular  复制链接,在HTML中嵌入script 最好写在正文下面 <script type="text/javascript& ...

最新文章

  1. 2007年上半年 网络工程师 上下午试卷【附带答案】
  2. 干货丨深度学习和经典机器学习的全方位对比
  3. java 范型 约束
  4. 关于Python编程的一些问答
  5. JavaScript|拖拽|仿Android手机九点连线开锁
  6. leetcode49. 字母异位词分组
  7. Linux系统:Centos7下搭建ClickHouse列式存储数据库
  8. Linux: Apache 安全设定
  9. 话里话外:传统到按单制造业的ERP变革
  10. 《EDIUS 6.5快刀手高效剪辑技法》——1.5 常用视频术语简介
  11. java OA办公管理系统 Springboot vue 前后分离 跨域 工作流 集成代码生成器java OA系统 流程审批 电子印章 手写文字识别 电子签名
  12. 软件测试项目反思,软件测试之项目线上问题总结与反思
  13. 5个最佳免费Linux杀毒软件
  14. IDM Internet Download Manager (IDM)临时文件夹
  15. 04 Nifty自定义带图片的下拉框
  16. MOOS程序解析记录(4)
  17. 设计模式-鸭子类型1
  18. webrtc jitterbuffer 学习
  19. 计算机网络的企业分析,计算机网络系统在企业中的应用分析
  20. 什么是GPT,如何克隆GPT类型的磁盘?

热门文章

  1. Java使用S3的一些操作
  2. 查 oracle 的sid,oracle 查询sid 运行的sql语句
  3. 老显卡都涨价了,所以我把坏的显卡拿出来修
  4. C++中一个类禁止继承好麻烦
  5. LINUX下载编译libogg
  6. LINUX使用sed完成文本文件的修改
  7. 由0.1+0.2-0.3不等于0说起
  8. 自己都看到满眼问题,还要请专业测试?
  9. 无锡硕放机场建议改名为金苏机场
  10. 解决办法:ImportError: 'module' object has no attribute 'check_specifier'