[英]Scaling a chart to a fixed screen height formula
这是一个简单的数学问题,但是由于某些原因,我无法找出正确的公式,在过去的一个小时里一直试图解决该问题,但没有成功。
比方说,我们有在垂直轴上的最高点的图表chartMax
和垂直轴分点chartMin
,和667最大屏幕尺寸的高度,我们要适应这个屏幕高度内这个图表不管其最小值和最大值,它将始终适合屏幕。
图表中有许多点(hMin, hMax)
每个点(hMin, hMax)
高度为h = Math.abs(hMax - hMin)
,为了使所有这些点都适合屏幕内,我们需要将每个点的高度乘以一个因素,这个因素的公式是什么?
const chartMax = 4000;
const chartMin = 3500;
const screenMax = 667;
const factor = /* math magic */;
const points = [
Math.abs(4000 - 3900) * factor,
Math.abs(3800 - 3700) * factor,
Math.abs(3600 - 3500) * factor,
];
如果只想在y轴上缩放:
const height = chartMax - chartMin;
const scalar = windowHeight / height; // scale factor
要将点从图表转换到屏幕:
function transformPoint(onChart) {
return (onChart - chartMin) * scalar; // make lowest point drop to the bottom, and the others drop by the same amount
// then scale it up from chart space to screen space
}
这是一个示例: https : //jsfiddle.net/bftbqqvv/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.