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

时间:2018-11-12 21:52:33

标签: javascript d3.js

我正在构建d3.js sankey图,并遵循此示例https://bl.ocks.org/emeeks/e9d64d27f286e61493c9。我对React非常陌生(2天),我正在尝试构建此可视化d3.js和React。 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());
}

错误

Line 90:   Unexpected string concatenation of literals

在生成粒子的同时,即使设置了计时器,粒子仍为空,并给出了空数组。

这是用于生成粒子的代码-

    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();
    }
  }

这是我的项目的链接: https://codesandbox.io/s/github/kuhu12/react-d3

1 个答案:

答案 0 :(得分:0)

您的串联错误是由于不必要使用串联same discussion

您可以替换

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

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

或者甚至更好地将新模板文字用于所有字符串连接

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

在此处eslint

查看更多