[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 ...
随机推荐
- Linux 防火墙设置,禁止某个ip访问
service iptables status 查看防火墙状态 service iptables start 开启防火墙 service iptables ...
- windows 基础及基本软件测试环境搭建
- 练习2 F题 - 平方和与立方和
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 给定一 ...
- ASP.NET MVC轻教程 Step By Step 7——改进Write动作方法
在上一节我们使用强类型视图改进Write视图获得更好的智能感知和代码重构,现在可以进一步的改进动作方法. Step 1. 数据模型绑定 在Save方法中我们使用Request来获取表单传送的值,其实可 ...
- css3百叶窗轮播图效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- xcode 环境,多工程联编设置【转】
http://blog.csdn.net/vienna_zj/article/details/8467522 一.xcode4中的环境变量 $(BUILT_PRODUCTS_DIR) build成功后 ...
- Ed Burns谈HTTP/2和Java EE Servlet 4规范
在2015年JavaLand大会上,Ed Burns展示了Java EE Servlet 4.0规范(JSR 369)的概要,演讲的重点在于Java EE平台对HTTP/2的支持.HTTP/2旨在解决 ...
- codeforces C. Painting Fence
http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...
- SysErrorMessage 函数和系统错误信息表
在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示.但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢?FormatMessa ...
- Storm学习笔记
1.如何让一个spout并行读取多个流? 方法:任何spout.bolts组件都可以访问TopologyContext.利用这个特性可以让Spouts的实例之间划分流. 示例:获取到storm集群sp ...