我有一个<svg>
其宽度为其容器的 100%。当容器调整大小时,我更新线性xScale.range()
代表新调整大小的宽度<svg>
。显然,然后我需要将范围重新应用到我的缩放行为,如中所述Zoom.x() 文档:
如果以编程方式修改比例的域或范围,则应再次调用此函数。设置 x 比例还会将比例重置为 1,并将平移重置为 [0, 0]。
的重置scale
and translate
这是我遇到问题的地方。如果我之前在调整大小之前放大过,我会调用zoom.x( xScale )
现在 d3 认为图表的比例为 1,平移为 0,0,因此,我无法缩小或平移。
我处理调整大小的方法是否不正确?
看起来最好的策略是缓存比例并转换值,重置,然后重新应用。作为记录,此代码(在我的调整大小处理程序中)大致显示了我的解决方案:
// Cache scale
var cacheScale = zoom.scale();
// Cache translate
var cacheTranslate = zoom.translate();
// Cache translate values as percentages/ratio of the full width
var cacheTranslatePerc = zoom.translate().map( function( v, i, a )
{
return (v * -1) / getFullWidth();
} );
// Manually reset the zoom
zoom.scale( 1 ).translate( [0, 0] );
// Update range values based on resized container dimensions
xScale.range( [0, myResizedContainerWidth] );
// Apply the updated xScale to the zoom
zoom.x( xScale );
// Revert the scale back to our cached value
zoom.scale( cacheScale );
// Overwrite the x value of cacheTranslate based on our cached percentage
cacheTranslate[0] = -(getFullWidth() * cacheTranslatePerc[0]);
// Finally apply the updated translate
zoom.translate( cacheTranslate );
function getFullWidth()
{
return xScale.range()[1] * zoom.scale();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)