2017-09-14 46 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/

相關問題