【转】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 目录 折线图 散点图 随机漫步 骰子点数概率 文 ...
随机推荐
- 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 ...
- android 自定义弹出框AlertDialog ,很炫的哦
于是就小小的模仿了下自己写了这个这样的效果,主要代码如下:dlg = new AlertDialog.Builder(context).create();dlg.show();dlg.getWin ...
- 【转】有赞的kylin方案
http://tech.youzan.com/kylin-mondrian-saiku/
- Jquery操作
一.文档操作 1.内部插入:append(),appendTo(),prepend(): 2.外部插入:after(),before(): 3.删除操作:remove(),empty(): 4.克隆操 ...
- Kmp 算法模板 C
/** * name:KMP * time:2012-11-22 * 字符串快速匹配 */ #include<stdio.h> #include<string.h> typed ...
- 模拟 POJ 2993 Emag eht htiw Em Pleh
题目地址:http://poj.org/problem?id=2993 /* 题意:与POJ2996完全相反 模拟题 + 字符串处理:无算法,读入两行字符串找出相应点用used标记,输出时标记过的输出 ...
- Java集合的线程安全用法
线程安全的集合包含2个问题 1.多线程并发修改一 个 集合 怎么办? 2.如果迭代的过程中 集合 被修改了怎么办? a.一个线程在迭代,另一个线程在修改 b.在同一个线程内用同一个迭代器对象进行迭代. ...
- 【C语言】10-字符和字符串常用处理函数
一.字符处理函数 下面介绍的两个字符处理函数都是在stdio.h头文件中声明的. 1.字符输出函数putchar putchar(65); // A putchar('A'); // A int a ...
- python中的list的方法
list1=[1,3,5,"a"]print(dir(list1)) """ ['__add__', '__class__', '__contains ...
- spark-submit [options]
执行时需要传入的参数说明如下: Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 -- ...