如何在d3 js的曲线上绘制svg圆圈?

时间:2017-04-07 19:39:48

标签: javascript css d3.js svg

我正在编写一个软件,我在其中绘制一些并排放置的svg圆圈,用户可以调整位置,改变线坐标。期望的结果是这个

enter image description here

现在圆圈的位置不正确,使圆圈向左移动

enter image description here

代码

function curve(val){
        var w = d3.select("#new_row_1").attr("width");

        var 
        numLines = 1,
        lineSpacing = 18,
        parabDepth = -30;

        var row_spacing = 18;

        if(val == 0){
            parabDepth = -18;
        }
        else if(val == 1){
            parabDepth = 0;
        }
        else if(val == 2){
            parabDepth = 20;
        }
        else if(val == 3){
            parabDepth = 30;
        }
        else if(val == 4){
            parabDepth = 40;
        }
        else if(val == 5){
            parabDepth = 50;
            // row_spacing = 18.41;
        }
        else if(val == 6){
            // row_spacing = 18.5;
            parabDepth = 60;
        }
        else if(val == 7){
            // row_spacing = 18.6;
            parabDepth = 70;
        }
        else if(val == 8){
            // row_spacing = 18.62;
            parabDepth = 80;
        }
        else if(val == 9){
            parabDepth = 90;
        }
        else if(val == 10){
            parabDepth = 100;
        }
        else if(val == 11){
            parabDepth = 110;
        }
        else if(val == 12){
            parabDepth = 120;
        }
        else if(val == 13){
            parabDepth = 130;
        }
        else if(val == 14){
            parabDepth = 140;
        }
        else if(val == 15){
            parabDepth = 150;
            row_spacing = 20;
        }

        width = w ;
        
        var curveData = [];

        curveData.push([0,0]);
        curveData.push([width/3 * 1, lineSpacing + parabDepth ]);
        curveData.push([width/3 * 2, lineSpacing + parabDepth ]);
        curveData.push([width/3 * 3, 1]);

        var line = d3.line()
            .x(function(d) {
              return d[0];
            })
            .y(function(d) {
              return d[1] + 8;
            })
            .curve(d3.curveCardinal);

        var svg = d3.select("#new_row_1").attr("height", (numLines * lineSpacing) + lineSpacing + parabDepth + row_spacing).attr("width", width);

        var g = svg.selectAll(".line")
            .data(d3.range(numLines))
            .enter()
            .append("g")
            .attr("class", "line")
            .attr("transform", function(d){
              return "translate(7," + (d*lineSpacing) + ")";
            });
        
        var path = g.append("path")
            .attr("d", line(curveData))
            .style("fill", "none")
            .style("stroke", "pink")
            .style("stroke-width","4")
            .each(function(){
                var g = d3.select(this.parentNode),
                    self = d3.select(this),
                    pathLength = width;
                  
                g.selectAll("circle")
                    .data(d3.range(1, width, row_spacing))
                    .enter()
                    .append("circle")
                    .attr("transform", (d,i) => {
                        var p = this.getPointAtLength(d);
                        return "translate(" + p.x + "," + p.y + ")";
                    })
                    .attr("r", 7)
                    .style("fill", function(d,i){
                        if(i == quant_col2 - 1){
                            return "red";
                        }else if(i == quant_col2/2){
                            return "yellow";
                        }
                        else if(i == quant_col2 - 2){
                            return "green";
                        }
                        else{
                            return "white";
                        }
                    })
                    .attr("stroke","black")
                    .attr("stroke-width","1");
            });
}

setTimeout(function(){
    curve(1)
},2000)
<script src="https://d3js.org/d3.v4.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<svg width="252" height="18" id="new_row_1" class="new_row" style="top: 150px; left: 302px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><g><circle cx="8" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="3" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">1</text></g><g><circle cx="26" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="21" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">2</text></g><g><circle cx="44" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="39" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">3</text></g><g><circle cx="62" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="57" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">4</text></g><g><circle cx="80" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="75" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">5</text></g><g><circle cx="98" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="93" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">6</text></g><g><circle cx="116" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="111" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">7</text></g><g><circle cx="134" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="129" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">8</text></g><g><circle cx="152" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="147" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">9</text></g><g><circle cx="170" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="165" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">10</text></g><g><circle cx="188" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="183" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">11</text></g><g><circle cx="206" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="201" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">12</text></g><g><circle cx="224" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="219" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">13</text></g><g><circle cx="242" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="237" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">14</text><g class="label" style="display: none;"><rect x="126" y="1" width="14" height="14" style="fill: black;"></rect><text dx="130" dy="11" style="font-size: 9px; font-weight: 400; fill: white;">A</text></g></g></svg>

