[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制作,分 ...
随机推荐
- 页面打开pdf格式文件的方法
<embed width=500 height=300 fullscreen=yes src="1.pdf" />
- android 4.4最新官方源代码下载
国内网络,日夜不休花了一个多月才下载成功android标准源代码,有些开发同人须要.已上传到网盘,分享给大家 微云地址: http://url.cn/PkkSzC 百度云盘地址(更新) http:// ...
- leetcode: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 第二天,导出文件sql,查询,视图view,聚合函数,反模式,字符串处理函数
//把数据库导出到脚本文件mysqldump -uroot -p1234 --databases abc > d:/a/abc.sql CREATE TABLE stud( id INT PRI ...
- jquery19 ajax()
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- jquery源码03 (3184 , 3295) support : 功能检测
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- vim 基础学习之插入模式
插入模式1.字符编码,插入特殊字符 <C-v>{3位} 如,你想输入A,你可以在输入模式下<C-v>065(必须是3位) <C-v>u{4位} 如,你想输入¿,你可 ...
- c++中sizeof()的用法介绍
1. 定义 sizeof是一个操作符(operator). 其作用是返回一个对象或类型所占的内存字节数. 2. 语法 sizeof有三种语法形式: 1) sizeof (obje ...
- 值得学习的CSS知识
这里零度给大家推荐几个值得学习的CSS技巧,能让你编写网页事半功倍!一.清除默认值 通常 padding 的默认值为 0,background-color 的默认值是 transparent.但是在不 ...
- Token ,Cookie和Session的区别--学习笔记
http://blog.csdn.net/tobetheender/article/details/52485948 Token token的意思是“令牌”,是用户身份的验证方式,最简单的token组 ...