• 【组件封装】基于neo4jD3封装关系图、关联图谱


    目录

     前言

     效果图:

    使用

    第一步

    第二步 

    目录结构

    组件代码: 


    前言

    有时在开发中用过的一些比较好的功能没有及时封装,导致日后再用的时候还得再来一遍,现在是想将这些功能封装成组件,做到“”拿来即用“”,为了更好地照顾到版本兼容,我直接将资源下载到本地作为静态资源使用,这个组件基于vue2,如果你是个新手不会将之转为vue3或者react等框架,可以留言,我看到后会更新,这里默认大家具有一定经验。


    这个组件的好处:相比于直接使用D3或者echarts,他的优点在于有大量已经内置的功能,且可以通过修改源码方式轻松定制,而不必从0开始写。具体更多的使用方式可以参考文档:

    https://github.com/eisman/neo4jd3


     

     效果图:

     


    好了,言归正传,接下来的步骤是具体封装为全局组件。你如果跟着我的步骤使用,我确保你100%可以成功。


    使用


     

    第一步

    public文件夹中, 将以下:

    一个css文件放入static下的css中;

    两个js文件放入static下的js文件中。

     依赖源码:我去字数太多,发不出来,大家请自行下载吧。

    neo4jd3.css:https://github.com/eisman/neo4jd3


     d3.min.js:https://github.com/eisman/neo4jd3


    neo4jd3.js :https://github.com/eisman/neo4jd3


    第二步 

    components文件夹下新建Neo4jD3文件夹,新建index.vue文件,复制以下代码粘贴过去,然后再index.js里注册全局组件

    类似这样:

    main.js 里面引入vue.use一下即可


     

     目录结构

     


     

    组件代码: 

    1. <script>
    2. export default {
    3. props: {
    4. Neo4jD3JsonList: Object
    5. },
    6. data () {
    7. return {
    8. nodeData: {
    9. "results": [{
    10. "columns": ["user", "entity"],
    11. "data": [{
    12. "graph": {
    13. "nodes": [{
    14. "id": "1",
    15. "labels": ["User"],
    16. "properties": {
    17. "userId": "eisman"
    18. }
    19. }, {
    20. "id": "2",
    21. "labels": ["Phone"],
    22. "properties": {
    23. "value": "555-555-5555"
    24. }
    25. }, {
    26. "id": "3",
    27. "labels": ["Address"],
    28. "properties": {
    29. "zipCode": "90210",
    30. "country": "US",
    31. "city": "Beverly Hills",
    32. "state": "CA"
    33. }
    34. }, {
    35. "id": "4",
    36. "labels": ["BirthDate"],
    37. "properties": {
    38. "value": 1326322800000
    39. }
    40. }, {
    41. "id": "5",
    42. "labels": ["Password"],
    43. "properties": {
    44. "value": "123456"
    45. }
    46. }, {
    47. "id": "6",
    48. "labels": ["Device"],
    49. "properties": {
    50. "value": "eisman"
    51. }
    52. }, {
    53. "id": "7",
    54. "labels": ["SecurityChallengeAnswer"],
    55. "properties": {
    56. "value": "hCxh4LItmWefWg71JiYUaxxFrCRaqQIDEoEbeqHa"
    57. }
    58. }, {
    59. "id": "8",
    60. "labels": ["Project"],
    61. "properties": {
    62. "name": "neo4jd3",
    63. "title": "neo4jd3.js",
    64. "description": "Neo4j graph visualization using D3.js.",
    65. "url": "https://eisman.github.io/neo4jd3"
    66. }
    67. }, {
    68. "id": "9",
    69. "labels": ["Git"],
    70. "properties": {
    71. "url": "https://github.com/eisman/neo4jd3"
    72. }
    73. }, {
    74. "id": "10",
    75. "labels": ["Issues"],
    76. "properties": {
    77. "url": "https://github.com/eisman/neo4jd3/issues"
    78. }
    79. }, {
    80. "id": "11",
    81. "labels": ["Github"],
    82. "properties": {
    83. "url": "https://github.com"
    84. }
    85. }, {
    86. "id": "12",
    87. "labels": ["Project"],
    88. "properties": {
    89. "name": "neo4j",
    90. "title": "Neo4j",
    91. "description": "Graphs for Everyone",
    92. "url": "http://neo4j.com"
    93. }
    94. }, {
    95. "id": "13",
    96. "labels": ["Project"],
    97. "properties": {
    98. "name": "d3",
    99. "title": "D3.js",
    100. "description": "Bring data to life with SVG, Canvas and HTML.",
    101. "url": "https://d3js.org/"
    102. }
    103. }, {
    104. "id": "14",
    105. "labels": ["Email"],
    106. "properties": {
    107. "email": "eeisman@gmail.com"
    108. }
    109. }, {
    110. "id": "15",
    111. "labels": ["CreditCard"],
    112. "properties": {
    113. "number": "4916928406205705",
    114. "type": "visa"
    115. }
    116. }, {
    117. "id": "16",
    118. "labels": ["Options"],
    119. "properties": {}
    120. }, {
    121. "id": "17",
    122. "labels": ["Language"],
    123. "properties": {
    124. "lang": "en_us"
    125. }
    126. }, {
    127. "id": "18",
    128. "labels": ["Cookie"],
    129. "properties": {
    130. "value": "itgnxe0xmvb1tazaqmkpmfzg8m3ma62qskfwcexc"
    131. }
    132. }, {
    133. "id": "19",
    134. "labels": ["Ip"],
    135. "properties": {
    136. "address": "127.0.0.1"
    137. }
    138. }, {
    139. "id": "20",
    140. "labels": ["icons"],
    141. "properties": {
    142. "description": "Map node labels to Font Awesome icons",
    143. "type": "object",
    144. "example": {
    145. "Address": "home",
    146. "BirthDate": "birthday-cake",
    147. "Password": "asterisk",
    148. "Phone": "phone",
    149. "User": "user"
    150. },
    151. "deafult": "{}"
    152. }
    153. }, {
    154. "id": "21",
    155. "labels": ["zoomIn"],
    156. "properties": {
    157. "description": "Scroll up to zoom in.",
    158. "type": "function"
    159. }
    160. }, {
    161. "id": "22",
    162. "labels": ["zoomOut"],
    163. "properties": {
    164. "description": "Scroll down to zoom out.",
    165. "type": "function"
    166. }
    167. }, {
    168. "id": "23",
    169. "labels": ["zoomFit"],
    170. "properties": {
    171. "description": "Adjust the graph to the container once it has been loaded.",
    172. "type": "boolean",
    173. "values": [true, false],
    174. "default": false
    175. }
    176. }, {
    177. "id": "24",
    178. "labels": ["Api"],
    179. "properties": {}
    180. }, {
    181. "id": "25",
    182. "labels": ["Google"],
    183. "properties": {
    184. "url": "https://www.google.com/#q=\"neo4jd3\""
    185. }
    186. }],
    187. "relationships": [{
    188. "id": "1",
    189. "type": "HAS_PHONE_NUMBER",
    190. "startNode": "1",
    191. "endNode": "2",
    192. "properties": {
    193. "from": 1473581532586
    194. }
    195. }, {
    196. "id": "2",
    197. "type": "HAS_ADDRESS",
    198. "startNode": "1",
    199. "endNode": "3",
    200. "properties": {
    201. "from": 1473581532586
    202. }
    203. }, {
    204. "id": "3",
    205. "type": "HAS_BIRTH_DATE",
    206. "startNode": "1",
    207. "endNode": "4",
    208. "properties": {
    209. "from": 1473581532586
    210. }
    211. }, {
    212. "id": "4",
    213. "type": "HAS_PASSWORD",
    214. "startNode": "1",
    215. "endNode": "5",
    216. "properties": {
    217. "from": 1473581532586
    218. }
    219. }, {
    220. "id": "5",
    221. "type": "USED_DEVICE",
    222. "startNode": "1",
    223. "endNode": "6",
    224. "properties": {
    225. "from": 1473581532586
    226. }
    227. }, {
    228. "id": "6",
    229. "type": "HAS_SECURITY_ANSWER",
    230. "startNode": "1",
    231. "endNode": "7",
    232. "properties": {
    233. "from": 1473581532586
    234. }
    235. }, {
    236. "id": "7",
    237. "type": "DEVELOPES",
    238. "startNode": "1",
    239. "endNode": "8",
    240. "properties": {
    241. "from": 1470002400000
    242. }
    243. }, {
    244. "id": "8",
    245. "type": "REPOSITORY",
    246. "startNode": "8",
    247. "endNode": "9",
    248. "properties": {}
    249. }, {
    250. "id": "9",
    251. "type": "ISSUES",
    252. "startNode": "8",
    253. "endNode": "10",
    254. "properties": {}
    255. }, {
    256. "id": "10",
    257. "type": "HOSTED_ON",
    258. "startNode": "8",
    259. "endNode": "11",
    260. "properties": {}
    261. }, {
    262. "id": "11",
    263. "type": "HOSTED_ON",
    264. "startNode": "12",
    265. "endNode": "11",
    266. "properties": {}
    267. }, {
    268. "id": "12",
    269. "type": "HOSTED_ON",
    270. "startNode": "13",
    271. "endNode": "11",
    272. "properties": {}
    273. }, {
    274. "id": "13",
    275. "type": "HAS_EMAIL",
    276. "startNode": "1",
    277. "endNode": "14",
    278. "properties": {}
    279. }, {
    280. "id": "14",
    281. "type": "USED_CREDIT_CARD",
    282. "startNode": "1",
    283. "endNode": "15",
    284. "properties": {}
    285. }, {
    286. "id": "15",
    287. "type": "DEPENDS_ON",
    288. "startNode": "8",
    289. "endNode": "12",
    290. "properties": {}
    291. }, {
    292. "id": "16",
    293. "type": "DEPENDS_ON",
    294. "startNode": "8",
    295. "endNode": "13",
    296. "properties": {}
    297. }, {
    298. "id": "17",
    299. "type": "OPTIONS",
    300. "startNode": "8",
    301. "endNode": "16",
    302. "properties": {}
    303. }, {
    304. "id": "18",
    305. "type": "HAS_LANGUAGE",
    306. "startNode": "6",
    307. "endNode": "17",
    308. "properties": {}
    309. }, {
    310. "id": "19",
    311. "type": "HAS_COOKIE",
    312. "startNode": "6",
    313. "endNode": "18",
    314. "properties": {}
    315. }, {
    316. "id": "20",
    317. "type": "HAS_IP",
    318. "startNode": "6",
    319. "endNode": "19",
    320. "properties": {}
    321. }, {
    322. "id": "21",
    323. "type": "ICONS",
    324. "startNode": "16",
    325. "endNode": "20",
    326. "properties": {}
    327. }, {
    328. "id": "22",
    329. "type": "ZOOM_IN",
    330. "startNode": "24",
    331. "endNode": "21",
    332. "properties": {}
    333. }, {
    334. "id": "23",
    335. "type": "ZOOM_OUT",
    336. "startNode": "24",
    337. "endNode": "22",
    338. "properties": {}
    339. }, {
    340. "id": "24",
    341. "type": "ZOOM_FIT",
    342. "startNode": "16",
    343. "endNode": "23",
    344. "properties": {}
    345. }, {
    346. "id": "25",
    347. "type": "API",
    348. "startNode": "8",
    349. "endNode": "24",
    350. "properties": {}
    351. }, {
    352. "id": "26",
    353. "type": "GOOGLE_SEARCH",
    354. "startNode": "8",
    355. "endNode": "25",
    356. "properties": {}
    357. }]
    358. }
    359. }]
    360. }],
    361. "errors": []
    362. },
    363. node: [],
    364. relationships: []
    365. }
    366. },
    367. methods: {
    368. init () {
    369. console.log("chufa init 触发……")
    370. let _that = this
    371. var neo4jd3 = new Neo4jd3('#neo4jd3', {
    372. highlight: [{
    373. class: 'Project',
    374. property: 'name',
    375. value: 'neo4jd3'
    376. }, {
    377. class: 'User',
    378. property: 'userId',
    379. value: 'eisman'
    380. }],
    381. icons: {
    382. // 'Address': 'home',
    383. 'Api': 'gear',
    384. // 'BirthDate': 'birthday-cake',
    385. 'Cookie': 'paw',
    386. // 'CreditCard': 'credit-card',
    387. // 'Device': 'laptop',
    388. 'Email': 'at',
    389. 'Git': 'git',
    390. 'Github': 'github',
    391. 'Google': 'google',
    392. // 'icons': 'font-awesome',
    393. 'Ip': 'map-marker',
    394. 'Issues': 'exclamation-circle',
    395. 'Language': 'language',
    396. 'Options': 'sliders',
    397. 'Password': 'lock',
    398. 'Phone': 'phone',
    399. 'Project': 'folder-open',
    400. 'SecurityChallengeAnswer': 'commenting',
    401. 'User': 'user',
    402. 'zoomFit': 'arrows-alt',
    403. 'zoomIn': 'search-plus',
    404. 'zoomOut': 'search-minus'
    405. },
    406. minCollision: 60,
    407. neo4jData: this.nodeData,
    408. nodeRadius: 25,
    409. // onNodeDoubleClick: function (node) {
    410. // // switch (node.id) {
    411. // // case '25':
    412. // // // Google
    413. // // window.open(node.properties.url, '_blank');
    414. // // break;
    415. // // default:
    416. // // var maxNodes = 5,
    417. // // data = neo4jd3.randomD3Data(node, maxNodes);
    418. // // neo4jd3.updateWithD3Data(data);
    419. // // break;
    420. // // }
    421. // },
    422. // onRelationshipDoubleClick: function (relationship) {
    423. // // console.log('double click on relationship: ' + JSON.stringify(relationship));
    424. // },
    425. // onNodeMouseEnter: function (d) {
    426. // // console.log('鼠标经过获取数据哦…………',d,'--------JSON.stringify(d)可获得此节点所有数据-----------',JSON.stringify(d));
    427. // // this.mouseNode = d
    428. // _that.$emit('mouseNode', d)
    429. // // console.log(d,'xXXXXXXXXXXXXXXXXXXXXXXXXXX');
    430. // },
    431. // updateWithNeo4jData ( Neo4jD3JsonList ),
    432. zoomFit: true
    433. })
    434. },
    435. onNodeMouseEnter (d) {
    436. // console.log(d,'');
    437. }
    438. },
    439. created () {
    440. this.$nextTick(() => {
    441. this.init()
    442. })
    443. },
    444. }
    445. script>
    446. <style>
    447. .neo4jd3 {
    448. height: 100%;
    449. width: 100%;
    450. /* height: 400px; */
    451. /* margin-bottom: 100px; */
    452. overflow: hidden;
    453. }
    454. style>
    455. <style scope>
    456. .neo4jd3-graph {
    457. /* background-image: url("./pageBg.png"); */
    458. background-color: #ddd;
    459. background-size: cover;
    460. background-position: center center;
    461. border: 1px solid #ddd;
    462. border-radius: 5px;
    463. height: 3.8646rem;
    464. text-align-last: left;
    465. }
    466. /* 右侧关联图谱占比30% */
    467. .neo4jd3-info {
    468. width: 30%;
    469. font-size: 16px;
    470. padding: 10px;
    471. position: absolute;
    472. right: 0;
    473. top: 120px;
    474. /* display: none; */
    475. }
    476. .neo4jd3-info a {
    477. border: 1px solid;
    478. display: inline-block;
    479. font-size: 16px;
    480. line-height: 1.428571429;
    481. margin-left: 5px;
    482. margin-top: 5px;
    483. padding: 6px 12px;
    484. }
    485. .neo4jd3-info a.class {
    486. color: #fff;
    487. }
    488. .neo4jd3-info a.property {
    489. background-color: #fff;
    490. border-color: #ccc;
    491. color: #333;
    492. }
    493. .neo4jd3-info a.btn {
    494. margin-left: 5px;
    495. margin-top: 5px;
    496. opacity: 1;
    497. }
    498. .neo4jd3-info a.info {
    499. background-color: #a5abb6;
    500. border: 1px solid #9aa1ac;
    501. color: #fff;
    502. }
    503. .node.node-highlighted .ring {
    504. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
    505. filter: alpha(opacity=50);
    506. opacity: 0.5;
    507. stroke: #888;
    508. stroke-width: 12px;
    509. }
    510. .node .outline {
    511. cursor: pointer;
    512. fill: #a5abb6;
    513. pointer-events: all;
    514. stroke: #9aa1ac;
    515. stroke-width: 2px;
    516. }
    517. .node .ring {
    518. fill: none;
    519. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    520. filter: alpha(opacity=0);
    521. opacity: 0;
    522. stroke: #6ac6ff;
    523. stroke-width: 8px;
    524. }
    525. .node .text.icon {
    526. font-family: FontAwesome;
    527. }
    528. .node.selected .ring,
    529. .node:hover .ring {
    530. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
    531. filter: alpha(opacity=30);
    532. opacity: 0.3;
    533. }
    534. .relationship {
    535. cursor: default;
    536. }
    537. .relationship line {
    538. stroke: #aaa;
    539. }
    540. .relationship .outline {
    541. cursor: default;
    542. }
    543. .relationship .overlay {
    544. cursor: default;
    545. fill: #6ac6ff;
    546. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    547. filter: alpha(opacity=0);
    548. opacity: 0;
    549. }
    550. .relationship text {
    551. cursor: default;
    552. }
    553. .relationship.selected .overlay,
    554. .relationship:hover .overlay {
    555. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
    556. filter: alpha(opacity=30);
    557. opacity: 0.3;
    558. }
    559. svg {
    560. cursor: move;
    561. }
    562. style>

    总结: 

    其实,你只需要复制最后的组件源码即可,至于怎么封装,完全看你心情,我只是提供一个方法,你也大可随便找个地方保存,引入使用即可。

    需要注意的地方也就是初始数据,注意格式即可。再就是要记得设置高度,我用的是rem单位,记得改一下。

     

  • 相关阅读:
    c++暴力法
    WPF转义字符
    图像处理——图像增强
    从 HTA 中启动应用程序
    为什么工业设计公司价格这么高?
    JWT笔记
    Cookie与Session
    华为机试真题实战应用【赛题代码篇】-数组组成的最小数字(附Java和Python代码)
    推荐一个专利检索的网站
    Guide1:Building a RESTful Web Service
  • 原文地址:https://blog.csdn.net/wanghaoyingand/article/details/126843184