Javascript 当路径长度发生变化时,保持阵列一致

Javascript 当路径长度发生变化时,保持阵列一致,javascript,d3.js,svg,Javascript,D3.js,Svg,我有一个由用户绘制的路径,使用D3.js 我希望在用户绘制的路径上定义一个dasharray,但是,当它更改其形状和长度时,dasharray的行为不一致,并且间隙正在移动并变小 这是一个密码笔: 随便用叉子叉。。绘制用户路径的函数如下所示: DrawItGraph.prototype.draw = function() { ... var pos = this.d3.mouse(event.target) if (pos[1] > this.chartHeig

我有一个由用户绘制的路径,使用D3.js

我希望在用户绘制的路径上定义一个dasharray,但是,当它更改其形状和长度时,dasharray的行为不一致,并且间隙正在移动并变小

这是一个密码笔:

随便用叉子叉。。绘制用户路径的函数如下所示:

DrawItGraph.prototype.draw = function() {
    ...

    var pos = this.d3.mouse(event.target)
    if (pos[1] > this.chartHeight) return

    var date = this.clamp(this.middleValue + 2629743, this.maxValue, this.convection.x.invert(pos[0]))
    var value = this.clamp(0, this.convection.y.domain()[1], this.convection.y.invert(pos[1]))

    this.userData.forEach(function (d) {
        if (Math.round(Math.abs(d.date - date) / 60000000) < 50) {
            d.value = value
            d.defined = true
        }
    })

    this.yourDataSel.at({d: this.line.defined(this.format('defined'))(this.userData)})

    if (this.d3.mean(this.userData, this.format('defined')) === 1) {
        this.graphCompleted = true
    }
}
DrawItGraph.prototype.draw=function(){
...
var pos=this.d3.mouse(event.target)
如果(位置[1]>此图表高度)返回
var date=this.clamp(this.middleValue+2629743,this.maxValue,this.convertive.x.invert(位置[0]))
var value=this.clamp(0,this.convertive.y.domain()[1],this.convertive.y.invert(位置[1]))
this.userData.forEach(函数(d){
if(数学圆整(数学绝对值(d.date-date)/60000000)<50){
d、 价值=价值
d、 定义=真
}
})
this.yourDataSel.at({d:this.line.defined(this.format('defined'))(this.userData)})
if(this.d3.mean(this.userData,this.format('defined'))==1){
this.graphCompleted=true
}
}
我怀疑我必须根据路径长度动态更改笔划dashoffset和/或笔划dasharray,但是,我不知道正确的公式是什么


非常感谢

您的问题不是虚线阵列或虚线偏移。让我们看一个生成的路径示例:

。你的台词{
行程:#ffb531;
笔画宽度:10;
行程:15;
}


Majestic。非常感谢保罗,现在天气晴朗如水。我将尝试在这里发布一个带有解决方案的代码笔分支。我正在尝试解决这个问题,但这并不是那么容易。。。出于某种原因,正如你所说,我正在创建一条双倍长度的直线,来回移动,但我不知道为什么。我想这是你用d3生成的图形类型。我不是d3用户,所以我不能就下一步做什么提供任何建议。很抱歉