Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. <script>
  2. $.post("getorderedtheories",{},
  3. function(data,status){
  4. render(data);
  5. });
  6. function render(data){
  7. var width = document.getElementById('timelineContainer').offsetWidth;
  8. var height = document.getElementById('timelineContainer').offsetHeight;
  9.  
  10. var scaleX = d3.scale.linear()
  11. .domain([data[0].theoryYear,data[data.length-1].theoryYear])
  12. .range([10, width-30]);
  13.  
  14. var svg = d3.select(".timelineContainer")
  15. .append("svg")
  16. .attr("width", width)
  17. .attr("height", height)
  18. .style("pointer-events", "all");
  19.  
  20. svg.append("rect")
  21. .attr("x", 10)
  22. .attr("y", height/2)
  23. .attr("width", width-35)
  24. .attr("height", 10);
  25. svg.selectAll("circle")
  26. .data(data)
  27. .enter()
  28. .append("circle")
  29. .attr("cx", function(d){
  30. return scaleX(d.theoryYear);
  31. })
  32. .attr("cy", (height/2)+5)
  33. .attr("r", 10)
  34. .on("mouseover", handleMouseOver)
  35. .on("mouseout", handleMouseOut)
  36. .on("click", function(d) {
  37. $.post("gettheorydata",
  38. {
  39. id : d.id
  40. },
  41. function(data,status){
  42. for(field in data[0]){
  43. $("#"+field).text(data[0][field]);
  44. }
  45. });
  46. });
  47. function handleMouseOver(d, i) { // Add interactivity
  48. d3.select(this).attr({
  49. fill: "orange",
  50. });
  51. svg.append("text").attr({
  52. id: "t" + d.theoryYear + "-" + "-" + i, // Create an id for text so we can select it later for removing on mouseout
  53. x: function() { return scaleX(d.theoryYear) - 50; },
  54. y: (height/2)+10,
  55. })
  56. .attr("transform", "rotate(90,"+(scaleX(d.theoryYear)-35)+","+((height/2)+40)+")")
  57. .text(function() {
  58. return [d.theoryName]; // Value of the text
  59. });
  60. }
  61. function handleMouseOut(d, i) {
  62. d3.select(this).attr({
  63. fill: "black",
  64. });
  65. // Select text by id and then remove
  66. d3.select("#t" + d.theoryYear + "-" + "-" + i).remove(); // Remove text location
  67. }
  68. }
  69. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement