用d3绘制非连续线

问题描述:

我使用d3.js做一个简单的线形图。我想知道是否有一种方法可以在图形中创建孔,也就是说,如果在没有可用数据时线可以中断或切断。

I'm using d3.js to make a simple line graph. I want to know if there's a way to create "holes" in the graph, that is, if the line can be interrupted or cut when there is no data available.

我正在寻找删除我不需要从域中的地方,或在特定的段设置线权重为0,但我找不到

I'm looking into either delete the places I don't need from the domain, or setting the line weight to 0 in specific segments, but I can't find a way to do either of these.

感谢您的帮助!

D3 line 生成器有一个内置函数可以执行此操作, line.defined 。你可以使用这个函数来控制你的行被定义的位置和它不在哪里(比如你有丢失的数据)。如果你想使你的行未定义,当点数组中的第二个值是一个javascript NaN值,你可以说:

The D3 line generator has a built in function to do just this, line.defined. You can use this function to control where your line is defined and where it is not (like where you have missing data.) If you wanted to make your line undefined whenever the second value in the point array is a javascript NaN value, you could say:

line.defined(function(d) { return !isNaN(d[1]); });

这里是一个很好的例子。