Here we have a force layout with three nodes.

In the example, we will link three nodes with line and path:

import React, {Component} from 'react';
import * as d3 from 'd3'; const nodesData = [
{name: 'Alice', id: 0},
{name: 'Eve', id: 1},
{name: 'Bob', id: 2}
]; const linksData = [
{source: 0, target: 1},
{source: 1, target: 2},
{source: 2, target: 0}
]; export default class SimpleExample extends Component { componentDidMount() { const {width, height} = this.props;
// Create svg inside container
const svg = d3.select(this.refs.mountSvg)
.append('svg')
.attr('width', width)
.attr('height', height);
// Create Force layout
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function (d) {
return d.id;
}))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2)); // Create node
const nodes = svg
.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(nodesData)
.enter().append('circle')
.attr('r', width * 0.05)
.attr('fill', '#c3c3c3')
.call(d3.drag()
.on('start', dragstarted)
.on('drag', dragged)
.on('end', dragended));
simulation
.nodes(nodesData)
.on('tick', ticked); // Create link
const link = svg
.append('g')
.attr('class', 'links')
.selectAll('line')
.data(linksData)
.enter().append('line')
.attr('stroke', '#c2c2c2')
.attr('stroke-width', d => Math.sqrt(d.value)); const path = svg
.append('g')
.selectAll('path')
.data(linksData)
.enter().append('path')
.attr('fill', 'none')
.attr('stroke', '#777')
.attr('stroke-width', '2px')
.attr('class', 'link'); simulation
.force('link')
.distance(height / 2)
.links(linksData); function ticked() {
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, i)=> d.x)
.attr('cy',(d, i)=> d.y); path
.attr('d', (d, i) => {
const dx = d.target.x - d.source.x;
const dy = d.target.y - d.source.y;
const dr = Math.sqrt(dx * dx + dy * dy);
return `M${d.source.x},${d.source.y}A${dr},${dr} 0 0,1 ${d.target.x},${d.target.y}`;
})
} 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(0);
d.fx = null;
d.fy = null;
}
} render() {
const {width, height} = this.props;
const style = {
width,
height,
border: '1px solid black',
margin: '10px auto'
};
return (
<div style={style} ref="mountSvg"></div>
);
}
}

[D3] Drawing path in D3的更多相关文章

  1. d3.js path路径

    转自:http://www.d3js.cn/?p=68 svg的path标签被称为”可以组成任何形状的形状” SVG Path可以绘制任何形状的图形,包括矩形,圆形,椭圆,折线,多边形,直线,曲线等. ...

  2. D3学习之:D3.js中的12中地图投影方式

    特别感谢:1.[张天旭]的D3API汉化说明.已被引用到官方站点: 2.[馒头华华]提供的ourd3js.com上提供的学习系列教程,让我们这些新人起码有了一个方向. 不得不说,学习国外的新技术真的是 ...

  3. D3中path各指令的含义

    svg.append('path').attr({ id: 'mypath', d: 'M50 100Q350 50 350 250Q250 50 50 250' }) path 的指令有: 指令 参 ...

  4. [D3] Reuse Transitions in D3 v4

    D3 transitions start executing as soon as they’re created, and they’re destroyed once they end. This ...

  5. [D3] Animate Transitions in D3 v4

    D3 makes it easy to add meaningful animations to your data visualizations. Whether it’s fading in ne ...

  6. [D3] Margin Convention with D3 v4

    You can’t add axes to a chart if you don’t make room for them. To that end, the D3 community has ado ...

  7. [D3] Basic Interactivity with D3 v4

    Data visualizations are a lot more interesting when they’re interactive. Whether it’s clicks, roll o ...

  8. D3.js 制作中国地图 .net 公共基础类

    D3.js 制作中国地图 from:  http://d3.decembercafe.org/pages/map/index.html GeoJSON is a format for encoding ...

  9. d3.js制作连线动画图和编辑器

    此文章为原创文章,原文地址:https://www.cnblogs.com/eagle1098/p/11431679.html 连线动画图 编辑器 效果如上图所示.本项目使用主要d3.jsv4制作,分 ...

随机推荐

  1. 再次学习 Iterator 迭代器 与 Generator 生成器

    Iterator : 返回的结果是:{value, done} function chef(foods){ let i = 0; return { next(){ let done = ( i> ...

  2. codeforces111D. Petya and Coloring(组合数学,计数问题)

    传送门: 解题思路: 要求一条直线分割矩阵时左右颜色数一样,那么就说明一个问题.直线左右移动时是不会改变左右矩阵的颜色集合的.所以说明:2~m-1列的颜色集一定属于第一列与第m列颜色集的交集.而且第一 ...

  3. python程序转exe程序之一——cx_Freeze

    原始网页 : http://keliang.blog.51cto.com/3359430/661884 本人用的64位系统,一开始装了32位的cx_freeze,结果貌似无法自动找到本地的python ...

  4. Android数据库高手秘籍(三)——使用LitePal升级表

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/39151617 在上一篇文章中,我们学习了LitePal的基本使用方法,体验了使用框 ...

  5. 零基础学python-7.6 字符串格式化表达式

    字符串格式化同意在一个单个的步骤中对一个字符串运行多个特定类型的替换 特别是给用户提示的时候,格式化很方便 实现方法: 1.格式化表达式,类似于c语音的printf 在表达式中,我们使用%二进制操作符 ...

  6. IOS开发人员经常使用的10个Xcode插件

    IOS开发人员经常使用的10个Xcode插件 申请达人,去除赞助商链接 一个合适的插件意味着它能够适应不同的开发环境,Sublime Text 和TextMate就是非常好的样例.你知道Xcode也支 ...

  7. jquery09--Callbacks : 回调对象

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. ListView-添加head跟foot item 问题

    今天在使用ListView 的 addFooterView 的方法时候,遇到了一个问题.当我代码中执行了如下的操作 ListView listView = new ListView(this); li ...

  9. amaze ui和bootstrap有哪些差别?

    amaze ui和bootstrap有哪些差别? 问题 我最近在学amaze ui,感觉如果单从功能性来看和bootstrap最大差别也就是扁平化,不过妹子ui号称对国产本土化支持更好,这个具体表现在 ...

  10. Spark Tachyon实战应用(配置启动环境、运行spark和运行mapreduce)

    Tachyon实战应用 配置及启动环境 修改spark-env.sh 启动HDFS 启动Tachyon Tachyon上运行Spark 添加core-site.xml 启动Spark集群 读取文件并保 ...