该值将由用户控制,这意味着它将动态更改。 我想通过线路均匀地处理它们。怎么办呢?

2 个答案:

答案 0 :(得分:2)

您正在使用C:\Program Files\Android 查找沿曲线一定距离的点的X,Y坐标。惨不忍睹那是错的。你想找到给定X的Y坐标。

没有内置的SVG功能。你将不得不自己计算。为此你需要贝塞尔方程的多项式形式,这不是一件容易的事。

由于你只是制作一个抛物线,你最好避免使用贝塞尔曲线,只需用普通的简单二次方程计算曲线( ax ^ 2 + bx + c

除此之外:如果您决定坚持使用贝塞尔曲线,那么您应该使用二次贝塞尔曲线,而不是使用二次贝塞尔曲线。二次贝塞尔曲线具有它们总是形成抛物线段的特性。

答案 1 :(得分:1)

不应使用width,而应使用getTotalLength()作为行的大小:

pathLength = this.getTotalLength();

以下是仅包含此更改的演示:

function curve(val) {
  var w = d3.select("#new_row_1").attr("width");

  var
    numLines = 1,
    lineSpacing = 18,
    parabDepth = -30;

  var row_spacing = 18;

  if (val == 0) {
    parabDepth = -18;
  } else if (val == 1) {
    parabDepth = 0;
  } else if (val == 15) {
    parabDepth = 150;
    row_spacing = 20;
  } else {
    parabDepth = val * 10;
  }

  width = w;

  var curveData = [];

  curveData.push([0, 0]);
  curveData.push([width / 3 * 1, lineSpacing + parabDepth]);
  curveData.push([width / 3 * 2, lineSpacing + parabDepth]);
  curveData.push([width / 3 * 3 - 8, - 8]);

  var line = d3.line()
    .x(function(d) {
      return d[0];
    })
    .y(function(d) {
      return d[1];
    })
    .curve(d3.curveCardinal);

  var svg = d3.select("#new_row_1").attr("height", (numLines * lineSpacing) + lineSpacing + parabDepth + row_spacing).attr("width", width);

  var g = svg.selectAll(".line")
    .data(d3.range(numLines))
    .enter()
    .append("g")
    .attr("class", "line")
    .attr("transform", function(d) {
      return "translate(7," + (d * lineSpacing + 7) + ")";
    });

  var path = g.append("path")
    .attr("d", line(curveData))
    .style("fill", "none")
    .style("stroke", "pink")
    .style("stroke-width", "4")
    .each(function() {
      var g = d3.select(this.parentNode),
        self = d3.select(this),
        pathLength = this.getTotalLength();

      g.selectAll("circle")
        .data(d3.range(1, pathLength, row_spacing))
        .enter()
        .append("circle")
        .attr("transform", (d, i) => {
          var p = this.getPointAtLength(d);
          return "translate(" + p.x + "," + p.y + ")";
        })
        .attr("r", 7)
        .style("fill", "darkslategray")
        .attr("stroke", "black")
        .attr("stroke-width", "1");
    });
}

setTimeout(function() {
  curve(5)
}, 500)
<script src="https://d3js.org/d3.v4.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<svg width="252" height="18" id="new_row_1" class="new_row" style="top: 150px; left: 302px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"><g><circle cx="8" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="3" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">1</text></g><g><circle cx="26" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="21" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">2</text></g><g><circle cx="44" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="39" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">3</text></g><g><circle cx="62" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="57" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">4</text></g><g><circle cx="80" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="75" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">5</text></g><g><circle cx="98" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="93" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">6</text></g><g><circle cx="116" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="111" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">7</text></g><g><circle cx="134" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="129" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">8</text></g><g><circle cx="152" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="147" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">9</text></g><g><circle cx="170" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="165" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">10</text></g><g><circle cx="188" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="183" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">11</text></g><g><circle cx="206" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="201" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">12</text></g><g><circle cx="224" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="219" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">13</text></g><g><circle cx="242" cy="8" r="7" style="fill: white; stroke: black; stroke-width: 1;"></circle><text dx="237" dy="11" class="label" style="font-size: 10px; font-weight: 500; display: none;">14</text><g class="label" style="display: none;"><rect x="126" y="1" width="14" height="14" style="fill: black;"></rect><text dx="130" dy="11" style="font-size: 9px; font-weight: 400; fill: white;">A</text></g></g></svg>

或者,如果您想使用width,则不应使用getPointAtLength(),因为LeBeau在his answer中解释过,因为在欧氏几何中,直线比任何其他几何都短形状从A点到B点。

除此之外,你还有两个问题:1。到处都有很多魔法数字; 2.你可以设计一个更好的数学来获得弧线。我相信你应该摆脱这些代码并编写一种全新的,不同的方法。