cytoscape.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, {useState} from 'react';
  2. import {useRouter} from 'next/router'
  3. import CytoscapeComponent from "react-cytoscapejs";
  4. const layout = {
  5. name: 'circle',
  6. fit: true, // whether to fit the viewport to the graph
  7. padding: 32, // the padding on fit
  8. boundingBox: undefined, // constrain layout bounds; { x1, y1, x2, y2 } or { x1, y1, w, h }
  9. avoidOverlap: true, // prevents node overlap, may overflow boundingBox and radius if not enough space
  10. nodeDimensionsIncludeLabels: false, // Excludes the label when calculating node bounding boxes for the layout algorithm
  11. spacingFactor: 0.9, // Applies a multiplicative factor (>0) to expand or compress the overall area that the nodes take up
  12. radius: 150, // the radius of the circle
  13. startAngle: -2 / 4 * Math.PI, // where nodes start in radians
  14. //sweep: undefined, // how many radians should be between the first and last node (defaults to full circle)
  15. clockwise: true, // whether the layout should go clockwise (true) or counterclockwise/anticlockwise (false)
  16. sort: undefined, // a sorting function to order the nodes; e.g. function(a, b){ return a.data('weight') - b.data('weight') }
  17. animate: false, // whether to transition the node positions
  18. animationDuration: 500, // duration of animation in ms if enabled
  19. animationEasing: undefined, // easing of animation if enabled
  20. //animateFilter: function ( node, i ){ return true; }, // a function that determines whether the node should be animated. All nodes animated by default on animate enabled. Non-animated nodes are positioned immediately when the layout starts
  21. ready: undefined, // callback on layoutready
  22. stop: undefined, // callback on layoutstop
  23. transform: function (node, position) {
  24. return position;
  25. } // transform a given node position. Useful for changing flow direction in discrete layouts
  26. };
  27. const styleSheet = [{
  28. selector: "node",
  29. style: {
  30. "background-color": "#666",
  31. "font-size": "12px",
  32. "width": "20px",
  33. "height": "20px",
  34. "label":"data(label)"
  35. }
  36. }, {
  37. selector: "label",
  38. style: {"font-size": "12px"},
  39. },
  40. {
  41. selector: 'edge',
  42. style: {
  43. 'width': 2,
  44. "height": 200,
  45. 'line-color': '#b2b2b2',
  46. 'target-arrow-color': '#ccc',
  47. // 'target-arrow-shape': 'triangle',
  48. 'curve-style': 'straight'
  49. }
  50. }];
  51. function Graph({graph}) {
  52. const [width, setWidth] = useState("300px");
  53. const [height, setHeight] = useState("300px");
  54. const [graphData, setGraphData] = useState({
  55. nodes: graph.nodes,
  56. edges: graph.edges
  57. });
  58. let myCyRef;
  59. const router = useRouter()
  60. //TODO: Listen to query change/ graphdata change to update state of this component
  61. // Can use this: https://github.com/vercel/next.js/discussions/12661
  62. return (
  63. <>
  64. <div className="right-bar-container">
  65. <h3>Interactive Graph</h3>
  66. <div
  67. style={{
  68. border: "1px solid #ddd",
  69. backgroundColor: "#f5f6fe",
  70. borderRadius:"8px"
  71. }}
  72. >
  73. <CytoscapeComponent
  74. elements={CytoscapeComponent.normalizeElements(graphData)}
  75. // pan={{ x: 200, y: 200 }}
  76. style={{width: width, height: height}}
  77. zoomingEnabled={true}
  78. maxZoom={2}
  79. minZoom={0.5}
  80. autounselectify={false}
  81. boxSelectionEnabled={true}
  82. layout={layout}
  83. stylesheet={styleSheet}
  84. cy={cy => {
  85. myCyRef = cy;
  86. console.log("EVT", cy);
  87. cy.on("tap", "node", evt => {
  88. var node = evt.target;
  89. let nodeData = node.data();
  90. if (typeof nodeData.id === 'string') {
  91. const path = '/note/' + node.data().id
  92. router.push(path)
  93. }
  94. });
  95. }}
  96. />
  97. </div>
  98. </div>
  99. </>
  100. );
  101. }
  102. export default Graph;