[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 ...
随机推荐
- Faces.JavaServer Pages(JSP)
zhengly.cn atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. ...
- Apache 多站点(虚拟主机)
普遍 apache多站点(灰色(连接一起的红色)字体 为命令) 编辑文件:httpd.conf 找到以下内容: # Virtual hosts # Include /private/etc/apach ...
- LINUX防火墙firewall、iptables
(1) 重启后永久性生效: 开启: systemctl enable iptables.service'.ln -s '/usr/lib/systemd/system/iptables.service ...
- Visual c++ 2012 软件错误
vs2012 未能正确加载"Visual C++ Language Manager Package"包 解决办法 如下图所示: 到官网下载更新即可. http://www.micr ...
- 基于TCP的NAT子网穿透实验
不得不说,在国内IP紧缺的现状下,NAT发挥了无比巨大的作用:它以把IP和端口重新分配的方式,满足了广大人民群众上网的强烈需求.但是对于个人服务器以及在内网中基于网络的嵌入式设备,却是个比较尴尬的事情 ...
- VC6.0中重载操作符函数无法访问类的私有成员
整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...
- HDU 2993 MAX Average Problem dp斜率优化
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- bzoj 1079: [SCOI2008]着色方案 DP
1079: [SCOI2008]着色方案 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 803 Solved: 512[Submit][Status ...
- 教你把UIView切成任意形状
有时候layer.cornerRadius并不能满足需求,自己实现drawRect又太麻烦,怎么办? 多的不说,直接上代码: - (void)dwMakeBottomRoundCornerWithRa ...
- poj 3667 Hotel
昨天学习了线段树的延迟标记: 发现自己还有区间合并没有学: 抓紧时间学习一下: 代码: #include<cstdio> #include<algorithm> #define ...