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.

  1. import React, {
  2. Component
  3. } from 'react';
  4. import * as d3 from 'd3';
  5. const data = {
  6. "nodes": [
  7. {"id": "Myriel", "group": },
  8. ...
  9. {"id": "Mme.Hucheloup", "group": }
  10. ],
  11. "links": [
  12. {"source": "Napoleon", "target": "Myriel", "value": },
  13. ...
  14. {"source": "Mme.Hucheloup", "target": "Enjolras", "value": }
  15. ]
  16. }
  17.  
  18. export default class ForceLayoutIntro extends Component {
  19.  
  20. componentDidMount() {
  21. const {
  22. width,
  23. height
  24. } = this.props;
  25.  
  26. const color = d3.scaleOrdinal(d3.schemeCategory20);
  27. const linkColor = d3.rgb('#c3c3c3');
  28.  
  29. function ticked (d) {
  30. link
  31. .attr("x1", (d) => d.source.x)
  32. .attr("y1", (d) => d.source.y)
  33. .attr("x2", (d) => d.target.x)
  34. .attr("y2", (d) => d.target.y);
  35. nodes
  36. .attr('cx', d => d.x)
  37. .attr('cy', d => d.y)
  38. }
  39.  
  40.       
  41.  
  42. const simulation = d3.forceSimulation()
  43. .force("link", d3.forceLink().id(function (d) {
  44. return d.id;
  45. }))
  46. .force("charge", d3.forceManyBody())
  47. .force("center", d3.forceCenter(width / , height / ));
  48.  
  49. const svg = d3.select(this.refs.mountPoint)
  50. .append('svg')
  51. .attr('height', height)
  52. .attr('width', width);
  53.  
  54. const link = svg
  55. .append('g')
  56. .attr('class', 'links')
  57. .selectAll('links')
  58. .data(data.links)
  59. .enter().append('line')
  60. .attr('stroke', linkColor.brighter(0.5))
  61. .attr('stroke-width', d => Math.sqrt(d.value))
  62.  
  63. const nodes = svg
  64. .append('g')
  65. .attr('class', 'nodes')
  66. .selectAll('circle')
  67. .data(data.nodes)
  68. .enter().append('circle')
  69. .attr('r', )
  70. .attr('fill', (d) => color(d.group))
  71. .call(d3.drag()
  72. .on("start", dragstarted)
  73. .on("drag", dragged)
  74. .on("end", dragended));
  75.  
  76. simulation
  77. .nodes(data.nodes)
  78. .on('tick', ticked);
  79.  
  80. simulation.force("link")
  81. .links(data.links);
  82.  
  83. function dragstarted(d) {
  84. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  85. d.fx = d.x;
  86. d.fy = d.y;
  87. }
  88.  
  89. function dragged(d) {
  90. d.fx = d3.event.x;
  91. d.fy = d3.event.y;
  92. }
  93.  
  94. function dragended(d) {
  95. if (!d3.event.active) simulation.alphaTarget();
  96. d.fx = null;
  97. d.fy = null;
  98. }
  99. }
  100.  
  101. // In render, we just need to create the container graph
  102. // The only important thing is we need to set ref
  103. // So that we can access the container in componentDidMount lifeCycle
  104. render() {
  105. const {
  106. width,
  107. height
  108. } = this.props;
  109. const style = {
  110. width,
  111. height,
  112. margin: '10px auto',
  113. border: '1px solid #323232',
  114. };
  115. return (
  116. <div style = {style} ref = "mountPoint"></div>
  117. );
  118. }
  119. }

[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. React开发实时聊天招聘工具 -第六章 登陆注册(1)

    1.基于cookie的用户认证 express 依赖 cookie-parser 2.axios语法: axios.get('/data').then(res=>{ if(res.status= ...

  2. nodeType 节点的类型

    元素结点 1 属性结点 2 attributes 文本结点 3 注释结点   8 document结点 9 documentFragment 11 文档碎片

  3. 利用反射实现Servlet公共类的抽取

    一次请求的执行过程: 请求:发送请求地址-->到达web.xml中,找到地址对应的servlet类-->通过反射调用该类的构造函数,创建该servlet类的对象-->通过当前对象调用 ...

  4. iscsi共享存储的简单配置和应用

    1.环境介绍 SCSI(Small Computer System Interface)是块数据传输协议,在存储行业广泛应用,是存储设备最基本的标准协议.从根本上说,iSCSI协议是一种利用IP网络来 ...

  5. Mybatis mapper.xml文件头文件备份

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  6. HDU 4971 A simple brute force problem.

    A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged o ...

  7. occActiveX - ActiveX with OpenCASCADE

    occActiveX - ActiveX with OpenCASCADE eryar@163.com Abstract. OpenCASCADE ActiveX wrapper for VB, C# ...

  8. 单线程实现检索当当网泄露的1GB用户数据

     新建项目dangdangusersearch 2.编写头文件head.h #ifndef _HEAD_H_ #define _HEAD_H_ #include <stdio.h>   ...

  9. 基于x86平台的Solaris安装视频(时长25分钟)

    基于X86平台的Solaris安装视频 本视频分为三个部分分别在附件中1.2.3(第三部分附件较大请在这里下载:http://down.51cto.com/data/263614) ,远程连接的视频由 ...

  10. Ansible学习记录六:Tower安装

    0.特别说明 1. 本文档没有特殊说明,均已root用户安装 2. 本文档中ftp传输文件的工具采用filezilla. 3. 本文档中的执行命令必须严格按照顺序而来. 4. 本文档中所用浏览器为Go ...