[D3] Drawing path in D3
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的更多相关文章
- d3.js path路径
转自:http://www.d3js.cn/?p=68 svg的path标签被称为”可以组成任何形状的形状” SVG Path可以绘制任何形状的图形,包括矩形,圆形,椭圆,折线,多边形,直线,曲线等. ...
- D3学习之:D3.js中的12中地图投影方式
特别感谢:1.[张天旭]的D3API汉化说明.已被引用到官方站点: 2.[馒头华华]提供的ourd3js.com上提供的学习系列教程,让我们这些新人起码有了一个方向. 不得不说,学习国外的新技术真的是 ...
- D3中path各指令的含义
svg.append('path').attr({ id: 'mypath', d: 'M50 100Q350 50 350 250Q250 50 50 250' }) path 的指令有: 指令 参 ...
- [D3] Reuse Transitions in D3 v4
D3 transitions start executing as soon as they’re created, and they’re destroyed once they end. This ...
- [D3] Animate Transitions in D3 v4
D3 makes it easy to add meaningful animations to your data visualizations. Whether it’s fading in ne ...
- [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 ...
- [D3] Basic Interactivity with D3 v4
Data visualizations are a lot more interesting when they’re interactive. Whether it’s clicks, roll o ...
- D3.js 制作中国地图 .net 公共基础类
D3.js 制作中国地图 from: http://d3.decembercafe.org/pages/map/index.html GeoJSON is a format for encoding ...
- d3.js制作连线动画图和编辑器
此文章为原创文章,原文地址:https://www.cnblogs.com/eagle1098/p/11431679.html 连线动画图 编辑器 效果如上图所示.本项目使用主要d3.jsv4制作,分 ...
随机推荐
- AOP 动态添加函数
Function.prototype.before = function(beforefn) { // 保存原函数的引用 var self = this; // 返回包含了原函数和新函数的代理函数 r ...
- Chrome OS 70 发布:这是安卓的私生子吗?
谷歌于28日正式宣布推出Chrome OS 70.这个最新的Chrome OS系统在一些设计上具备了更多安卓风味,为配备了触摸屏的Chromebook.平板电脑和二合一设备带来了操作界面改善. 据9t ...
- Vim插件使用技巧(转)
在 IDEA Intellij小技巧和插件 一文中简单介绍了一下IdeaVim插件.在这里详细总结一下这个插件在日常编程中的一些常用小技巧.供有兴趣使用这个插件,但对Vim还不十分熟悉的朋友参考.当然 ...
- Linux系统消息队列框架Kafka单机安装配置
http://www.ithao123.cn/content-11128587.html
- Top 16 Java 应用类 - 这些功能再也不用自己写了
Java中有很多应用类.这些类定义静态方法能够解决非常多常见的问题.以下是通过5万个开源项目统计得到的最热门的16个应用类. 类按热门程序排列.类的方法也是按热门程序排序. 浏览这个类能够看看有哪些功 ...
- Linux下使用fstatfs/statfs查询系统相关信息
Linux下使用fstatfs/statfs查询系统相关信息 1. 功能 #include < sys/statfs.h > int statfs(const char *path, ...
- 利用gradle加入构建版本
在java的程序中,貌似都没有这个构建版本的概念.用的诸如eclipse. idea和android studio的IDE也没有直接提供构建版本的选项.只是我却想在android程序的版本其中加入一个 ...
- LaTeX Subfigure 中间加入垂直线
近期论文用到这个效果. 先实现下, 嘿嘿. \documentclass{article} \usepackage{tikz,lscape,amsmath} \usepackage[margin=1c ...
- java高质量缩放图片
可按照比例缩放,也可以指定宽高 import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JP ...
- iOS 创建静态库文件时去掉当中的Symbols
在project中创建静态库文件时.默认会将一些Symbols加到静态库文件里.这样做有两个缺点: 1.假设引用静态库文件的project中发生了bug.就会直接跳转到静态库的源代码. 也许有人问:静 ...