Javascript D3js-从特定域的d3.line()中获取最大值
我正在制作一个多折线图,我已经实现了一个画笔,以便能够放大x轴上的特定域。但是,当我放大时,我希望y轴沿比例缩放,以便其域从Javascript D3js-从特定域的d3.line()中获取最大值,javascript,d3.js,Javascript,D3.js,我正在制作一个多折线图,我已经实现了一个画笔,以便能够放大x轴上的特定域。但是,当我放大时,我希望y轴沿比例缩放,以便其域从[0,maxY]开始,其中maxY是x轴上当前选择的最大y值。要生成行,我使用的是d3.line()(它在x和y值之间有连接)。这是我当前计算maxY值的方式: //Find max and min values in data to set the domain of the y-axis var maxArray = updatedData.map(function(v
[0,maxY]
开始,其中maxY
是x轴上当前选择的最大y值。要生成行,我使用的是d3.line()
(它在x和y值之间有连接)。这是我当前计算maxY
值的方式:
//Find max and min values in data to set the domain of the y-axis
var maxArray = updatedData.map(function(variable){
//First map the values in the array to a new array
var valuesArray = variable.values.map(function(d){
return d.value;
})
//Find max value in array
return Math.max(...valuesArray);
});
var maxY = Math.max(...maxArray);
在这里我设置了刻度并创建了d3.line()
:
下面是我设置新的xScale.domain()
并放大该间隔的代码(刷牙结束时调用该间隔):
我想做的是在当前选择的域中查找最大y值。我知道我可以过滤数据值以删除不在当前选定域中的数据值,然后从中计算最大值(就像我对原始域所做的那样)。但似乎应该有一个更简单的解决办法。我在d3.line()
的文档中没有找到任何可以计算最大值的函数
有什么简单的方法可以从d3.line()
计算最大值吗
多亏了没有更简单的解决方案,因为您必须以某种方式过滤这些值,以便只考虑所选x域中的值。但是,使用对的两个嵌套调用至少可以给它一个愉快的外观,并通过避免额外调用.filter()
节省一些迭代。由于d3.max()
将忽略null
值,因此如果当前数据在x域的边界之外,您可以使用它通过返回null
来过滤您的值。要获得最大值,可以使用以下方法:
const maxY = xDomain => d3.max(updatedData, variable =>
d3.max(
variable.values,
v => v.Timestamp >= xDomain[0] && v.Timestamp <= xDomain[1] ? v.value : null
)
);
constmaxy=xDomain=>d3.max(updatedata,variable=>
d3.max(
变量值,
v=>v.Timestamp>=xDomain[0]&&v.Timestamp d3.max(updateData,变量=>
d3.max(
变量值,
v=>(!xDomain | | v.时间戳>=xDomain[0]&v.时间戳
function brushend(){
//sourceEvent - the underlying input event, such as mousemove or touchmove.
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
var brushInterval = d3.event.selection; //The interval of the current brushed selection
//If the function is called with no selection: ignore
if(!brushInterval) return;
//Enable reset button
resetButton.attr("disabled", null)
.on("click", resetAxis);
var newDomain = brushInterval.map(xScale.invert, xScale);
//TODO: Find max and min values in data to set the domain of the y-axis
xScale.domain(newDomain);
chart.selectAll(".line")
.transition()
.duration(1000)
.attr("d", function(d){ return line(d.values)});
chart.select(".x-axis")
.transition()
.duration(1000)
.call(xAxis);
//Remove the visual brush
d3.select(".brush").call(brush.move, null);
}
const maxY = xDomain => d3.max(updatedData, variable =>
d3.max(
variable.values,
v => v.Timestamp >= xDomain[0] && v.Timestamp <= xDomain[1] ? v.value : null
)
);