vue+ D3+drag


import template from './explore.vue' //import dataJson from './data.json' import * as d3 from 'd3' import Draggabilly from 'draggabilly' export default { ...template, name: 'explore', data(){ return { dataJson: { "name":"中国", "children": [{ "name":"广西" , "children": [ { "name":"桂林", "children": [ {"name":"秀峰区"}, {"name":"叠彩区"}, {"name":"象山区"}, {"name":"七星区", "children": [ {"name":"哈尔滨"}, {"name":"齐齐哈尔"}, {"name":"牡丹江"}, {"name":"大庆"} ] } ] }, {"name":"南宁"}, {"name":"柳州"}, {"name":"防城港"} ] }] } } }, mounted () { this.$nextTick(function () { let width = 600 let height = 600 let svg = d3.select('#tree-container').append('svg') .attr('width', 1500) .attr('height', 850) .append('g') .attr('transform', 'translate(40,0)') /* 定义了元素拖拽行为的原点 设置为圆的圆心位置可以避免明显的元素跳动 第一个参考连接中的例子可以看到明显的跳动 */ var drag = d3.drag() .subject(function() { var t = d3.select(this); return { x: t.attr("cx"), y: t.attr("cy") }; }) .on("drag", dragmove); /* 树状图 * */ let cluster = d3.tree() .size([width, height - 100]); // 此处省略请求方法 let nodes = d3.hierarchy(this.dataJson); let haveDeal = cluster(nodes); /** * 所有节点 * */ let lastAfterEdit = haveDeal.descendants(); /** * 所有连线 * */ let links = nodes.links(); /** * 画连线(垂直图用d3.linkVertical() x return d.x y return d.y) * */ let link = svg.selectAll('.link') .data(links) .enter() .append("path") .attr("class", "link") .attr("d", d3.linkHorizontal() .x(function (d) { return d.y; }) .y(function (d) { return d.x; })); /** * 画圆圈(垂直图 cx return d.x /cy return d.y) * */ var circles = svg.selectAll('.circle') .data(lastAfterEdit) .enter() .append("circle") .attr('cx', function (d) { return d.y; }) .attr('cy', function (d) { return d.x; }) .attr("r", 6) .attr('class','nodes-group') .attr("data", function (d) { return d.data.name; }) .call(drag); /** * 画文本(垂直图 x return d.x /y return d.y) * */ var texts = svg .selectAll('text') .data(lastAfterEdit) .enter() .append('text') .attr('x', function (d) { return d.y; }) .attr("id", function (d) { return d.data.name; }) .attr('y', function (d) { return d.x; }) .text(function (d) { return d.data.name; }) .style("text-anchor", function (d) { return d.children ? "end" : "start"; }) .attr("dx", function (d) { return d.children ? -10 : 10; }) .attr('dy', function (d) { return 5; }); function dragmove(d) { /* 只能拖拽圆 */ d3.select(this) .attr("cx", function() { return d.cx = d3.event.x }) .attr("cy", d.cy = d3.event.y) /* 拖拽圆后,要控制文字的上(减)、下(加)、左(减)、右方向 */ var txt = svg.select('#'+d.data.name) .attr('y',function(){ return d.y = event.offsetY }) .attr('x',function(){ return d.x = event.offsetX-40 }) /* 重新绘制线的路径 (x/y 同理)*/ link.attr("d", d3.linkHorizontal() .x(function (d) { return d.y; }) .y(function (d) { return d.x; })); } }) } } if (typeof window !== 'undefined' && window.Vue) { window.Vue.component('o-explore', template) }

  

转载于:https://www.cnblogs.com/caolidan/p/8993869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一个基于JavaScript的开源框架,用于构建用户界面。而D3.js是一个用于在网页上创建可交互数据可视化的JavaScript库。结合Vue.js和D3.js可以很方便地绘制流程图。 首先,我们需要在Vue.js项目中安装D3.js,可以通过npm安装。在终端中运行以下命令: npm install d3 安装完成后,在Vue.js组件中引入D3.js库: import * as d3 from 'd3'; 接下来,我们可以在Vue.js组件的生命周期函数中创建一个D3.js绘制流程图的函数。比如,在mounted钩子函数中: mounted() { this.drawFlowChart(); } 然后,在drawFlowChart函数中,我们可以使用D3.js提供的方法和API来绘制流程图。这个过程可以分为以下几步: 1. 创建svg元素,用于承载流程图的绘制: const svg = d3.select('body') .append('svg') .attr('width', '100%') .attr('height', '100%'); 2. 定义绘制流程图所需的数据,比如节点和连接线的位置和样式信息: const nodes = [ { id: 1, name: 'Node 1' }, { id: 2, name: 'Node 2' }, { id: 3, name: 'Node 3' }, // ... ]; const links = [ { source: 1, target: 2 }, { source: 2, target: 3 }, // ... ]; 3. 创建节点和连接线的元素,并设置其位置和样式: const node = svg.selectAll('.node') .data(nodes) .enter() .append('g') .attr('class', 'node') .attr('transform', d => `translate(${d.x}, ${d.y})`); node.append('rect') .attr('width', 100) .attr('height', 50); const link = svg.selectAll('.link') .data(links) .enter() .append('line') .attr('class', '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); 4. 可以根据需要添加交互功能,比如节点的点击事件、鼠标悬停效果等等。 至此,我们使用Vue.js和D3.js成功绘制了一个流程图。通过Vue.js的数据绑定和D3.js的数据驱动视图的特性,可以很方便地更新流程图的数据和样式,实现交互和动态效果。同时,D3.js也提供了丰富的API可以用于进行复杂的数据可视化操作,比如节点之间的动画过渡效果、缩放和拖拽等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值