Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. import store from './../../store/store.js'
  2.  
  3. export default function renderEntitiesGraph(graph) {
  4. var d3 = require("d3");
  5. console.log('graph edges = ', graph.edges);
  6. var svg = d3.select("#entities-graph-canvas"),
  7. width = +svg.attr("width"),
  8. height = +svg.attr("height");
  9.  
  10. var nodeTooltip = d3.select("body").append("div")
  11. .attr("class", "node-tooltip")
  12. .on("mouseover", function() {
  13. clearTimeout(nodeTooltipHideTimeout);
  14. nodeTooltip.transition()
  15. .duration(50)
  16. .style("opacity", 1);
  17. })
  18. .on("mouseleave", function() {
  19. console.log("here!");
  20. nodeTooltip.transition()
  21. .duration(200)
  22. .style("opacity", 0);
  23.  
  24. setTimeout(
  25. () => nodeTooltip.style("display", "none"),
  26. 200);
  27. });
  28.  
  29. var linkTooltip = d3.select("body").append("div")
  30. .attr("class", "link-tooltip")
  31. .on("mouseover", function() {
  32. clearTimeout(linkTooltipHideTimeout);
  33. linkTooltip.transition()
  34. .duration(50)
  35. .style("opacity", 1);
  36. })
  37. .on("mouseleave", function() {
  38. linkTooltip.transition()
  39. .duration(200)
  40. .style("opacity", 0);
  41.  
  42. setTimeout(
  43. () => linkTooltip.style("display", "none"),
  44. 200);
  45. });
  46.  
  47. var nodeTooltipHideTimeout, linkTooltipHideTimeout;
  48.  
  49. var nodeTooltipTitleWrapper = nodeTooltip.append("div")
  50. .attr("class", "node-tooltip-title-wrapper");
  51.  
  52. var nodeTooltipTitle = nodeTooltipTitleWrapper.append("a")
  53. .attr("class", "node-tooltip-title");
  54.  
  55. var linkTooltipTitle = linkTooltip.append("div")
  56. .attr("class", "link-tooltip-title")
  57. .html("Статьи");
  58.  
  59. var linkTooltipArticlesList = linkTooltip.append("ul")
  60. .attr("class", "link-tooltip-articles-list");
  61.  
  62. var linkTooltipListItems = [];
  63. var linkTooltipListArticles = [];
  64. for (var i = 0; i < 6; i++) {
  65. var newItem = linkTooltipArticlesList.append('li').attr("class", "link-tooltip-article-item");
  66. var newArticle = newItem.append('a');
  67. linkTooltipListItems.push(newItem);
  68. linkTooltipListArticles.push(newArticle);
  69. }
  70.  
  71. let centralNode = graph.vertices[0];
  72. let colors = {};
  73. graph.vertices.forEach(function(d) {
  74.  
  75. d.color = d3.interpolateRainbow(Math.random());
  76. // colors.push(d.color)
  77. colors[d.id] = d.color;
  78. // $('#label-' + d.id).css("background", d.color)
  79. });
  80. // store.state.colorLabels = colors;
  81.  
  82.  
  83. var n_vertices = graph.vertices.length;
  84.  
  85. var simulation = d3.forceSimulation()
  86. .force("link", d3.forceLink().iterations(4).id(function(d) {
  87. return d.id;
  88. }))
  89. // .force("size", )
  90. .force("charge", d3.forceManyBody().strength(-10000 / n_vertices))
  91. .force("center", d3.forceCenter(400, 250))
  92. .force("x", d3.forceX())
  93. .force("y", d3.forceY());
  94.  
  95. // svg.attr("transform","translate(100, 50) scale(" + 10 / n_vertices + ", " + 10 / n_vertices + " )");
  96.  
  97. var g = svg.append("g");
  98.  
  99. var link = g.append("g")
  100. .attr("class", "links")
  101. .selectAll("line")
  102. .data(graph.edges)
  103. .enter().append("line")
  104. .attr("stroke-width", function(d) {
  105. return Math.sqrt(d.articles.length);
  106. })
  107. .on("mouseover", function(d) {
  108.  
  109. clearTimeout(linkTooltipHideTimeout);
  110.  
  111.  
  112. var x = this.getBoundingClientRect().x + this.getBoundingClientRect().width / 2;
  113. var y = this.getBoundingClientRect().y + this.getBoundingClientRect().height / 2;
  114.  
  115. for (var i = 0; i < 5; i++) {
  116. linkTooltipListItems[i].style('display', 'block');
  117. }
  118. for (var i = 0; i < Math.min(6, d.articles.length); i++) {
  119. var articleId = d.articles[i];
  120. var article = graph.articles.find(x => x.id === articleId);
  121. linkTooltipListArticles[i]
  122. .html(article.title)
  123. .attr("href", article.link);
  124. }
  125. for (var i = d.articles.length; i < 6; i++) {
  126. linkTooltipListItems[i].style('display', 'none');
  127. }
  128.  
  129. linkTooltip.style("display", "block");
  130.  
  131. linkTooltip.transition()
  132. .duration(200)
  133. .style("opacity", 1);
  134.  
  135. linkTooltip
  136. .style("left", x - parseFloat(linkTooltip.style("width")) / 2 + window.scrollX + "px")
  137. .style("top", y - parseFloat(linkTooltip.style("height")) - 15 + window.scrollY + "px");
  138. })
  139. .on("mouseout", function(d) {
  140. linkTooltip.transition()
  141. .duration(400)
  142. .style("opacity", 0);
  143.  
  144. linkTooltipHideTimeout = setTimeout(
  145. () => linkTooltip.style("display", "none"),
  146. 400);
  147. });
  148.  
  149. var node = g.append("g")
  150. .attr("class", "nodes")
  151. .selectAll("circle")
  152. .data(graph.vertices)
  153. .enter().append("circle")
  154. .attr("r", 9)
  155. .attr("fill", function(d) {
  156. return d.color;
  157. })
  158. .call(d3.drag()
  159. .on("start", dragstarted)
  160. .on("drag", dragged)
  161. .on("end", dragended))
  162. .on("mouseover", function(d) {
  163. console.log("mouse over node!")
  164.  
  165. clearTimeout(nodeTooltipHideTimeout);
  166.  
  167. var x = this.getBoundingClientRect().x + this.getBoundingClientRect().width / 2;
  168. var y = this.getBoundingClientRect().y;
  169.  
  170.  
  171. nodeTooltip.transition()
  172. .duration(200)
  173. .style("opacity", 1);
  174.  
  175. nodeTooltipTitle.html(d.title)
  176. .attr('href', '#/profile/' + d.id)
  177. .attr('onclick', "location.reload()");
  178.  
  179. <<<<<<< HEAD
  180. nodeTooltip
  181. .style("left", x - parseFloat(nodeTooltip.style("width")) / 2 + window.scrollX + "px")
  182. .style("top", y - parseFloat(nodeTooltip.style("height")) - 15 + window.scrollY + "px");
  183.  
  184. nodeTooltip
  185. =======
  186. nodeTooltip
  187. .style("left", x - parseFloat(nodeTooltip.style("width")) / 2 + window.scrollX + "px")
  188. .style("top", y - parseFloat(nodeTooltip.style("height")) - 15 + window.scrollY + "px");
  189.  
  190. nodeTooltip
  191. >>>>>>> df21880d2a370e1e2643e5392e8a8ca190101423
  192. .style("display", "block");
  193. })
  194. .on("mouseout", function(d) {
  195.  
  196. nodeTooltip.transition()
  197. .duration(1000)
  198. .style("opacity", 0);
  199.  
  200. nodeTooltipHideTimeout = setTimeout(
  201. () => nodeTooltip.style("display", "none"),
  202. 1000);
  203. });
  204.  
  205. svg.call(d3.zoom()
  206. .scaleExtent([1 / 2, 8])
  207. .on("zoom", zoomed));
  208.  
  209. function zoomed() {
  210. g.attr("transform", d3.event.transform);
  211. }
  212.  
  213. simulation
  214. .nodes(graph.vertices)
  215. .on("tick", ticked);
  216.  
  217. simulation.force("link")
  218. .links(graph.edges);
  219.  
  220. function ticked() {
  221. link
  222. .attr("x1", function(d) {
  223. return d.source.x;
  224. })
  225. .attr("y1", function(d) {
  226. return d.source.y;
  227. })
  228. .attr("x2", function(d) {
  229. return d.target.x;
  230. })
  231. .attr("y2", function(d) {
  232. return d.target.y;
  233. });
  234.  
  235. node
  236. .attr("cx", function(d) {
  237. return d.x;
  238. })
  239. .attr("cy", function(d) {
  240. return d.y;
  241. });
  242. }
  243.  
  244. function dragstarted(d) {
  245. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  246. d.fx = d.x;
  247. d.fy = d.y;
  248. }
  249.  
  250. function dragged(d) {
  251. d.fx = d3.event.x;
  252. d.fy = d3.event.y;
  253. }
  254.  
  255. function dragended(d) {
  256. if (!d3.event.active) simulation.alphaTarget(0);
  257. d.fx = null;
  258. d.fy = null;
  259. }
  260.  
  261. function redirectToEntityPage(id) {
  262. router.push('/profile/'+ entityId + '/');
  263. }
  264.  
  265. return colors;
  266. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement