old_d3.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <script type="text/javascript">
  2. var data = {{ data|json_encode|raw }};
  3. let now = Date.now();
  4. /*let year_now = new Date().getFullYear() + 0;
  5. let futur = new Date( year_now.toString() );
  6. now = futur.getTime();*/
  7. let lane_array = []
  8. data.forEach(function(e){
  9. lane_array.push(e.title)
  10. });
  11. let datum = new Date('19990');
  12. let sec = datum.getTime();
  13. var lanes = lane_array,
  14. laneLength = lanes.length,
  15. items = data,
  16. timeBegin = sec ,
  17. timeEnd = now ;
  18. // get the d3 lib
  19. // append the d3
  20. var m = [20, 15, 15, 120], //top right bottom left -> margin
  21. w = 960 - m[1] - m[3], // width
  22. h = (laneLength * 150) - m[0] - m[2], // height
  23. miniHeight = laneLength * 12 + 50,
  24. mainHeight = h - miniHeight - 50;
  25. //scales
  26. var x = d3.scaleLinear()
  27. .domain([timeBegin, timeEnd])
  28. .range([0, w]);
  29. var x1 = d3.scaleLinear()
  30. .range([0, w]);
  31. var y1 = d3.scaleLinear()
  32. .domain([0, laneLength])
  33. .range([0, mainHeight]);
  34. var y2 = d3.scaleLinear()
  35. .domain([0, laneLength])
  36. .range([0, miniHeight]);
  37. //creat the chart body
  38. var w_box = w + m[1] + m[3];
  39. var h_box = h + m[0] + m[2];
  40. var chart = d3.select(".datavis")
  41. .append("svg")
  42. .attr("viewBox",'0 0 '+ w_box +' '+ h_box )
  43. /*.attr("width", w + m[1] + m[3])
  44. .attr("height", h + m[0] + m[2])*/
  45. .attr("class", "chart");
  46. var main = chart.append("g")
  47. .attr("transform", "translate(" + m[3] + "," + m[0] + ")")
  48. .attr("width", w)
  49. .attr("height", mainHeight)
  50. .attr("class", "main");
  51. // X axis
  52. const xaxis = d3.scaleLinear()
  53. .domain([timeBegin, timeEnd])
  54. .range([0, w]);
  55. main.append("g")
  56. .attr("transform", "translate(0," + mainHeight + ")")
  57. .call(d3.axisBottom(xaxis).ticks(4).tickFormat(d3.timeFormat('%Y')))
  58. .selectAll("text")
  59. /*.attr("transform", "translate(-10,10)rotate(-45)")*/
  60. .style("text-anchor", "center")
  61. .style("font-size", 10)
  62. .style("fill", "black");
  63. //main lanes and texts
  64. main.append("g").selectAll(".laneLines")
  65. .data(items)
  66. .enter().append("line")
  67. .attr("x1", m[1])
  68. .attr("y1", function(d) {return y1(d.lane);})
  69. .attr("x2", w)
  70. .attr("y2", function(d) {return y1(d.lane);})
  71. .attr("stroke", "lightgray")
  72. main.append("g").selectAll(".laneText")
  73. .data(lanes)
  74. .enter().append("text")
  75. .text(function(d) {return d;})
  76. .attr("x", -m[1])
  77. .attr("y", function(d, i) {return y1(i + .5);})
  78. .attr("dy", ".5ex")
  79. .attr("text-anchor", "end")
  80. .attr("class", "laneText");
  81. // coalition
  82. var coa = main.append("g").selectAll("rect")
  83. .data(items)
  84. .enter().append("rect")
  85. .attr("class", "coalition")
  86. .attr("x", function(d) {var date = new Date(d.start); console.log(d.start); return x(d.start);})
  87. .attr("y", function(d) {return y1(d.lane) + 10;})
  88. .attr("height", function(d) {return .8 * y1(1);})
  89. .attr("width", function(d) { return x(d.end) - x(d.start);})
  90. .attr("pos", function(d) {return d.lane });
  91. // labels
  92. /*labels = main.append("g").selectAll("text")
  93. .data(items, function (d) { return d.id; })
  94. .attr("x", function(d) {return x(d.start + 2);});
  95. labels.enter().append("text")
  96. .text(function(d) {return d.id;})
  97. .attr("fill", "White")
  98. .attr("x", function(d) {return x(d.start + 2);})
  99. .attr("y", function(d) {return y1(d.lane + .5);});*/
  100. //ok
  101. var act = main.append("g").selectAll("activists")
  102. .data(items)
  103. .enter()
  104. act.selectAll("rects")
  105. .data(function(d){ return d.activist })
  106. .enter().append("rect")
  107. .attr("x", function (d) { return x(d.start) } )
  108. .attr("y", function(d, i) { let l = items[d.lane].activist.length; let margin = y1(1)/(l+1) ; /*return y1(d.lane)*/ return y1( d.lane ) + ( margin * (i+1)) - 10 })
  109. .attr("height", 15 )
  110. .attr("width", function(d) {return x(d.end) - x(d.start);} )
  111. .attr("class", "rectangle");
  112. act.selectAll("name")
  113. .data(function(d){ return d.activist })
  114. .enter().append("text")
  115. .text(function(d) {return d.name;})
  116. .attr("class", "name")
  117. .attr("x", function(d) {return x(d.start + .4) + 7;})
  118. .attr("y", function(d, i) {let l = items[d.lane].activist.length; let margin = y1(1)/(l+1) ; return y1( d.lane ) + ( margin * (i+1) ) + 1 });
  119. //adjust rect size
  120. let rect_array = $(".rectangle");
  121. $(".name").each(function(i, e){
  122. var bbox = this.getBBox();
  123. let name_size = bbox.width;
  124. let rect_size = $(rect_array[i]).width();
  125. if( name_size+ 15 > rect_size){
  126. $(rect_array[i]).css("width", name_size + 15);
  127. }
  128. });
  129. //chart interaction
  130. $(".rectangle").on("click", function(){
  131. $(".container_info").fadeIn("fast", function(){
  132. $(this).css("display", "grid");
  133. });
  134. });
  135. $(".container_info .button").on("click", function(){
  136. $(".container_info").fadeOut();
  137. });
  138. </script>