d3 notes

Submitted by code_admin on Wed, 08/28/2019 - 15:48

https://github.com/d3/d3
https://observablehq.com/@d3/force-directed-graph
https://thecodingtutorials.blogspot.com/2012/07/introduction-to-d3.html

Some messing around with animation

  1. <html>
  2.  
  3. <head>
  4.  
  5. <script type="text/javascript">
  6. window.onload = function()
  7. {
  8.  //Create the SVG graph.
  9.  var svg = d3.select("body").append("svg").attr("width", "100%").attr("height", "100%");
  10.  
  11.  
  12.  
  13.  //Add data to the graph and call enter.
  14.  var dataEnter = svg.selectAll("rect").data([8, 22, 31, 36, 48, 17, 25]).enter();
  15.  
  16.  
  17.  
  18.  //The height of the graph (without text).
  19.  var graphHeight = 450;
  20.  
  21.  //The width of each bar.
  22.  var barWidth = 80;
  23.  
  24.  //The distance between each bar.
  25.  var barSeparation = 10;
  26.  
  27.  //The maximum value of the data.
  28.  var maxData = 50;
  29.  
  30.  
  31.  
  32.  //The actual horizontal distance from drawing one bar rectangle to drawing the next.
  33.  var horizontalBarDistance = barWidth + barSeparation;
  34.  
  35.  
  36.  //The horizontal and vertical ofsvg.selectAll("rect")fsets of the text that displays each bar's value.
  37.  var textXOffset = horizontalBarDistance / 2 - 12;
  38.  var textYOffset = 20;
  39.  
  40.  
  41.  //The value to multiply each bar's value by to get its height.
  42.  var barHeightMultiplier = graphHeight / maxData;
  43.  
  44.  //The actual Y position of every piece of text.
  45.  var textYPosition = graphHeight + textYOffset;
  46.  
  47.  
  48.  //Draw the bars.
  49.  dataEnter.append("rect").attr("x", function(d, i)
  50.  {
  51.   return i * horizontalBarDistance;
  52.  }).attr("y", function(d)
  53.  {
  54.   return graphHeight - d * barHeightMultiplier;
  55.  }).attr("width", function(d)
  56.  {
  57.   return barWidth;
  58.  }).attr("height", function(d)
  59.  {
  60.   return d * barHeightMultiplier;
  61. }).attr("id", function(d, i)
  62.  {
  63.   return "dd" + i;
  64.  });
  65.  
  66.  
  67.  
  68.  //Draw the text.
  69.  dataEnter.append("text").text(function(d)
  70.  {
  71.   return d;
  72.  }).attr("x", function(d, i)
  73.  {
  74.   return i * horizontalBarDistance + textXOffset;
  75.  }).attr("y", textYPosition);
  76.  
  77.  var mySquare=svg.append("rect")
  78.   .attr("x",60)
  79.   .attr("y",60)
  80.   .attr("width",60)
  81.   .attr("height",60)
  82.   .attr("id",function(d, i)
  83.   {
  84.    console.log('d',i)
  85.    return "dd7";
  86.   });
  87.  
  88.   mySquare
  89.     .transition()
  90.     .attr("x",320)
  91.  
  92.     mySquare
  93.       .transition()
  94.       .attr("width",120); // will make it bigger
  95.  
  96.     mySquare
  97.       .style("fill","blue") // if the fill is originally left blank and comes
  98.                              //  from a style sheet, it will start as black
  99.       .transition()
  100.       .style("fill","blue");
  101.  
  102.     mySquare
  103.       .transition()
  104.       .style("opacity",0.3);
  105.  
  106.     var right = [false,false,false,false,false,false,false,false,false,false]
  107.  
  108.     var mof = function(cur, i) {
  109.       console.log(cur, i)
  110.       var d = 0
  111.       var w = 100
  112.       if (right[i]) {
  113.         d = 320 + (i * w)
  114.       } else {
  115.         d = 0 + (i * w)
  116.       }
  117.       right[i] = !right[i]
  118.       svg.select("#dd" + i).transition().attr("x",d);
  119.       // mySquare.transition().attr("x",d);
  120.     }
  121.  
  122.     svg.selectAll("rect").on("mouseover", mof)
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. </script>
  131.  
  132. </head>
  133.  
  134. <body>
  135.  
  136. </body>
  137.  
  138. </html>
RJM Article Type
Work Notes