[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})
        )
    }
});
时间: 2024-12-29 06:44:41

[React] React Fundamentals: Integrating Components with D3 and AngularJS的相关文章

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

转载: http://bbs.reactnative.cn/topic/15/react-react-native-%E7%9A%84es5-es6%E5%86%99%E6%B3%95%E5%AF%B9%E7%85%A7%E8%A1%A8 英文版: https://babeljs.io/blog/2015/06/07/react-on-es6-plus 很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends

[React Fundamentals] Composable Components

To make more composable React components, you can define common APIs for similar component types. import React from 'react'; import ReactDOM from 'react-dom'; export default class App extends React.Component{ constructor(){ super(); this.state = { re

[React] React Router: Named Components

In this lesson we'll learn how to render multiple component children from a single route. Define a named component by "components": <Route path="/other" components={ {header: Other, body: OtherBody}}></Route> 'header' and '

[React] React Fundamentals: with-addons - ReactLink

It can be tedious to type out all the boilerplate needed to get the DOM and states in React to synchronize. Luckily, React provides a version of the toolkit with a selection of available addons. This lesson is going to dig into ReactLink, and how thi

[React] React Fundamentals: Precompile JSX

The JSX Transformer library is not recommended for production use. Instead, you'll probably want to precompile your JSX into JavaScript. Install: npm install react-tools -g Run: jsx dev build --no-cache-dir --watch //watch dev dir and compile js to b

[React] React Router: Nested Routes

Since react-router routes are components, creating nested routes is as simple as making one route a child of another in JSX. Make the nested component: class App extends React.Component { render(){ return( <Router history={hashHistory}> <Route pa

[React] React Router: Router, Route, and Link

In this lesson we'll take our first look at the most common components available to us in react-router; Router, Route, and Link. import React from 'react'; import {hashHistory, Route, Router, Link} from 'react-router'; const Home = () => <div><

[React] React Router: IndexRoute

IndexRoute allows us to define a default child component to be rendered at a specific route when no other sub-route is available. When Home page display, we also make About component as default Route to dsiplay, only when use click Contact link, then

[React] React Router: Querystring Parameters

Define query param in Link, accept path and query : const Links = () => <nav > <Link to={{path: '/', query: {message: 'Yo'}}}>Home</Link> </nav>; Use Query param by props.location.query: const Container = (props) => <div&g