简体   繁体   English

d3.js v4 sankey图-粒子和拖动不起作用

[英]d3.js v4 sankey diagram— particles and drag not working

I am building d3.js sankey diagram and following this example https://bl.ocks.org/emeeks/e9d64d27f286e61493c9 . 我正在构建d3.js sankey图,并遵循以下示例https://bl.ocks.org/emeeks/e9d64d27f286e61493c9 I am fairly new to React (2 days) and I am trying to build this visualization d3.js and React. 我对React非常陌生(2天),我正在尝试构建此可视化d3.js和React。 The dragmove function works fine but throws me an error Dragmove function dragmove函数可以正常工作,但会抛出错误Dragmove函数

function dragmove(d) {

  var rectY = d3.select(this).select("rect").attr("y");

  d.y0 = d.y0 + d3.event.dy;

  var yTranslate = d.y0 - rectY;
  console.log(yTranslate)

  d3.select(this).attr("transform", 
            "translate(0" + "," + (yTranslate) + ")");

  sankey.update(graph);
  link.attr("d", D3sankey.sankeyLinkHorizontal());
}

error 错误

Line 90:   Unexpected string concatenation of literals

While for generating particles my particles remains empty even after setting the timer and gives empty arrays. 在生成粒子的同时,即使设置了计时器,粒子仍保持为空,并给出了空数组。

This is the code for generating particles-- 这是用于生成粒子的代码-

    var linkExtent = d3.extent(Data.links, function (d) {return d.o_value});
  var frequencyScale = d3.scaleLinear().domain(linkExtent).range([0.05,1]);
  var particleSize = d3.scaleLinear().domain(linkExtent).range([1,5]);


  Data.links.forEach(function (link) {
    link.freq = frequencyScale(link.o_value);
    link.particleSize = 2;
    link.particleColor = d3.scaleLinear().domain([0,1])
    .range([link.source.color, link.target.color]);
  })

  var t = d3.timer(tick, 1000);
  var particles = [];

  function tick(elapsed, time) {
    particles = particles.filter(function (d) {return d.current < d.path.getTotalLength()});

    d3.selectAll("path.link")
    .each(
      function (d) {
//        if (d.freq < 1) {
        for (var x = 0;x<2;x++) {
          var offset = (Math.random() - .5) * (d.dy - 4);
          if (Math.random() < d.freq) {
            var length = this.getTotalLength();
            particles.push({link: d, time: elapsed, offset: offset, path: this, length: length, animateTime: length, speed: 0.5 + (Math.random())})
          }
        }

//        }
/*        else {
          for (var x = 0; x<d.freq; x++) {
            var offset = (Math.random() - .5) * d.dy;
            particles.push({link: d, time: elapsed, offset: offset, path: this})
          }
        } */
      });

    particleEdgeCanvasPath(elapsed);
  }

  function particleEdgeCanvasPath(elapsed) {
    var context = d3.select("canvas").node().getContext("2d")

    context.clearRect(0,0,1000,1000);

      context.fillStyle = "gray";
      context.lineWidth = "1px";
    for (var x in particles) {
        var currentTime = elapsed - particles[x].time;
//        var currentPercent = currentTime / 1000 * particles[x].path.getTotalLength();
        particles[x].current = currentTime * 0.15 * particles[x].speed;
        var currentPos = particles[x].path.getPointAtLength(particles[x].current);
        context.beginPath();
      context.fillStyle = particles[x].link.particleColor(0);
        context.arc(currentPos.x,currentPos.y + particles[x].offset,particles[x].link.particleSize,0,2*Math.PI);
        context.fill();
    }
  }

This is the link to my project: https://codesandbox.io/s/github/kuhu12/react-d3 这是我项目的链接: https : //codesandbox.io/s/github/kuhu12/react-d3

You concatenation error is due to unnecessary use of concatenation eslint 您的串联错误是由于不必要使用串联eslint

you can replace 你可以更换

d3.select(this).attr("transform", "translate(0" + "," + (yTranslate) + ")");

with

d3.select(this).attr("transform", "translate(0," + (yTranslate) + ")");

Or even better use the new template literals for all your string concatenation 甚至最好对所有字符串连接使用新的模板文字

d3.select(this).attr("transform", `translate(0,${yTranslate})`);

See more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals 在此处查看更多信息https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals

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

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