[React] React Fundamentals: Integrating Components with D3 and AngularJS
Since React is only interested in the V (view) of MVC, it plays well with other toolkits and frameworks. This includes AngularJS and D3.
A app with React and D3.js:
/** @jsx React.DOM */ var App = React.createClass({
getInitialState: function () {
return {
data: [
{val: 5},
{val: 4},
{val: 7},
{val: 6},
{val: 8},
{val: 1}
]
}
},
componentWillMount: function () { setTimeout(function () {
this.renderChart(this.state.data);
}.bind(this), 100) },
renderChart: function (dataset) { d3.select("body")
.selectAll('div')
.data(dataset)
.enter()
.append('div')
.attr('class', 'bar')
.style('height', function (d) {
console.log(d.val * 5 + 'px');
return d.val * 5 + 'px';
});
},
render: function () {
return (
<div id="chart"></div>
)
}
}); React.render(<App />, document.getElementById('panel'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React + D3 + AngularJS</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css">
</head>
<body id="panel"> <script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/JSXTransformer.js"></script>
<script src="bower_components/d3/d3.min.js"></script>
<script type="text/jsx" src="jsx/app.js"></script>
</body>
</html>
Integrating with Angular:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React + D3 + AngularJS</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css">
</head>
<body ng-app="app">
<div ng-controller="RenderChartController as chartCtrl">
<h1>chart 1</h1>
<renderchart data="chartCtrl.data" id="rchart"></renderchart>
<h1>chart 2</h1>
<renderchart data="chartCtrl.data2" id="rchart2"></renderchart>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<script src="bower_components/d3/d3.min.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/angular-app.js"></script>
</body>
</html>
/**
* Created by Answer1215 on 9/2/2015.
*/
///////////////
// controller
////////////// function RenderChartController($http){ var vm = this; $http.jsonp('http://filltext.com/?rows=010&val={randomNumber}&callback=JSON_CALLBACK')
.success(function (data) {
vm.data = data;
}); $http.jsonp('http://filltext.com/?rows=010&val={randomNumber}&callback=JSON_CALLBACK')
.success(function (data) {
vm.data2 = data;
});
} //////////////
// directive
//////////////
function renderchart(){
return {
restrict: 'E',
scope: {
data: '=',
id: '@'
},
link: function (scope, element, attrs) {
scope.$watch('data', function (newVal, oldVal) {
React.renderComponent(
App({data: scope.data, target: scope.id}),
element[0]
)
})
}
}
} angular.module('app', []) .controller('RenderChartController',RenderChartController)
.directive('renderchart', renderchart);
/** @jsx React.DOM */ var App = React.createClass({displayName: "App",
defaultProps: function () {
return {
data: {},
id: ''
}
},
componentWillReceiveProps: function (nextProp) {
if(nextProp.data){
this.renderChart(nextProp.data)
}
},
renderChart: function (dataset) {
d3.select("#" + this.props.target)
.selectAll('div')
.data(dataset)
.enter()
.append('div')
.attr('class', 'bar')
.style('height', function (d) {
return d.val * 5 + 'px';
});
},
render: function () {
return (
React.createElement("div", {id: this.props.target})
)
}
});
[React] React Fundamentals: Integrating Components with D3 and AngularJS的更多相关文章
- [React] react+redux+router+webpack+antd环境搭建一版
好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...
- React/React Native 的ES5 ES6写法对照表
//es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
- React: React组件的生命周期
一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...
- React: React的属性验证机制
一.简介 在开发中,属性变量类型的验证,几乎是任何语言都必须关注的问题,因为如果传入的数据类型不对,轻者程序运行仅仅是给出警告⚠️,严重的会直接导致程序中断,APP闪退或者web页面挂掉,这是很严重的 ...
- React/react相关小结
React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...
- [React Fundamentals] Composable Components
To make more composable React components, you can define common APIs for similar component types. im ...
- [React] React Fundamentals: Using Refs to Access Components
When you are using React components you need to be able to access specific references to individual ...
- [React] React Fundamentals: Mixins
Mixins will allow you to apply behaviors to multiple React components. Components are the best way t ...
随机推荐
- Window 下安装Redis
下载地址:https://github.com/dmajkic/redis/downloads. 下载到的Redis支持32bit和64bit.根据自己实际情况选择,将64bit的内容cp到自定义盘符 ...
- partial函数-python学习
一个函数可以有多个参数,而在有的情况下有的参数先得到,有的参数需要在后面的情景中才能知道,python 给我们提供了partial函数用于携带部分参数生成一个新函数. def add(a,b,c=2) ...
- c++清除输入缓冲区之 sync() vs ignore()
最近在写程序的时候总是不注意输入缓冲区内是否还有东西,导致出现了一些异常,调试了半天.所以来上一贴,学习注意,引以为戒! http://blog.chinaunix.net/uid-21254310- ...
- uboot环境变量区为何不能放在data段
一.疑问 环境变量也是全局变量,为何不能像其他的全局变量放在data段呢?为什么要放在堆中或者使用ENV_IS_EMBEDDED定义的CFG_ENV_SIZE的空间大小,又为什么需要这么大的空间呢? ...
- POJ 3083 Children of the Candy Corn bfs和dfs
Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8102 Acc ...
- 如何通过数据库修改WordPress后台登录密码
大家是否有过因为忘记WordPress后台登陆密码的时候?其实WordPress后台登陆密码的找回或修改的方法有多种,比如通过邮箱重启密码,又或者通过主机控制面板进入数据库修改等等.本篇教程以GoDd ...
- HDU 2993 MAX Average Problem dp斜率优化
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- bzoj 1004 1004: [HNOI2008]Cards burnside定理
1004: [HNOI2008]Cards Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1668 Solved: 978[Submit][Stat ...
- 树莓派学习路程No.1 GPIO功能初识 wiringPi安装
WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi遵守GUN Lv3.wiringPi使用C或者C++开发并且可以被其他语言包转,例如python.ruby或者PHP等.Wiri ...
- SQL server 触发器、视图
一.触发器 1.触发器为特殊类型的存储过程,可在执行语言事件时自动生效.SQL Server 包括三种常规类型的触发器:DML 触发器.DDL 触发器和登录触发器. 主要讲述DML触发器,DML触发器 ...