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的更多相关文章

  1. [React] react+redux+router+webpack+antd环境搭建一版

    好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...

  2. React/React Native 的ES5 ES6写法对照表

    //es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...

  3. React/React Native 的ES5 ES6写法对照表-b

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

  4. React: React组件的生命周期

    一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...

  5. React: React的属性验证机制

    一.简介 在开发中,属性变量类型的验证,几乎是任何语言都必须关注的问题,因为如果传入的数据类型不对,轻者程序运行仅仅是给出警告⚠️,严重的会直接导致程序中断,APP闪退或者web页面挂掉,这是很严重的 ...

  6. React/react相关小结

    React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...

  7. [React Fundamentals] Composable Components

    To make more composable React components, you can define common APIs for similar component types. im ...

  8. [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 ...

  9. [React] React Fundamentals: Mixins

    Mixins will allow you to apply behaviors to multiple React components. Components are the best way t ...

随机推荐

  1. etTimeout与setInterval方法的区别

    etTimeout与setInterval方法的区别 setTimeout()用于设定在指定的时间之后执行对应的函数或代码.,在全局作用域下执行 setTimeout(code,time[,args… ...

  2. 在 Sublime Text 3 中运行 PHP

    参考http://segmentfault.com/blog/tony/1190000000395951 把php添加到环境变量 1.我的电脑->属性->高级系统设置->高级-> ...

  3. MYSQL 错误 :Out of resources when opening file './datagather/mx_domain#P#p178.MYD' (Errcode: 24) 解决办法

    出现Out of resources when opening file './xxx.MYD' (Errcode: 24)错误是因为打开的文件数超过了my.cnf的--open-files-limi ...

  4. 那些年优秀的HTML5活动页面

    一个好的手机活动宣传 更能让人分享 传播是爆炸性的 下面是我平时看到一些好的微信活动宣传页面  分享给大家 其中用到的技术 常做微信活动 专题页面的人 可以看看大神们是怎么做的  这样到自己做的时候 ...

  5. PHPCMS标签:PC标签模板语法规则

    模板语法规则1.变量表示{$name} 被解析成 <?=$name?>,表示显示变量$name的值,其中的“name”由英文字母.数字和下划线组成首字母必须是英文字母或者下划线. 2.常量 ...

  6. Quartz1.8.5例子(九)

    /* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...

  7. 2015_WEB页面前端工程师_远程测题_东方蜘蛛_1

    请使用HTML+CSS实现如下效果: 1. 使用CSS Sprites,实现如图1效果,素材图片为: icons.png: 2. 使用脚本语言验证邮箱.密码的必填以及邮箱的合法性: 若验证失败,则出现 ...

  8. 如何分析matlab程序的主要效率问题

    利用profile on 在需要分析效率的程序段前后加入 profile on profile off 然后,在common line中输入profile viewer即可观察到这段程序的效率

  9. Uva_11462 GCD - Extreme (II)

    题目链接 题意: 给定一个n, 求:GCD(1, 2) + GCD(1, 3) + GCD(2, 3) + …… + GCD(1, n) + GCD(2, n) + …… + GCD(n-1, n); ...

  10. Codeforces 712C Memory and De-Evolution

    Description Memory is now interested in the de-evolution of objects, specifically triangles. He star ...