简体   繁体   English

在 d3.js 中,使用 svg.node() 的目的是什么?

[英]In d3.js, what is the purpose of using svg.node()?

I'm trying to understand the code I got.我试图理解我得到的代码。 I think I kind of went through the details.我想我有点了解细节。 But still, there are something I really can't understand.但是,还是有一些我真的无法理解。

Things that I can't understand are我无法理解的事情是

1) svg.node() 1) svg.node()

2) bisect/ bisector part 2)二等分/二等分部分

3) for (let i = 0; i < n; ++i) { if (i % 1 ==0 || 1 ) yield svg.node(); 3) for (let i = 0; i < n; ++i) { if (i % 1 ==0 || 1 ) yield svg.node();



1) svg.node() 1) svg.node()

In the generator 'gen()', it yields svg.node().在生成器 'gen()' 中,它生成 svg.node()。 and it is literally drawing things on my svg ( I checked it just uncomment the line only)它实际上是在我的 svg 上绘制东西(我检查它只是取消注释该行)

What is svg.node() and why do I need to use that argument to draw something?什么是 svg.node() 以及为什么我需要使用该参数来绘制一些东西? because, normally, when I want to draw something, I just need to d3.select('svg').append('###').... then I could draw something on my 'svg' however, in this generator, it is using 'svg.node()' to draw something.因为,通常,当我想画一些东西时,我只需要 d3.select('svg').append('###').... 然后我可以在我的 'svg' 上画一些东西,但是,在这个生成器,它使用 'svg.node()' 来绘制一些东西。 I wonder why and what is 'svg.node()我想知道为什么以及什么是 'svg.node()

function* gen() {
  var random = d3.randomNormal(); // Try randomUniform?

  const n = 2000;
  const width = window.innerWidth;
  const height = 400;
  const radius = 2;
  const dodge = dodger(radius * 2 + 1);
  const margin = { top: 0, right: 10, bottom: 20, left: 10 };

  const values = Float64Array.from({ length: n }, random);

  const x = d3.scaleLinear(d3.extent(values), [
    margin.left,
    width - margin.right
  ]);
  const svg = d3
    .select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height)
    .style("overflow", "visible");

  // var fillScale = d3.scaleSequentialLog(chroma.interpolateSinebow)

  svg
    .append("g")
    .attr("transform", `translate(0,${height - margin.bottom})`)
    .call(d3.axisBottom(x));

  function dodger(radius) {
    const radius2 = radius ** 2;
    const bisect = d3.bisector(d => d.x);
    const circles = [];

    return function(x) {
      const l = bisect.left(circles, x - radius);
      const r = bisect.right(circles, x + radius);
      let y = 0;
      for (let i = l; i < r; ++i) {
        const { x: xi, y: yi } = circles[i];
        const x2 = (xi - x) ** 2;
        const y2 = (yi - y) ** 2;
        if (radius2 > x2 + y2) {
          y = yi + Math.sqrt(radius2 - x2) + 1e-6;
          i = l - 1;
          continue;
        }
      }
      circles.splice(bisect.left(circles, x, l, r), 0, { x, y }); 
      return y;
    };
  }

  for (let i = 0; i < n; ++i) {
    if (i % 5 === 0) yield svg.node();
    const cx = x(values[i]); // x(values[i]);
    const cy = height - margin.bottom - dodge(cx) - radius - 1;

    svg
      .append("circle")
      .attr("cx", cx)
      .attr("cy", -400)
      .attr("r", radius)
      .attr("fill", "red")
      .attr("fill", "#9e0dd7") //purple
      .transition()
      .duration(650)
      .ease(d3.easeBounce)
      .attr("cy", cy);
  }

  yield svg.node();
}


const genratorAnimation = gen(); 

let result = genratorAnimation.next();
//genratorAnimation.next();
let interval = setInterval(function(){
   if(!result.done) {
     genratorAnimation.next();
   }
   else {
    clearInterval(interval)
   }
}, 50);

2)bisect/ bisector /splice 2)二等分/二等分/拼接

I don't get the logic that the code uses for placing balls using bisect, bisector and splice.我不明白代码使用二等分、二等分和拼接放置球的逻辑。 I understand bisect is spitting the index number that new entity would go into the array.(based on ascending order) splice is replacing or insert entities in the array.我知道 bisect 正在吐出新实体将进入数组的索引号。(基于升序) splice 正在替换或插入数组中的实体。 However, I don't get how the code enables the arrangement of dropping balls desired.但是,我不明白代码如何实现所需的落球布置。

3) for (let i = 0; i < n; ++i) { if (i % 1 ==0 || 1 ) yield svg.node(); 3) for (let i = 0; i < n; ++i) { if (i % 1 ==0 || 1 ) yield svg.node();

I played with the number by changing 1==0 to 2==0 and 10==0.我通过将 1==0 更改为 2==0 和 10==0 来玩这个数字。 It seems like the equation defines the number of balls that would drop each time.这个方程似乎定义了每次掉落的球数。 Why?为什么?

I know I asked too many question and I'm open to ask several time by posting many questions one by one.我知道我问了太多问题,并且我愿意通过一个接一个地发布许多问题来多次提问。 But it would be very grateful if someone could answer.但如果有人能回答,将不胜感激。

Thank you in advance.先感谢您。

if you console.log(svg.node()),you can find a object contains total element nodes of svg in your console.如果您使用 console.log(svg.node()),您可以在控制台中找到一个包含 svg 总元素节点的对象。

return svg.node() is usually used to show or update svg figure. return svg.node() 通常用于显示或更新 svg 图。

svg.node() returns the svg value. svg.node() 返回 svg 值。 But, I don't know why it draws something by putting the argument.但是,我不知道为什么它通过提出论点来得出一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM