D3Vueecharts個人亂記

利用d3+vue開發的一個網絡拓撲圖

https://copyfuture.com/blogs-details/20200710101052238h32wazjmcii49dr

一開始用的是echart畫的。

根據https://gallery.echartsjs.com/editor.html?c=xH1Rkt3hkb,成功畫出簡單的節點關系。

如圖:?

?

?

?

總結——

【優點】:關系一目了然,可以鼠標懸浮查看相鄰節點,其他節點淡化。

【缺點】:拖動結果不理想,尤其是數據過多時,一旦拖動一個,整個頁面所有的節點都在動,很久都無法停止(可能是我配置方法不對,但是后續沒找到解決方法)

?


?

于是轉而使用d3力導圖。

?

?

?

?

? 除了基本的節點展示和拖動之外,還可以雙擊新增節點,以及右擊展示節點詳情。

核心操作有以下:

1、繪制graph力

var simulation = d3
.forceSimulation(nodes)
.force(
'collide',
d3
.forceCollide()
.radius(() => 30)
.iterations(2)
)
.force(
'charge',
d3
.forceManyBody()
// .distanceMax(300)
.strength(-400)
)
.force(
'link',
d3
.forceLink(links)
.id(d => d.id)
.distance(100)
)
.force('center', d3.forceCenter(this.width / 2, this.height / 2))
// .force('x', d3.forceX(this.width / 2))
// .force('y', d3.forceY(this.height / 2))

2、繪制存放節點和關系的svg

var svgArea = d3
.select('.containers')
.append('svg')
.attr('viewBox', [0, 0, this.width, this.height])
.attr('class', 'd3Test')
.call(
d3.zoom().on('zoom', function() {
g.attr('transform', d3.event.transform)
})
)
.on('dblclick.zoom', () => {}) // 禁止雙擊放大

const g = this.svgArea.append('g').attr('class', 'content')

3、繪制節點關系

var links = g
.append('g')
.attr('class', 'links')
.selectAll('path')
.data(links, function(d) {
if (typeof d.source === 'object') {
return d.source.name + '_' + d.relationship + '_' + d.target.name
} else {
return d.source + '_' + d.relationship + '_' + d.target
}
})
.join('path')
.attr('stroke-width', d => Math.sqrt(d.value))
.attr('class', 'link')
.attr('id', function(d) {
if (typeof d.source === 'object') {
return d.source.name + '_' + d.relationship + '_' + d.target.name
} else {
return d.source + '_' + d.relationship + '_' + d.target
}
})

4、繪制節點

var nodes = g
.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(nodes, d => d.name)
.join('circle')
.attr('r', d => (d.number ? d.number : 20))
.attr('class', 'node')
.attr('stroke', '#fff')
.attr('stroke-width', 1.5)
.attr('fill', this.color)
.on('dblclick', this.dbclickNode)//雙擊節點事件
.on('click', this.clickNode)//單擊節點觸發事件
// .on('mouseover', this.mouseoverNode)
// .on('mouseout', this.mouseoutNode)
.call(this.drag(this.simulation))

nodes.append('title').text(d => d.name)

5、然后還有個讓節點緩慢停止下來的tick

 this.simulation.on('tick', () => {
this.links.attr('d', function(d) {
if (d.source.x < d.target.x) {
return (
'M ' +
d.source.x +
' ' +
d.source.y +
' L ' +
d.target.x +
' ' +
d.target.y
)
} else {
return (
'M ' +
d.target.x +
' ' +
d.target.y +
' L ' +
d.source.x +
' ' +
d.source.y
)
}
})
this.nodes
.attr('cx', function(d) {
if (d.fixed) {
d.fx = nodes[d.index].x
}
return d.x
})
.attr('cy', function(d) {
if (d.fixed) {
d.fy = nodes[d.index].y
}
return d.y
})
this.nodesName.attr('x', d => d.x).attr('y', d => d.y)
})

附上官網案例:https://observablehq.com/@d3/force-directed-graph

這個案例的版本好像比較老,個人建議用新版,不過新版的API有改動。

?

參考案例:https://eisman.github.io/neo4jd3/

?

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247298.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247298.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247298.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vue中使用Vue-pdf在線預覽

下載 npm i vue-pdf 引入(在所需要預覽的頁面) <script>import axios from axiosimport pdf from vue-pdfimport CMapReaderFactory from vue-pdf/src/CMapReaderFactory.js // 加載中文的包export default {components: {pdf},data () {return {numPages:&#xff0c;…

oracle 自定義 聚合函數

