Learn how to leverage d3's layout module to create a Force Layout inside of React. We'll take a look at React's lifecycle methods, using them to bootstrap d3's force layout in order to render our visualization.

import React, {
Component
} from 'react';
import * as d3 from 'd3';
const data = {
"nodes": [
{"id": "Myriel", "group": },
...
{"id": "Mme.Hucheloup", "group": }
],
"links": [
{"source": "Napoleon", "target": "Myriel", "value": },
...
{"source": "Mme.Hucheloup", "target": "Enjolras", "value": }
]
} export default class ForceLayoutIntro extends Component { componentDidMount() {
const {
width,
height
} = this.props; const color = d3.scaleOrdinal(d3.schemeCategory20);
const linkColor = d3.rgb('#c3c3c3'); function ticked (d) {
link
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
nodes
.attr('cx', d => d.x)
.attr('cy', d => d.y)
}        const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function (d) {
return d.id;
}))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / , height / )); const svg = d3.select(this.refs.mountPoint)
.append('svg')
.attr('height', height)
.attr('width', width); const link = svg
.append('g')
.attr('class', 'links')
.selectAll('links')
.data(data.links)
.enter().append('line')
.attr('stroke', linkColor.brighter(0.5))
.attr('stroke-width', d => Math.sqrt(d.value)) const nodes = svg
.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(data.nodes)
.enter().append('circle')
.attr('r', )
.attr('fill', (d) => color(d.group))
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)); simulation
.nodes(data.nodes)
.on('tick', ticked); simulation.force("link")
.links(data.links); function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
} function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
} function dragended(d) {
if (!d3.event.active) simulation.alphaTarget();
d.fx = null;
d.fy = null;
}
} // In render, we just need to create the container graph
// The only important thing is we need to set ref
// So that we can access the container in componentDidMount lifeCycle
render() {
const {
width,
height
} = this.props;
const style = {
width,
height,
margin: '10px auto',
border: '1px solid #323232',
};
return (
<div style = {style} ref = "mountPoint"></div>
);
}
}

[D3] Creating a D3 Force Layout in React的更多相关文章

  1. Creating a Custom Page Layout in SharePoint 2013

    Creating a Custom Page Layout in SharePoint 2013 In my last article, I documented how to create a Ma ...

  2. D3笔记01——D3简介与安装

    1 D3简介 发布于2011年,全称Data-Driven Documents,直译为“数据驱动的文档”. 简单概括为一句话:D3是一个Javascript的函数库,是用来做数据可视化的.文档指DOM ...

  3. A better way to learn D3 js - iLearning D3.js

    iLearning D3.js Basic is an iPad app to learn and code with D3. In 1.1 version, new tutorial is prov ...

  4. [D3] 13. Cleaner D3 code with selection.call()

    selection.call() method in D3 can aid in code organization and flexibility by eliminating the need t ...

  5. [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. Creating Isomorphic Apps with Node.js, React, and Express

    In this article, we’re going to use following software: React: the UI framework that can rendered on ...

  7. 初探React与D3的结合-或许是visualization的新突破?

    自诞生之初截止目前(2016年初),React可以说是前端界最流行的话题,如果你还不知道React是何物,你就该需要充充电了. d3是由纽约时报工程师开源的一个绘制基于svg的数据可视化工具,是近几年 ...

  8. D3.js(v3)+react框架 基础部分之数据绑定及其工作过程与绑定顺序

    数据绑定: 将数据绑定到Dom上,是D3最大的特色.d3.select和d3.selectAll返回的元素的选择集.选择集上是没有数据的. 数据绑定就是使被选择元素里“含有”数据. 相关函数有两个: ...

  9. D3.js (v3)+react框架 基础部分之认识选择集和如何绘制一个矢量图

    首先需要下载安装d3.js  :  yarn add d3 然后在组建中引入 :  import * as d3 from 'd3' 然后定义一个方法,在componentDidMount()这个钩子 ...

随机推荐

  1. 03013_JDBC工具类

    1.“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装工具类JDBCUtils.提供获取连接对象的方法,从而达到代码的重复利用. 2.该工具类提供方法:public static C ...

  2. 【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground

    [Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...

  3. 在JAVA中怎样跳出当前的多重嵌套循环?

    在JAVA中怎样跳出当前的多重嵌套循环?         这道题是考察大家对于break语句的应用.同一时候也是对你多重嵌套循环的使用进行考察.在java中,要想跳出多重循环,能够在外循环语句前面定义 ...

  4. C#制作文本转换为声音的demo,保存音频文件到本地

    TTS(Text To Speech)可以实现把文本转换成语音并朗读出来.Windows Xp可以使用Com组件--Microsoft Speech Object Library实现TTS,Windo ...

  5. Socket实例之客户端向服务端数据库上传文件UI版

    http://blog.csdn.net/su20145104009/article/details/52843735 首先实现分析: 1用户注册 客户单选择‘用户注册’,提示要输入用户名,密码,确认 ...

  6. vue+mui+html5+ plus开发的混合应用底部导航的显示与隐藏

    1. 模板相关内容(template) <template> <div> <transition :name="transitionName"> ...

  7. 玩转redux--从会用到庖丁解牛

    目录 为何而写 redux是什么 redux的设计哲学 redux的工作流 redux的几个核心要素 store action reducer actionCreator combineReducer ...

  8. 003 python 注释/数据类型/运算符/输入输出/格式化输出

    集成开发环境 pycharm 工欲善其事,必先利其器 pycharm是具备一般的python ide的功能,同时呢支持调试,语法高亮,代码管理,智能提示 加快快发的速度,提高开发效率 注释 what ...

  9. 【习题 8-11 UVA - 1615】Highway

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个村庄都有一个范围[li..ri] 只要在这个范围内放点都可以"支配"这个村庄. 则问题就等价于线段上有n个区 ...

  10. 利用淘宝ip库限制地区访问

    https://sss.one/97.html 利用淘宝ip库限制地区访问 有些应用可能需要对某些地区的用户进行限制访问 在采用才此方法的时候,可以利用一些ip库对访问者的ip进行判断 淘宝ip库地址 ...