问题描述
在我的D3"图表中,如何将yDomain
设置为最大值.有什么建议 ?如果我们在nov 10
上看到我的line
和area
,则开箱即用.
In My D3 chart, How to set yDomain
to the max value. Any suggestions ?If we see on nov 10
my line
and area
is going out of the box.
我的代码沙箱此处
我正在分别计算yDomainMagnitude和yDomainStartupMagnitude的两个域,但是现在如何合并或合并两者并分配给yDomain.
I am calculating the two domains separately for yDomainMagnitude and yDomainStartupMagnitude, but now how to consolidate or take the union of both and assign to yDomain.
var yDomainMagnitude = d3.extent(data, function(d) {
return d.magnitude;
});
var yDomainStartupMagnitude = d3.extent(data, function(d) {
return d.startupMagnitude;
});
var yDomain = yDomainStartupMagnitude; // here I have to have union of both and assign.
var xScale = d3
.scaleTime()
.range([0, width])
.domain(xDomain);
var yScale = d3
.scaleLinear()
.range([height, 0])
.domain(yDomain);
推荐答案
有几种方法可以就地计算yDomain
.您的确实可以工作,但是可以将其大大简化:
There are several ways to calculate the yDomain
in place. Yours does obviously work, it can be significantly simplified, though:
var yDomain = [
d3.min(data, d => Math.min(d.magnitude, d.startupMagnitude)),
d3.max(data, d => Math.max(d.magnitude, d.startupMagnitude))
];
此方法分别使用Math.min()
/Math.max()
检查任何数据点的值magnitude
或startupMagnitude
较小/较大.然后利用d3.min()
和d3.max()
来计算这些值上的全局范围.
This approach checks whatever value magnitude
or startupMagnitude
is smaller / greater using Math.min()
/ Math.max()
, respectively, for any data point. It then utilizes d3.min()
and d3.max()
to calculate the global extent over those values.
这篇关于如何在D3 Js中将y轴(yDomain)值设置为max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!