Oracle自定義聚合函數實現字符串連接的聚合 create or replace type string_sum_obj as object ( --聚合函數的實質就是一個對象 sum_string varchar2(4000), static function ODCIAggregateInitialize(v_self in out string_sum_obj) return number, --對象初始化 member func…

Vue3里的setup中使用vuex

useStore 這里我們可以直接從vuex 4.X中解構出useStore方法&#xff0c;就可以在setup中使用vuex的相關函數 template 使用$store <template><div><h2>{{ $store.state.count }}</h2><button click"increaseCount">點擊</button…

JQ 取CHECKBOX選中項值

備忘錄 $("[namecheckbox]:checked").each(function(){ alert((this).val()); }) ;轉載于:https://www.cnblogs.com/showblog/archive/2010/09/13/1825099.html

vue3 echarts5 graph關系圖譜 點擊圖例節點消失線不消失重復生成問題

const myChart ref(null);const myCharts ref(null);onMounted(() > {// 這種會導致線仍然存在 重復生成myCharts.value echarts.init(myChart.value);myCharts.value.setOption(option);});return {myChart,myCharts,}; 現象&#xff1a;如下圖1 點擊圖例類目2&#xf…

非常完整的coco screator socketio

https://github.com/SeaPlanet/cocoscreator_chat 前端源碼 https://github.com/socketio/socket.io-client https://cdnjs.com/libraries/socket.io 轉載于:https://www.cnblogs.com/suneil/p/11288628.html

JavaScript 中 obj.hasOwnProperty(prop) 方法

語法 obj.hasOwnProperty(prop) 參數 prop 要檢測的屬性的 String 字符串形式表示的名稱&#xff0c;或者 Symbol。 返回值 用來判斷某個對象是否含有指定的屬性的布爾值 Boolean。 描述 所有繼承了 Object 的對象都會繼承到 hasOwnProperty 方法。這個方法可以用來檢測…

python面向對象初識

面向對象編程 1.面向對象初步了解 ? 面向過程編程與函數編程對比&#xff1a; s1 ajdsgkaffddha count 0 for i in s1:count 1 print(f字符串的長度為{count}) # 面向過程編程每計算一次便使用一次for循環def my_len(s): # 計算數據類型長度的函數&#xff0c;可重復使用…

Vue3 VSCode新建項目報錯The template root requires exactly one element.

1.首先我們點擊左側第四個圖標插件2.輸入框搜索vetur插件3.點擊設置圖標&#xff0c;再點擊擴展設置4.搜素vetur>validation>template&#xff0c;取消vetur>validation>template的勾選 然后就不會報錯了

計算機視覺概述

關于計算機視覺的介紹性文章&#xff0c;包括計算機視覺的定義&#xff0c;和人類視覺的區別以及涉及到的學科等等。 1. 什么是計算機視覺 計算機視覺既是工程領域&#xff0c;也是科學領域中的一個富有挑戰性重要研究領域。計算機視覺是一門綜合性的學科&#xff0c;它已經吸引…

Java生鮮電商平臺-電商支付流程架構實戰

Java生鮮電商平臺-電商支付流程架構實戰 說明&#xff1a;我一直秉承的就是接地氣的業務架構實戰。我的文章都有一個這樣的核心。 1. 業務場景 2. 解決問題。 3.代碼實現。 4.代碼重構。 5.總結與復盤。 6.缺點與防范 一、場景描述 想必大家都曾遇到過這個問題&#xff0c;在電…

vue3.0 AntDesignVue2.0 table的rowkey報錯問題解決方法

Warning: [antdv: Each record in table should have a unique key prop,or set rowKey to an unique primary key.] Warning: [antdv: Table] Each record in dataSource of table should have a unique key prop, or set rowKey of Table to an unique primary key 提示因為…

模式識別掃盲

模式識別是對表征事物或現象的各種形式的信息進行處理和分析&#xff0c;以對事物或現象進行描述、辨認、分類和解釋的過程&#xff0c;是信息科學和人工智能的重要組成部分。英文“Pattern”源于法文“Patron”&#xff0c;本來是指可作為大家典范的理想的人&#xff0c;或用以…

vue2項目使用codemirror插件實現代碼編輯器功能

1、使用npm安裝依賴 npm install --save codemirror 2、在頁面中放入如下代碼 <template><textarea ref"mycode" class"codesql" v-model"code" style"height:200px;width:600px;"></textarea> </template>…

CentOS 6.5系統安裝配置LAMP(Apache+PHP5+MySQL)服務器環境

安裝篇&#xff1a; 一、安裝Apache yum install httpd #根據提示&#xff0c;輸入Y安裝即可成功安裝 /etc/init.d/httpd start#啟動Apache 備注&#xff1a;Apache啟動之后會提示錯誤&#xff1a; 正在啟動 httpd:httpd: Could not reliably determine the servers fully qual…

前端有用的庫

HTML awesome-html5 精選的HTML5資源精選清單 CSS tailwindcss 與Tailwind CSS相關的很棒的事情awesome-css-frameworks 很棒的CSS框架列表awesome-css-cn CSS 資源大全中文版&#xff0c;內容包括&#xff1a;CSS預處理器、框架、CSS結構、代碼風格指南、命名習慣等等awesom…

計算機視覺牛人(轉載)(最早在自動化所論壇上發現的)

paper畢竟是死的, 寫paper的人才是活的. 那么我現在研究一下cv圈的格局, 按師承關系, 借鑒前人, 我總結a tree stucture of cv guys.David Marr----->Shimon Ullman (Weizmann) ----->Eric Grimson (MIT)----->Daniel Huttenlocher (Cornell)----->Pedro Felzenszw…

Java生鮮電商平臺-促銷系統的架構設計與源碼解析

Java生鮮電商平臺-促銷系統的架構設計與源碼解析 說明&#xff1a;本文重點講解現在流行的促銷方案以及源碼解析,讓大家對促銷&#xff0c;納新有一個深入的了解與學習過程. 促銷系統是電商系統另外一個比較大&#xff0c;也是比較復雜的系統&#xff0c;作為一個賣貨的&#x…

vue3中websocket用法

1.0 認識 websocket #1.0.1 什么是 websocket 和 http 協議類似&#xff0c;websocket 也是是一個網絡通信協議&#xff0c;是用來滿足前后端數據通信的。 #1.0.2 websocket 相比于 HTTP 的優勢 HTTP 協議&#xff1a;客戶端與服務器建立通信連接之后&#xff0c;服務器端只…

介紹幾個醫學圖像處理會議

Information Processing in Medical Imaging &#xff0c; IPMI &#xff0c;醫學圖像處理最頂級的會議&#xff0c;兩年召開一次&#xff0c;全球大概入選 50 篇左右&#xff0c;一個非常小圈子的會&#xff0c;據說通常是關在一個偏僻的地方開一周&#xff0c;會議口頭報告提…