2017-09-14 48 views
0

这是一个简单的数学问题,但由于某种原因,我无法弄清楚正确的公式,试图在过去的一个小时内没有成功地解决它。将图表缩放为固定屏幕高度公式

假设我们有一个图表,纵轴上的最大点为chartMax,纵轴上的最小点为chartMin,最大屏幕尺寸高度为667,我们希望在此屏幕中使用此图表无论其最小值和最大值如何,它都将始终适合屏幕。

图表有很多点(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, 
]; 

回答

0

如果你只想规模在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/

相关问题