Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 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. nodeTooltip
  180. .style("left", x - parseFloat(nodeTooltip.style("width")) / 2 + window.scrollX + "px")
  181. .style("top", y - parseFloat(nodeTooltip.style("height")) - 15 + window.scrollY + "px");
  182.  
  183. nodeTooltip
  184. .style("display", "block");
  185. })
  186. .on("mouseout", function(d) {
  187.  
  188. nodeTooltip.transition()
  189. .duration(1000)
  190. .style("opacity", 0);
  191.  
  192. nodeTooltipHideTimeout = setTimeout(
  193. () => nodeTooltip.style("display", "none"),
  194. 1000);
  195. });
  196.  
  197. svg.call(d3.zoom()
  198. .scaleExtent([1 / 2, 8])
  199. .on("zoom", zoomed));
  200.  
  201. function zoomed() {
  202. g.attr("transform", d3.event.transform);
  203. }
  204.  
  205. simulation
  206. .nodes(graph.vertices)
  207. .on("tick", ticked);
  208.  
  209. simulation.force("link")
  210. .links(graph.edges);
  211.  
  212. function ticked() {
  213. link
  214. .attr("x1", function(d) {
  215. return d.source.x;
  216. })
  217. .attr("y1", function(d) {
  218. return d.source.y;
  219. })
  220. .attr("x2", function(d) {
  221. return d.target.x;
  222. })
  223. .attr("y2", function(d) {
  224. return d.target.y;
  225. });
  226.  
  227. node
  228. .attr("cx", function(d) {
  229. return d.x;
  230. })
  231. .attr("cy", function(d) {
  232. return d.y;
  233. });
  234. }
  235.  
  236. function dragstarted(d) {
  237. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  238. d.fx = d.x;
  239. d.fy = d.y;
  240. }
  241.  
  242. function dragged(d) {
  243. d.fx = d3.event.x;
  244. d.fy = d3.event.y;
  245. }
  246.  
  247. function dragended(d) {
  248. if (!d3.event.active) simulation.alphaTarget(0);
  249. d.fx = null;
  250. d.fy = null;
  251. }
  252.  
  253. function redirectToEntityPage(id) {
  254. router.push('/profile/'+ entityId + '/');
  255. }
  256.  
  257. return colors;
  258. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement