【转】Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax
http://897371388.iteye.com/blog/1975351
大致思路也就是下面,由于最近在学Laravel也在学Angularjs,加上之前做的项目用到了d3。
原来的方案如下:
jQuery+highchart.js+Django
jQuery主要于ajax,以及Json解析详情可见:http://api.phodal.com
现在的方案就变成了
Laravel+Angularjs+D3+Bootstrap
效果可见:www.xianuniversity.com/athome
最后效果图如下所示:
代码可见:https://github.com/gmszone/learingphp
框架简介
Laravel
Angular
AngularJS 是一个为动态WEB应用设计的结构框架。它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚、简洁地构建你的应用组件。它的创新点在于,利用数据绑定和依赖注入,它使你不用再写大量的代码了。这些全都是通过浏览器端的Javascript实现,这也使得它能够完美地和任何服务器端技术结合。
不过,一开始是考虑ember js,不喜欢谷歌学术化的东西。只是ember js的体积暂时让我失去了兴趣。。
D3
D3 是最流行的可视化库之一,它被很多其他的表格插件所使用。它允许绑定任意数据到DOM,然后将数据驱动转换应用到Document中。你可以使用它用一个数组创建基本的HMTL表格,或是利用它的流体过度和交互,用相似的数据创建惊人的SVG条形图。
Bootstrap
Bootstrap是Twitter推出的一个用于前端开发的开源工具包。它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架。
一个又一个的开源组合起来,形成了巨大的优势。就是对热门的技术感兴趣。。。(转载自Phodal's Blog)
创建RESTful
这个也就是由Lavarel来完成了。
- php artisan migrate:make create_athomes_table
打开对就的table进行修改,代码大致如下
- <?php
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class CreateAthomesTable extends Migration {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- Schema::create('athomes', function(Blueprint $table)
- {
- $table->increments('id');
- $table->float('temperature');
- $table->float('sensors1');
- $table->float('sensors2');
- $table->boolean('led1');
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::drop('athomes');
- }
- }
还需要在models下添加一个class
- <?php
- class Athomes extends Eloquent {
- protected $table = 'athomes';
- }
添加到routes.php
- Route::get('/athome/{atid}',function($atid){
- $atdata=Athomes::where('id','=',$atid)
- ->select('id','temperature','sensors1','sensors2','led1')
- ->get();
- return Response::json($atdata);
- });
再为其创建一个页面
- Route::get('/athome',function(){
- $maxid=Athomes::max('id');
- return View::make('athome')->with('maxid',$maxid);
- });
添加两个seeds
- class AthomesTableSeeder extends Seeder
- {
- public function run()
- {
- Athomes::create(array(
- 'temperature'=>'19.8',
- 'sensors1'=>'22.2',
- 'sensors2'=>'7.5',
- 'led1'=>False
- ));
- Athomes::create(array(
- 'temperature'=>'18.8',
- 'sensors1'=>'22.0',
- 'sensors2'=>'7.6',
- 'led1'=>False
- ));
- }
- }
然后,
- php artisan migrate
- php artisan db:seed
这样我们就完成了REST的创建
打开/athome/1看有没有出现相应的json数据
添加Angularjs
开始之前我们需要修改angularjs,默认的{{我选择了喜欢的<%,修改代码如下
- var myApp = angular.module('myApp', [], function($interpolateProvider) {
- $interpolateProvider.startSymbol('<%');
- $interpolateProvider.endSymbol('%>');
- });
让我们用一个简单的例子来测试下是否工作。
- function FetchCtrl($scope, $http, $templateCache) {
- $scope.method = 'GET';
- $scope.url = '<?= url('/athome/1') ?>';
- $scope.code = null;
- $scope.response = null;
- $http({method: $scope.method, url: $scope.url, cache: $templateCache}).
- success(function(data, status) {
- $scope.status = status;
- $scope.data = data;
- $.each(data,function(key,val){
- sensorsData.push(val.sensors1);
- })
- }).
- error(function(data, status) {
- $scope.data = data || "Request failed";
- $scope.status = status;
- log.l("Request Failed");
- });
- }
HTML代码
- <div id="App1" ng-app="myApp" ng-controller="FetchCtrl">
- <pre>http status code: <%status%></pre>
- <pre>http response data: <%data%></pre>
- </div>
至于为什么会写一个id="App1"是因为会出现一个id="App2",也就是两个angularjs的App,需要在第二个下面添加:
- angular.bootstrap(document.getElementById("App2"),['chartApp']);
那么效果应该如下所示:
- http status code: 200
- http response data: [{"id":1,"temperature":19.799999237061,"sensors1":22.200000762939,"sensors2":7.5,"led1":0}]
或如下图所示
D3
- var app = angular.module('chartApp', ['n3-charts.linechart']);
- app.controller('MainCtrl', function($scope, $http, $templateCache) {
- $scope.click=function(){
- $scope.options = {lineMode: 'cardinal',series: [{y: 'value', label: '温度', color: 'steelblue'}]};
- $scope.data=[{x:0,value:12}];
- $scope.url = '<?= url('/athome') ?>';
- $scope.url=$scope.url+'/'+{{$maxid}};
- log.l($scope.url);
- $scope.method = 'GET';
- $http({method: $scope.method, url: $scope.url, cache: $templateCache}).
- success(function(data, status) {
- $.each(data,function(key,val){
- $scope.data.push({x:1,value:val.sensors1});
- $scope.data.push({x:2,value:val.sensors2});
- log.l($scope.data);
- })
- }).
- error(function(data, status) {
- $scope.data = data || "Request failed";
- log.l("Request Failed");
- });
- }
- });
HTML代码如下:
- <div id="App2" ng-controller="MainCtrl">
- <button ng-click="click()" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span> Star
- 获取数据</button>
- <linechart data='data' options='options'></linechart>
- </div>
【转】Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax的更多相关文章
- 动态可视化 数据可视化之魅D3,Processing,pandas数据分析,科学计算包Numpy,可视化包Matplotlib,Matlab语言可视化的工作,Matlab没有指针和引用是个大问题
动态可视化 数据可视化之魅D3,Processing,pandas数据分析,科学计算包Numpy,可视化包Matplotlib,Matlab语言可视化的工作,Matlab没有指针和引用是个大问题 D3 ...
- Laravel 中使用 JWT 认证的 Restful API
Laravel 中使用 JWT 认证的 Restful API 5天前/ 678 / 3 / 更新于 3天前 在此文章中,我们将学习如何使用 JWT 身份验证在 Laravel 中构建 r ...
- 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍
4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...
- Charted – 自动化的可视化数据生成工具
Charted 是一个让数据自动生成可视化图表的工具.只需要提供一个数据文件的链接,它就能返回一个美丽的,可共享的图表.Charted 不会存储任何数据.它只是获取和让链接提供的数据可视化. 在线演示 ...
- 可视化数据包分析工具-CapAnalysis
可视化数据包分析工具-CapAnalysis 我们知道,Xplico是一个从pcap文件中解析出IP流量数据的工具,本文介绍又一款实用工具-CapAnalysis(可视化数据包分析工具),将比Xpli ...
- [翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据
原文地址:http://www.dotnetcurry.com/aspnet/1354/elastic-search-kibana-in-docker-dotnet-core-app 想要轻松地通过许 ...
- laravel数据库查询返回的数据形式
版本:laravel5.4+ 问题描述:laravel数据库查询返回的数据不是单纯的数组形式,而是数组与类似stdClass Object这种对象的结合体,即使在查询构造器中调用了toArray(), ...
- 【转】Python——plot可视化数据,作业8
Python——plot可视化数据,作业8(python programming) subject1k和subject1v的形状相同 # -*- coding: utf-8 -*- import sc ...
- [Python] Python 学习 - 可视化数据操作(一)
Python 学习 - 可视化数据操作(一) GitHub:https://github.com/liqingwen2015/my_data_view 目录 折线图 散点图 随机漫步 骰子点数概率 文 ...
随机推荐
- C++ Primer与c++编程思想的比较(转)
C++primer是最经典的c++教材之一,它的经典程度要超过thinking in c++.连thinking in c++作者本人都说他写这本书在某种程度上是让读者更好的理解C++primer.但 ...
- Android ActionBar 一步一步分析 (转)
原文摘自:http://blog.csdn.net/android2me/article/details/8874846 1.Action Bar 介绍 我们能在应用中看见的actionbar一般就是 ...
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP
D. Kay and Snowflake After the piece of a devilish mirror hit the Kay's eye, he is no longer int ...
- WinForm点击按钮,访问百度
命名空间 using System.Diagnostics; button的click事件中写入如下 Process.Start("http://www.xxx.com"); 注: ...
- Spring的属性编辑器
bean类 import java.util.Date; public class Bean { private Date date; public Date getDate() { return d ...
- 暴力枚举 UVA 10976 Fractions Again?!
题目传送门 /* x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k */ #include <cstdio> #inc ...
- HDU 4647 Another Graph Game(贪心)
题目链接 思路题.看的题解. #include <cstdio> #include <string> #include <cstring> #include < ...
- RGB
一,介绍 RGB色彩模式是工业界的一种颜色标准,是通过对红(R).绿(G).蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的, RGB即是代表红.绿.蓝三个通道的颜色,这个标准几 ...
- SQL 标量函数-----日期函数datediff()、 day() 、month()、year()
select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_uni ...
- Java虚拟机(JVM)中的内存设置详解
在一些规模稍大的应用中,Java虚拟机(JVM)的内存设置尤为重要,想在项目中取得好的效率,GC(垃圾回收)的设置是第一步. PermGen space:全称是Permanent Generation ...