[英]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.