路径交叉导致它们的某些部分消失 [D3 路径生成]

我一直试图在SVG上生成两条路径,但似乎其中一条路径略有消失。我想知道是什么导致了这个问题。我尝试过使用不同的路径绘制公式,但没有运气。代码非常简单,如下所示:


     import * as d3 from "d3";


     let canvas =  d3.select('#canvas');

        let svg = canvas.append('svg')

        .attr('width',1820)

        .attr('height', 790)

        .style('background-color', 'black')


        var pathInfo = [

            {

              p: 'P',

              data:  [[0, 40], [50, 30], [100, 50], [200, 60], [300, 90]]

            },

            {

              p:'p2',

              data:  [[0, 40], [50, 30], [100, 50], [200, 60], [350, 90]]

            }

        ]

        const curve = d3.line().curve(d3.curveNatural);

        svg.selectAll('path')

        .data(pathInfo)

        .enter()

        .append('path')

        .attr('d', (d)=> curve(d.data)).attr('stroke', 'white')

http://img1.mukewang.com/633564710001335307680356.jpg

UYOU
浏览 118回答 1
1回答

白猪掌柜的

如果将路径的属性设置为橙色并降低不透明度,则所看到的行为的原因应该会变得更加明显:fillSVG 路径的默认填充颜色为黑色 - 默认情况下,无论路径是否为闭合路径,都会填充路径。上面的橙色区域在您的示例中为黑色。因此,您看到的结果是由于第二条路径的填充覆盖了第一条路径在其大部分长度上的笔触。因此,只有部分行程可见。背面背景会混淆问题,但解决方案是将填充设置为:nonelet canvas =&nbsp; d3.select('body');&nbsp; &nbsp; &nbsp; &nbsp; let svg = canvas.append('svg')&nbsp; &nbsp; &nbsp; &nbsp; .attr('width',1820)&nbsp; &nbsp; &nbsp; &nbsp; .attr('height', 790)&nbsp; &nbsp; &nbsp; &nbsp; .style('background-color', 'black')&nbsp; &nbsp; &nbsp; &nbsp; var pathInfo = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p: 'P',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:&nbsp; [[0, 40], [50, 30], [100, 50], [200, 60], [300, 90]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p:'p2',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:&nbsp; [[0, 40], [50, 30], [100, 50], [200, 60], [350, 90]]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; const curve = d3.line().curve(d3.curveNatural);&nbsp; &nbsp; &nbsp; &nbsp; svg.selectAll('path')&nbsp; &nbsp; &nbsp; &nbsp; .data(pathInfo)&nbsp; &nbsp; &nbsp; &nbsp; .enter()&nbsp; &nbsp; &nbsp; &nbsp; .append('path')&nbsp; &nbsp; &nbsp; &nbsp; .attr('d', (d)=> curve(d.data)).attr('stroke', 'white').attr("fill","none");<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript